From 336cf15242b7eb8b22edd54de55f1e08a10fc2f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=B8rch?= Date: Wed, 14 Jul 2021 14:49:16 +0200 Subject: [PATCH] Fix "No files matching the pattern" when using backslash paths on Windows (#5386) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Rewrite paths for globby to use forward slash * Remove stray comment * Extend tests to verify 'windows paths' * Use normalize-path for path normalization instead of regex * Amend tests as suggested in PR comments * Updated test description Co-authored-by: Thomas Bowman Mørch --- lib/__tests__/standalone-globs.test.js | 29 ++++++++++++++++++++++++++ lib/standalone.js | 3 ++- package-lock.json | 23 ++++++++++++++++---- package.json | 2 ++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/lib/__tests__/standalone-globs.test.js b/lib/__tests__/standalone-globs.test.js index 0d9d2c8255..e5fc550af6 100644 --- a/lib/__tests__/standalone-globs.test.js +++ b/lib/__tests__/standalone-globs.test.js @@ -112,6 +112,35 @@ describe('standalone globbing', () => { ); }); + describe('windows path style with backslash', () => { + // The tests fail on non-windows environments. + if (process.platform !== 'win32') { + return; + } + + it('should handle a backslash-separated file path', async () => { + const cssGlob = path.win32.join(fixturesPath, 'with spaces', 'styles.css'); + + const { results } = await standalone({ + files: cssGlob, + config: { + rules: { + 'block-no-empty': true, + }, + }, + }); + + expect(results).toHaveLength(1); + expect(results[0].errored).toEqual(true); + expect(results[0].warnings[0]).toEqual( + expect.objectContaining({ + rule: 'block-no-empty', + severity: 'error', + }), + ); + }); + }); + describe('mixed globs and paths with special chars', () => { it('manual escaping', async () => { const cssGlob = `${fixturesPath}/got\\[braces\\] and \\(spaces\\)/*.+(s|c)ss`; diff --git a/lib/standalone.js b/lib/standalone.js index f075bb2c6d..cd5b210a85 100644 --- a/lib/standalone.js +++ b/lib/standalone.js @@ -12,6 +12,7 @@ const getFormatterOptionsText = require('./utils/getFormatterOptionsText'); const globby = require('globby'); const hash = require('./utils/hash'); const NoFilesFoundError = require('./utils/noFilesFoundError'); +const normalizePath = require('normalize-path'); const path = require('path'); const pkg = require('../package.json'); const prepareReturnValue = require('./prepareReturnValue'); @@ -176,7 +177,7 @@ module.exports = function (options) { if (fs.existsSync(absolutePath)) { // This path points to a file. Return an escaped path to avoid globbing - return fastGlob.escapePath(entry); + return fastGlob.escapePath(normalizePath(entry)); } return entry; diff --git a/package-lock.json b/package-lock.json index 94dda4ea65..5d9190e5bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,6 +31,7 @@ "mathml-tag-names": "^2.1.3", "meow": "^9.0.0", "micromatch": "^4.0.4", + "normalize-path": "^3.0.0", "normalize-selector": "^0.2.0", "postcss": "^8.3.0", "postcss-media-query-parser": "^0.2.3", @@ -62,6 +63,7 @@ "@types/globjoin": "^0.1.0", "@types/imurmurhash": "^0.1.1", "@types/micromatch": "^4.0.1", + "@types/normalize-path": "^3.0.0", "@types/postcss-less": "^4.0.0", "@types/postcss-safe-parser": "^5.0.0", "@types/style-search": "^0.1.1", @@ -1235,6 +1237,12 @@ "resolved": "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz", "integrity": "sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==" }, + "node_modules/@types/normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@types/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-Nd8y/5t/7CRakPYiyPzr/IAfYusy1FkcZYFEAcoMZkwpJv2n4Wm+olW+e7xBdHEXhOnWdG9ddbar0gqZWS4x5Q==", + "dev": true + }, "node_modules/@types/parse-json": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz", @@ -2151,6 +2159,7 @@ "dependencies": { "anymatch": "~3.1.1", "braces": "~3.0.2", + "fsevents": "~2.1.2", "glob-parent": "~5.1.0", "is-binary-path": "~2.1.0", "is-glob": "~4.0.1", @@ -3004,7 +3013,8 @@ "esprima": "^4.0.1", "estraverse": "^5.2.0", "esutils": "^2.0.2", - "optionator": "^0.8.1" + "optionator": "^0.8.1", + "source-map": "~0.6.1" }, "bin": { "escodegen": "bin/escodegen.js", @@ -5226,6 +5236,7 @@ "@types/node": "*", "anymatch": "^3.0.3", "fb-watchman": "^2.0.0", + "fsevents": "^2.3.2", "graceful-fs": "^4.2.4", "jest-regex-util": "^27.0.1", "jest-serializer": "^27.0.1", @@ -7091,7 +7102,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", - "dev": true, "engines": { "node": ">=0.10.0" } @@ -11735,6 +11745,12 @@ "resolved": "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz", "integrity": "sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==" }, + "@types/normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@types/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-Nd8y/5t/7CRakPYiyPzr/IAfYusy1FkcZYFEAcoMZkwpJv2n4Wm+olW+e7xBdHEXhOnWdG9ddbar0gqZWS4x5Q==", + "dev": true + }, "@types/parse-json": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz", @@ -16286,8 +16302,7 @@ "normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", - "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", - "dev": true + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==" }, "normalize-selector": { "version": "0.2.0", diff --git a/package.json b/package.json index 18c4bb0e05..6239b4591c 100644 --- a/package.json +++ b/package.json @@ -130,6 +130,7 @@ "mathml-tag-names": "^2.1.3", "meow": "^9.0.0", "micromatch": "^4.0.4", + "normalize-path": "^3.0.0", "normalize-selector": "^0.2.0", "postcss": "^8.3.0", "postcss-media-query-parser": "^0.2.3", @@ -158,6 +159,7 @@ "@types/globjoin": "^0.1.0", "@types/imurmurhash": "^0.1.1", "@types/micromatch": "^4.0.1", + "@types/normalize-path": "^3.0.0", "@types/postcss-less": "^4.0.0", "@types/postcss-safe-parser": "^5.0.0", "@types/style-search": "^0.1.1",