Skip to content

Commit

Permalink
fix(gatsby): Handle special characters in windows paths (#19600)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladar authored and sidharthachatterjee committed Nov 18, 2019
1 parent 81bfb1b commit 9929cf0
Show file tree
Hide file tree
Showing 34 changed files with 238 additions and 72 deletions.
22 changes: 21 additions & 1 deletion packages/gatsby-core-utils/src/__tests__/path.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { joinPath, isNodeInternalModulePath } = require(`../path`)
const { joinPath, isNodeInternalModulePath, slash } = require(`../path`)
const os = require(`os`)

describe(`paths`, () => {
Expand Down Expand Up @@ -30,4 +30,24 @@ describe(`paths`, () => {
expect(isNodeInternalModulePath(modulePath)).toBe(false)
})
})

describe(`slash path`, () => {
it(`can correctly slash path`, () => {
;[
[`foo\\bar`, `foo/bar`],
[`foo/bar`, `foo/bar`],
[`foo\\中文`, `foo/中文`],
[`foo/中文`, `foo/中文`],
[`foo/жä`, `foo/жä`],
[`foo\\жä`, `foo/жä`],
].forEach(([path, expectRes]) => {
expect(slash(path)).toBe(expectRes)
})
})

it(`does not modify extended length paths`, () => {
const extended = `\\\\?\\some\\path`
expect(slash(extended)).toBe(extended)
})
})
})
1 change: 1 addition & 0 deletions packages/gatsby-core-utils/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
exports.createContentDigest = require(`./create-content-digest`)
exports.joinPath = require(`./path`).joinPath
exports.isNodeInternalModulePath = require(`./path`).isNodeInternalModulePath
exports.slash = require(`./path`).slash
exports.cpuCoreCount = require(`./cpu-core-count`)
exports.urlResolve = require(`./url`).resolve
exports.isCI = require(`./ci`).isCI
Expand Down
19 changes: 19 additions & 0 deletions packages/gatsby-core-utils/src/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,22 @@ const nodePaths = [
*/
export const isNodeInternalModulePath = fileName =>
nodePaths.some(regTest => regTest.test(fileName))

/**
* slash
* --
* Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
*
*
* @param {String} path
* @return {String} slashed path
*/
export function slash(path) {
const isExtendedLengthPath = /^\\\\\?\\/.test(path)

if (isExtendedLengthPath) {
return path
}

return path.replace(/\\/g, `/`)
}
4 changes: 2 additions & 2 deletions packages/gatsby-page-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
"bluebird": "^3.7.1",
"chokidar": "3.3.0",
"fs-exists-cached": "^1.0.0",
"gatsby-core-utils": "^1.0.19",
"glob": "^7.1.6",
"lodash": "^4.17.15",
"micromatch": "^3.1.10",
"slash": "^3.0.0"
"micromatch": "^3.1.10"
},
"devDependencies": {
"@babel/cli": "^7.7.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-page-utils/src/watch-directory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Promise = require(`bluebird`)
const chokidar = require(`chokidar`)
const slash = require(`slash`)
const { slash } = require(`gatsby-core-utils`)

module.exports = async (path, glob, onNewFile, onRemovedFile) =>
new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-mdx/loaders/mdx-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const genMdx = require(`../utils/gen-mdx`)
const withDefaultOptions = require(`../utils/default-options`)
const createMDXNode = require(`../utils/create-mdx-node`)
const { createFileNode } = require(`../utils/create-fake-file-node`)
const slash = require(`slash`)
const { slash } = require(`gatsby-core-utils`)

const DEFAULT_OPTIONS = {
footnotes: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-mdx/loaders/mdx-scopes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require(`fs`)
const path = require(`path`)
const slash = require(`slash`)
const { slash } = require(`gatsby-core-utils`)
const loaderUtils = require(`loader-utils`)
const { MDX_SCOPES_LOCATION } = require(`../constants`)

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"escape-string-regexp": "^1.0.5",
"eval": "^0.1.4",
"fs-extra": "^8.1.0",
"gatsby-core-utils": "^1.0.19",
"gray-matter": "^4.0.2",
"json5": "^2.1.1",
"loader-utils": "^1.2.3",
Expand All @@ -45,7 +46,6 @@
"remark": "^10.0.1",
"remark-retext": "^3.1.3",
"retext-english": "^3.0.4",
"slash": "^3.0.0",
"static-site-generator-webpack-plugin": "^3.4.2",
"style-to-object": "^0.3.0",
"underscore.string": "^3.3.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-mdx/utils/create-fake-file-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const slash = require(`slash`)
const { slash } = require(`gatsby-core-utils`)
const path = require(`path`)
const fs = require(`fs`)
const mime = require(`mime`)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-offline/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"dependencies": {
"@babel/runtime": "^7.7.2",
"cheerio": "^1.0.0-rc.3",
"gatsby-core-utils": "^1.0.19",
"glob": "^7.1.6",
"idb-keyval": "^3.2.0",
"lodash": "^4.17.15",
"slash": "^3.0.0",
"workbox-build": "^4.3.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-offline/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
let fs = require(`fs`)
let workboxBuild = require(`workbox-build`)
const path = require(`path`)
const slash = require(`slash`)
const { slash } = require(`gatsby-core-utils`)
const glob = require(`glob`)
const _ = require(`lodash`)

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-remark-images/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"@babel/runtime": "^7.7.2",
"chalk": "^2.4.2",
"cheerio": "^1.0.0-rc.3",
"gatsby-core-utils": "^1.0.19",
"is-relative-url": "^3.0.0",
"lodash": "^4.17.15",
"mdast-util-definitions": "^1.2.5",
"potrace": "^2.1.2",
"query-string": "^6.8.3",
"slash": "^3.0.0",
"unist-util-select": "^1.5.0",
"unist-util-visit-parents": "^2.1.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-remark-images/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const _ = require(`lodash`)
const { fluid, stats, traceSVG } = require(`gatsby-plugin-sharp`)
const Promise = require(`bluebird`)
const cheerio = require(`cheerio`)
const slash = require(`slash`)
const { slash } = require(`gatsby-core-utils`)
const chalk = require(`chalk`)

// If the image is relative (not hosted elsewhere)
Expand Down
19 changes: 0 additions & 19 deletions packages/gatsby-source-filesystem/src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const {
getRemoteFileExtension,
getRemoteFileName,
createProgress,
slash,
} = require(`../utils`)
const reporter = require(`gatsby-cli/lib/reporter`)
const progress = require(`progress`)
Expand Down Expand Up @@ -62,21 +61,3 @@ describe(`createProgress`, () => {
expect(bar).toHaveProperty(`total`)
})
})

describe(`slash path`, () => {
it(`can correctly slash path`, () => {
;[
[`foo\\bar`, `foo/bar`],
[`foo/bar`, `foo/bar`],
[`foo\\中文`, `foo/中文`],
[`foo/中文`, `foo/中文`],
].forEach(([path, expectRes]) => {
expect(slash(path)).toBe(expectRes)
})
})

it(`does not modify extended length paths`, () => {
const extended = `\\\\?\\some\\path`
expect(slash(extended)).toBe(extended)
})
})
3 changes: 1 addition & 2 deletions packages/gatsby-source-filesystem/src/create-file-node.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const { slash } = require(`./utils`)
const path = require(`path`)
const fs = require(`fs-extra`)
const mime = require(`mime`)
const prettyBytes = require(`pretty-bytes`)

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

exports.createFileNode = async (
pathToFile,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-filesystem/src/create-file-path.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require(`path`)
const { slash } = require(`./utils`)
const { slash } = require(`gatsby-core-utils`)

function findFileNode({ node, getNode }) {
// Find the file node.
Expand Down
19 changes: 0 additions & 19 deletions packages/gatsby-source-filesystem/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,6 @@ export function createProgress(message, reporter) {
}
}

/**
* slash
* --
* Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
*
*
* @param {String} path
* @return {String} slashed path
*/
export function slash(path) {
const isExtendedLengthPath = /^\\\\\?\\/.test(path)

if (isExtendedLengthPath) {
return path
}

return path.replace(/\\/g, `/`)
}

/**
* createFilePath
* --
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"shallow-compare": "^1.2.2",
"sift": "^5.1.0",
"signal-exit": "^3.0.2",
"slash": "^3.0.0",
"slugify": "^1.3.6",
"socket.io": "^2.3.0",
"stack-trace": "^0.0.10",
"string-similarity": "^1.2.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

const _ = require(`lodash`)
const slash = require(`slash`)
const { slash } = require(`gatsby-core-utils`)
const fs = require(`fs-extra`)
const md5File = require(`md5-file/promise`)
const crypto = require(`crypto`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const loadPlugins = require(`../index`)
const slash = require(`slash`)
const { slash } = require(`gatsby-core-utils`)

describe(`Load plugins`, () => {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/bootstrap/load-plugins/load.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const _ = require(`lodash`)
const slash = require(`slash`)
const { slash } = require(`gatsby-core-utils`)
const fs = require(`fs`)
const path = require(`path`)
const crypto = require(`crypto`)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/bootstrap/requires-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const _ = require(`lodash`)
const path = require(`path`)
const fs = require(`fs-extra`)
const crypto = require(`crypto`)
const slash = require(`slash`)
const { slash } = require(`gatsby-core-utils`)
const { store, emitter } = require(`../redux/`)
const reporter = require(`gatsby-cli/lib/reporter`)
const { match } = require(`@reach/router/lib/utils`)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/commands/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const withResolverContext = require(`../schema/context`)
const sourceNodes = require(`../utils/source-nodes`)
const websocketManager = require(`../utils/websocket-manager`)
const getSslCert = require(`../utils/get-ssl-cert`)
const slash = require(`slash`)
const { slash } = require(`gatsby-core-utils`)
const { initTracer } = require(`../utils/tracer`)
const apiRunnerNode = require(`../utils/api-runner-node`)
const db = require(`../db`)
Expand Down
Loading

0 comments on commit 9929cf0

Please sign in to comment.