From 85f7b2c9518bb52e8d47074a3d36f16f736e68a6 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 1 Jun 2021 09:15:36 +0800 Subject: [PATCH 1/6] CLI: Fix failure on dir with trailing slash --- src/cli/expand-patterns.js | 3 +- .../__snapshots__/patterns-dirs.js.snap | 101 ++++++++++++++++++ tests/integration/__tests__/patterns-dirs.js | 41 ++++++- 3 files changed, 142 insertions(+), 3 deletions(-) diff --git a/src/cli/expand-patterns.js b/src/cli/expand-patterns.js index 8e2e4d7297f3..682ec170653d 100644 --- a/src/cli/expand-patterns.js +++ b/src/cli/expand-patterns.js @@ -76,10 +76,11 @@ async function* expandPatternsInternal(context) { input: pattern, }); } else if (stat.isDirectory()) { + const relativePath = path.relative(cwd, absolutePath) || "."; entries.push({ type: "dir", glob: - escapePathForGlob(fixWindowsSlashes(pattern)) + + escapePathForGlob(fixWindowsSlashes(relativePath)) + "/" + getSupportedFilesGlob(), input: pattern, diff --git a/tests/integration/__tests__/__snapshots__/patterns-dirs.js.snap b/tests/integration/__tests__/__snapshots__/patterns-dirs.js.snap index 1ef92eb65958..fa293776b88a 100644 --- a/tests/integration/__tests__/__snapshots__/patterns-dirs.js.snap +++ b/tests/integration/__tests__/__snapshots__/patterns-dirs.js.snap @@ -149,6 +149,107 @@ exports[`Negative patterns with explicit files: prettier dir1/a1.js dir2/a2.js ' " `; +exports[`Trailing slash 1: prettier ./ (stdout) 1`] = ` +"!dir/a.js +dir1/a1.js +dir1/b1.js +dir1/nested1/an1.css +dir1/nested1/an1.js +dir1/nested1/bn1.js +dir2/a2.js +dir2/b2.js +dir2/nested2/an2.js +dir2/nested2/bn2.js +" +`; + +exports[`Trailing slash 2: prettier .// (stdout) 1`] = ` +"!dir/a.js +dir1/a1.js +dir1/b1.js +dir1/nested1/an1.css +dir1/nested1/an1.js +dir1/nested1/bn1.js +dir2/a2.js +dir2/b2.js +dir2/nested2/an2.js +dir2/nested2/bn2.js +" +`; + +exports[`Trailing slash 3: prettier dir1/ (stdout) 1`] = ` +"dir1/a1.js +dir1/b1.js +dir1/nested1/an1.css +dir1/nested1/an1.js +dir1/nested1/bn1.js +" +`; + +exports[`Trailing slash 4: prettier dir1// (stdout) 1`] = ` +"dir1/a1.js +dir1/b1.js +dir1/nested1/an1.css +dir1/nested1/an1.js +dir1/nested1/bn1.js +" +`; + +exports[`Trailing slash 5: prettier ./dir2/../dir1// (stdout) 1`] = ` +"dir1/a1.js +dir1/b1.js +dir1/nested1/an1.css +dir1/nested1/an1.js +dir1/nested1/bn1.js +" +`; + +exports[`Trailing slash run in sub dir 1: prettier .. (stdout) 1`] = ` +"../!dir/a.js +../dir1/a1.js +../dir1/b1.js +../dir1/nested1/an1.css +../dir1/nested1/an1.js +../dir1/nested1/bn1.js +a2.js +b2.js +nested2/an2.js +nested2/bn2.js +" +`; + +exports[`Trailing slash run in sub dir 2: prettier ../ (stdout) 1`] = ` +"../!dir/a.js +../dir1/a1.js +../dir1/b1.js +../dir1/nested1/an1.css +../dir1/nested1/an1.js +../dir1/nested1/bn1.js +a2.js +b2.js +nested2/an2.js +nested2/bn2.js +" +`; + +exports[`Trailing slash run in sub dir 3: prettier ../dir1 (stdout) 1`] = ` +"../dir1/a1.js +../dir1/b1.js +../dir1/nested1/an1.css +../dir1/nested1/an1.js +../dir1/nested1/bn1.js +" +`; + +exports[`Trailing slash run in sub dir 4: prettier ../dir1/ (stdout) 1`] = ` +"../dir1/a1.js +../dir1/b1.js +../dir1/nested1/an1.css +../dir1/nested1/an1.js +../dir1/nested1/bn1.js +" +`; + exports[`plugins \`*\` (stderr) 1`] = ` "[error] No parser could be inferred for file: unknown.unknown " diff --git a/tests/integration/__tests__/patterns-dirs.js b/tests/integration/__tests__/patterns-dirs.js index 29806ee081bc..d9d26e4b7838 100644 --- a/tests/integration/__tests__/patterns-dirs.js +++ b/tests/integration/__tests__/patterns-dirs.js @@ -44,6 +44,38 @@ testPatterns("3", ["nonexistent-dir", "dir2/**/*"], { status: 2 }); testPatterns("4", [".", "dir2/**/*"], { status: 1 }); +describe("Trailing slash", () => { + testPatterns("1", ["./"], { status: 1, stderr: "" }); + testPatterns("2", [".//"], { status: 1, stderr: "" }); + testPatterns("3", ["dir1/"], { status: 1, stderr: "" }); + testPatterns("4", ["dir1//"], { status: 1, stderr: "" }); + testPatterns("5", ["./dir2/../dir1//"], { status: 1, stderr: "" }); + testPatterns( + "run in sub dir 1", + [".."], + { status: 1, stderr: "" }, + "cli/patterns-dirs/dir2" + ); + testPatterns( + "run in sub dir 2", + ["../"], + { status: 1, stderr: "" }, + "cli/patterns-dirs/dir2" + ); + testPatterns( + "run in sub dir 3", + ["../dir1"], + { status: 1, stderr: "" }, + "cli/patterns-dirs/dir2" + ); + testPatterns( + "run in sub dir 4", + ["../dir1/"], + { status: 1, stderr: "" }, + "cli/patterns-dirs/dir2" + ); +}); + describe("Negative patterns", () => { testPatterns("1", ["dir1", "!dir1/nested1"]); testPatterns("1a", ["dir1", "!dir1/nested1/*"]); @@ -119,7 +151,12 @@ if (path.sep === "/") { }); } -function testPatterns(namePrefix, cliArgs, expected = {}) { +function testPatterns( + namePrefix, + cliArgs, + expected = {}, + cwd = "cli/patterns-dirs" +) { const testName = (namePrefix ? namePrefix + ": " : "") + "prettier " + @@ -128,7 +165,7 @@ function testPatterns(namePrefix, cliArgs, expected = {}) { .join(" "); describe(testName, () => { - runPrettier("cli/patterns-dirs", [...cliArgs, "-l"]).test({ + runPrettier(cwd, [...cliArgs, "-l"]).test({ write: [], ...(!("status" in expected) && { stderr: "", status: 1 }), ...expected, From b37bdb73c28ceabb76fdd4c7e2aa93344312983b Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 1 Jun 2021 09:51:49 +0800 Subject: [PATCH 2/6] Changelog --- changelog_unreleased/cli/11000.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 changelog_unreleased/cli/11000.md diff --git a/changelog_unreleased/cli/11000.md b/changelog_unreleased/cli/11000.md new file mode 100644 index 000000000000..64e1d3d58f69 --- /dev/null +++ b/changelog_unreleased/cli/11000.md @@ -0,0 +1,17 @@ +#### Fix failure on dir with trailing slash (#11000 by @fisker) + + +```console +$ ls +1.js 1.unknown + +# Prettier stable +$ prettier . -l +1.js +$ prettier ./ -l +[error] No supported files were found in the directory: "./". + +# Prettier main +$ prettier ./ -l +1.js +``` From d02f44ec4e1f14a96a2f72115b921369920161d5 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 1 Jun 2021 10:05:48 +0800 Subject: [PATCH 3/6] More slashes --- tests/integration/__tests__/__snapshots__/patterns-dirs.js.snap | 2 +- tests/integration/__tests__/patterns-dirs.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/__tests__/__snapshots__/patterns-dirs.js.snap b/tests/integration/__tests__/__snapshots__/patterns-dirs.js.snap index fa293776b88a..8be8bd58f651 100644 --- a/tests/integration/__tests__/__snapshots__/patterns-dirs.js.snap +++ b/tests/integration/__tests__/__snapshots__/patterns-dirs.js.snap @@ -195,7 +195,7 @@ dir1/nested1/bn1.js " `; -exports[`Trailing slash 5: prettier ./dir2/../dir1// (stdout) 1`] = ` +exports[`Trailing slash 5: prettier .//dir2/..//./dir1// (stdout) 1`] = ` "dir1/a1.js dir1/b1.js dir1/nested1/an1.css diff --git a/tests/integration/__tests__/patterns-dirs.js b/tests/integration/__tests__/patterns-dirs.js index d9d26e4b7838..5f4666ca9b03 100644 --- a/tests/integration/__tests__/patterns-dirs.js +++ b/tests/integration/__tests__/patterns-dirs.js @@ -49,7 +49,7 @@ describe("Trailing slash", () => { testPatterns("2", [".//"], { status: 1, stderr: "" }); testPatterns("3", ["dir1/"], { status: 1, stderr: "" }); testPatterns("4", ["dir1//"], { status: 1, stderr: "" }); - testPatterns("5", ["./dir2/../dir1//"], { status: 1, stderr: "" }); + testPatterns("5", [".//dir2/..//./dir1//"], { status: 1, stderr: "" }); testPatterns( "run in sub dir 1", [".."], From 3a10600671d32aa340f1721d6dd9b8ee4f578fdd Mon Sep 17 00:00:00 2001 From: fisker Date: Fri, 4 Jun 2021 22:13:41 +0800 Subject: [PATCH 4/6] Only remove trailing slashes --- src/cli/expand-patterns.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cli/expand-patterns.js b/src/cli/expand-patterns.js index 682ec170653d..89bb244d1602 100644 --- a/src/cli/expand-patterns.js +++ b/src/cli/expand-patterns.js @@ -76,11 +76,12 @@ async function* expandPatternsInternal(context) { input: pattern, }); } else if (stat.isDirectory()) { - const relativePath = path.relative(cwd, absolutePath) || "."; entries.push({ type: "dir", glob: - escapePathForGlob(fixWindowsSlashes(relativePath)) + + escapePathForGlob( + fixWindowsSlashes(pattern.replace(/[/\\]+$/, "")) + ) + "/" + getSupportedFilesGlob(), input: pattern, From da461964378c29df112f67d7f44a52f1147e8955 Mon Sep 17 00:00:00 2001 From: fisker Date: Fri, 4 Jun 2021 22:16:07 +0800 Subject: [PATCH 5/6] Only remove `/`, after `\` has been replaced --- src/cli/expand-patterns.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/cli/expand-patterns.js b/src/cli/expand-patterns.js index 89bb244d1602..70e4d4886ba5 100644 --- a/src/cli/expand-patterns.js +++ b/src/cli/expand-patterns.js @@ -79,9 +79,7 @@ async function* expandPatternsInternal(context) { entries.push({ type: "dir", glob: - escapePathForGlob( - fixWindowsSlashes(pattern.replace(/[/\\]+$/, "")) - ) + + escapePathForGlob(fixWindowsSlashes(pattern).replace(/\/+$/, "")) + "/" + getSupportedFilesGlob(), input: pattern, From b00987886d90edaf9d34b3e45b56fd56ab659ebd Mon Sep 17 00:00:00 2001 From: fisker Date: Fri, 4 Jun 2021 22:31:04 +0800 Subject: [PATCH 6/6] Restore to use `` --- src/cli/expand-patterns.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cli/expand-patterns.js b/src/cli/expand-patterns.js index 70e4d4886ba5..5fb087a344ee 100644 --- a/src/cli/expand-patterns.js +++ b/src/cli/expand-patterns.js @@ -76,10 +76,16 @@ async function* expandPatternsInternal(context) { input: pattern, }); } else if (stat.isDirectory()) { + /* + 1. Remove trailing `/`, `fast-glob` can't find files for `src//*.js` pattern + 2. Cleanup dirname, when glob `src/../*.js` pattern with `fast-glob`, + it returns files like 'src/../index.js' + */ + const relativePath = path.relative(cwd, absolutePath) || "."; entries.push({ type: "dir", glob: - escapePathForGlob(fixWindowsSlashes(pattern).replace(/\/+$/, "")) + + escapePathForGlob(fixWindowsSlashes(relativePath)) + "/" + getSupportedFilesGlob(), input: pattern,