Skip to content

Commit 9929cf0

Browse files
vladarsidharthachatterjee
authored andcommitted
fix(gatsby): Handle special characters in windows paths (#19600)
1 parent 81bfb1b commit 9929cf0

File tree

34 files changed

+238
-72
lines changed

34 files changed

+238
-72
lines changed

packages/gatsby-core-utils/src/__tests__/path.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { joinPath, isNodeInternalModulePath } = require(`../path`)
1+
const { joinPath, isNodeInternalModulePath, slash } = require(`../path`)
22
const os = require(`os`)
33

44
describe(`paths`, () => {
@@ -30,4 +30,24 @@ describe(`paths`, () => {
3030
expect(isNodeInternalModulePath(modulePath)).toBe(false)
3131
})
3232
})
33+
34+
describe(`slash path`, () => {
35+
it(`can correctly slash path`, () => {
36+
;[
37+
[`foo\\bar`, `foo/bar`],
38+
[`foo/bar`, `foo/bar`],
39+
[`foo\\中文`, `foo/中文`],
40+
[`foo/中文`, `foo/中文`],
41+
[`foo/жä`, `foo/жä`],
42+
[`foo\\жä`, `foo/жä`],
43+
].forEach(([path, expectRes]) => {
44+
expect(slash(path)).toBe(expectRes)
45+
})
46+
})
47+
48+
it(`does not modify extended length paths`, () => {
49+
const extended = `\\\\?\\some\\path`
50+
expect(slash(extended)).toBe(extended)
51+
})
52+
})
3353
})

packages/gatsby-core-utils/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
exports.createContentDigest = require(`./create-content-digest`)
22
exports.joinPath = require(`./path`).joinPath
33
exports.isNodeInternalModulePath = require(`./path`).isNodeInternalModulePath
4+
exports.slash = require(`./path`).slash
45
exports.cpuCoreCount = require(`./cpu-core-count`)
56
exports.urlResolve = require(`./url`).resolve
67
exports.isCI = require(`./ci`).isCI

packages/gatsby-core-utils/src/path.js

+19
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,22 @@ const nodePaths = [
7474
*/
7575
export const isNodeInternalModulePath = fileName =>
7676
nodePaths.some(regTest => regTest.test(fileName))
77+
78+
/**
79+
* slash
80+
* --
81+
* Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
82+
*
83+
*
84+
* @param {String} path
85+
* @return {String} slashed path
86+
*/
87+
export function slash(path) {
88+
const isExtendedLengthPath = /^\\\\\?\\/.test(path)
89+
90+
if (isExtendedLengthPath) {
91+
return path
92+
}
93+
94+
return path.replace(/\\/g, `/`)
95+
}

packages/gatsby-page-utils/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
"bluebird": "^3.7.1",
2525
"chokidar": "3.3.0",
2626
"fs-exists-cached": "^1.0.0",
27+
"gatsby-core-utils": "^1.0.19",
2728
"glob": "^7.1.6",
2829
"lodash": "^4.17.15",
29-
"micromatch": "^3.1.10",
30-
"slash": "^3.0.0"
30+
"micromatch": "^3.1.10"
3131
},
3232
"devDependencies": {
3333
"@babel/cli": "^7.7.0",

packages/gatsby-page-utils/src/watch-directory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Promise = require(`bluebird`)
22
const chokidar = require(`chokidar`)
3-
const slash = require(`slash`)
3+
const { slash } = require(`gatsby-core-utils`)
44

55
module.exports = async (path, glob, onNewFile, onRemovedFile) =>
66
new Promise((resolve, reject) => {

packages/gatsby-plugin-mdx/loaders/mdx-loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const genMdx = require(`../utils/gen-mdx`)
2121
const withDefaultOptions = require(`../utils/default-options`)
2222
const createMDXNode = require(`../utils/create-mdx-node`)
2323
const { createFileNode } = require(`../utils/create-fake-file-node`)
24-
const slash = require(`slash`)
24+
const { slash } = require(`gatsby-core-utils`)
2525

2626
const DEFAULT_OPTIONS = {
2727
footnotes: true,

packages/gatsby-plugin-mdx/loaders/mdx-scopes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require(`fs`)
22
const path = require(`path`)
3-
const slash = require(`slash`)
3+
const { slash } = require(`gatsby-core-utils`)
44
const loaderUtils = require(`loader-utils`)
55
const { MDX_SCOPES_LOCATION } = require(`../constants`)
66

packages/gatsby-plugin-mdx/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"escape-string-regexp": "^1.0.5",
3434
"eval": "^0.1.4",
3535
"fs-extra": "^8.1.0",
36+
"gatsby-core-utils": "^1.0.19",
3637
"gray-matter": "^4.0.2",
3738
"json5": "^2.1.1",
3839
"loader-utils": "^1.2.3",
@@ -45,7 +46,6 @@
4546
"remark": "^10.0.1",
4647
"remark-retext": "^3.1.3",
4748
"retext-english": "^3.0.4",
48-
"slash": "^3.0.0",
4949
"static-site-generator-webpack-plugin": "^3.4.2",
5050
"style-to-object": "^0.3.0",
5151
"underscore.string": "^3.3.5",

packages/gatsby-plugin-mdx/utils/create-fake-file-node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const slash = require(`slash`)
1+
const { slash } = require(`gatsby-core-utils`)
22
const path = require(`path`)
33
const fs = require(`fs`)
44
const mime = require(`mime`)

packages/gatsby-plugin-offline/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
"dependencies": {
1010
"@babel/runtime": "^7.7.2",
1111
"cheerio": "^1.0.0-rc.3",
12+
"gatsby-core-utils": "^1.0.19",
1213
"glob": "^7.1.6",
1314
"idb-keyval": "^3.2.0",
1415
"lodash": "^4.17.15",
15-
"slash": "^3.0.0",
1616
"workbox-build": "^4.3.1"
1717
},
1818
"devDependencies": {

packages/gatsby-plugin-offline/src/gatsby-node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
let fs = require(`fs`)
33
let workboxBuild = require(`workbox-build`)
44
const path = require(`path`)
5-
const slash = require(`slash`)
5+
const { slash } = require(`gatsby-core-utils`)
66
const glob = require(`glob`)
77
const _ = require(`lodash`)
88

packages/gatsby-remark-images/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
"@babel/runtime": "^7.7.2",
1111
"chalk": "^2.4.2",
1212
"cheerio": "^1.0.0-rc.3",
13+
"gatsby-core-utils": "^1.0.19",
1314
"is-relative-url": "^3.0.0",
1415
"lodash": "^4.17.15",
1516
"mdast-util-definitions": "^1.2.5",
1617
"potrace": "^2.1.2",
1718
"query-string": "^6.8.3",
18-
"slash": "^3.0.0",
1919
"unist-util-select": "^1.5.0",
2020
"unist-util-visit-parents": "^2.1.2"
2121
},

packages/gatsby-remark-images/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const _ = require(`lodash`)
1313
const { fluid, stats, traceSVG } = require(`gatsby-plugin-sharp`)
1414
const Promise = require(`bluebird`)
1515
const cheerio = require(`cheerio`)
16-
const slash = require(`slash`)
16+
const { slash } = require(`gatsby-core-utils`)
1717
const chalk = require(`chalk`)
1818

1919
// If the image is relative (not hosted elsewhere)

packages/gatsby-source-filesystem/src/__tests__/utils.js

-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const {
44
getRemoteFileExtension,
55
getRemoteFileName,
66
createProgress,
7-
slash,
87
} = require(`../utils`)
98
const reporter = require(`gatsby-cli/lib/reporter`)
109
const progress = require(`progress`)
@@ -62,21 +61,3 @@ describe(`createProgress`, () => {
6261
expect(bar).toHaveProperty(`total`)
6362
})
6463
})
65-
66-
describe(`slash path`, () => {
67-
it(`can correctly slash path`, () => {
68-
;[
69-
[`foo\\bar`, `foo/bar`],
70-
[`foo/bar`, `foo/bar`],
71-
[`foo\\中文`, `foo/中文`],
72-
[`foo/中文`, `foo/中文`],
73-
].forEach(([path, expectRes]) => {
74-
expect(slash(path)).toBe(expectRes)
75-
})
76-
})
77-
78-
it(`does not modify extended length paths`, () => {
79-
const extended = `\\\\?\\some\\path`
80-
expect(slash(extended)).toBe(extended)
81-
})
82-
})

packages/gatsby-source-filesystem/src/create-file-node.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
const { slash } = require(`./utils`)
21
const path = require(`path`)
32
const fs = require(`fs-extra`)
43
const mime = require(`mime`)
54
const prettyBytes = require(`pretty-bytes`)
65

76
const md5File = require(`bluebird`).promisify(require(`md5-file`))
8-
const { createContentDigest } = require(`gatsby-core-utils`)
7+
const { createContentDigest, slash } = require(`gatsby-core-utils`)
98

109
exports.createFileNode = async (
1110
pathToFile,

packages/gatsby-source-filesystem/src/create-file-path.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require(`path`)
2-
const { slash } = require(`./utils`)
2+
const { slash } = require(`gatsby-core-utils`)
33

44
function findFileNode({ node, getNode }) {
55
// Find the file node.

packages/gatsby-source-filesystem/src/utils.js

-19
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,6 @@ export function createProgress(message, reporter) {
6868
}
6969
}
7070

71-
/**
72-
* slash
73-
* --
74-
* Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
75-
*
76-
*
77-
* @param {String} path
78-
* @return {String} slashed path
79-
*/
80-
export function slash(path) {
81-
const isExtendedLengthPath = /^\\\\\?\\/.test(path)
82-
83-
if (isExtendedLengthPath) {
84-
return path
85-
}
86-
87-
return path.replace(/\\/g, `/`)
88-
}
89-
9071
/**
9172
* createFilePath
9273
* --

packages/gatsby/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"shallow-compare": "^1.2.2",
122122
"sift": "^5.1.0",
123123
"signal-exit": "^3.0.2",
124-
"slash": "^3.0.0",
124+
"slugify": "^1.3.6",
125125
"socket.io": "^2.3.0",
126126
"stack-trace": "^0.0.10",
127127
"string-similarity": "^1.2.2",

packages/gatsby/src/bootstrap/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
const _ = require(`lodash`)
4-
const slash = require(`slash`)
4+
const { slash } = require(`gatsby-core-utils`)
55
const fs = require(`fs-extra`)
66
const md5File = require(`md5-file/promise`)
77
const crypto = require(`crypto`)

packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const loadPlugins = require(`../index`)
2-
const slash = require(`slash`)
2+
const { slash } = require(`gatsby-core-utils`)
33

44
describe(`Load plugins`, () => {
55
/**

packages/gatsby/src/bootstrap/load-plugins/load.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const _ = require(`lodash`)
2-
const slash = require(`slash`)
2+
const { slash } = require(`gatsby-core-utils`)
33
const fs = require(`fs`)
44
const path = require(`path`)
55
const crypto = require(`crypto`)

packages/gatsby/src/bootstrap/requires-writer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const _ = require(`lodash`)
22
const path = require(`path`)
33
const fs = require(`fs-extra`)
44
const crypto = require(`crypto`)
5-
const slash = require(`slash`)
5+
const { slash } = require(`gatsby-core-utils`)
66
const { store, emitter } = require(`../redux/`)
77
const reporter = require(`gatsby-cli/lib/reporter`)
88
const { match } = require(`@reach/router/lib/utils`)

packages/gatsby/src/commands/develop.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const withResolverContext = require(`../schema/context`)
3232
const sourceNodes = require(`../utils/source-nodes`)
3333
const websocketManager = require(`../utils/websocket-manager`)
3434
const getSslCert = require(`../utils/get-ssl-cert`)
35-
const slash = require(`slash`)
35+
const { slash } = require(`gatsby-core-utils`)
3636
const { initTracer } = require(`../utils/tracer`)
3737
const apiRunnerNode = require(`../utils/api-runner-node`)
3838
const db = require(`../db`)

0 commit comments

Comments
 (0)