diff --git a/changelog_unreleased/cli/pr-7588.md b/changelog_unreleased/cli/pr-7588.md new file mode 100644 index 000000000..9f33fd1c5 --- /dev/null +++ b/changelog_unreleased/cli/pr-7588.md @@ -0,0 +1,4 @@ +#### Respect `--ignore-path` when prettier executes from a subdirectory ([#7588](https://github.com/prettier/prettier/pull/7588) by [@heylookltsme](https://github.com/heylookltsme)) + +Changes the filename used when filtering ignored files to be relative to the +`--ignore-path`, if present, rather than the current working directory. diff --git a/src/cli/util.js b/src/cli/util.js index 173c321d2..465e67783 100644 --- a/src/cli/util.js +++ b/src/cli/util.js @@ -362,7 +362,11 @@ function formatStdin(context) { : process.cwd(); const ignorer = createIgnorerFromContextOrDie(context); - const relativeFilepath = path.relative(process.cwd(), filepath); + // If there's an ignore-path set, the filename must be relative to the + // ignore path, not the current working directory. + const relativeFilepath = context.argv["ignore-path"] + ? path.relative(path.dirname(context.argv["ignore-path"]), filepath) + : path.relative(process.cwd(), filepath); thirdParty .getStream(process.stdin) @@ -439,7 +443,12 @@ function formatFiles(context) { } eachFilename(context, context.filePatterns, filename => { - const fileIgnored = ignorer.filter([filename]).length === 0; + // If there's an ignore-path set, the filename must be relative to the + // ignore path, not the current working directory. + const ignoreFilename = context.argv["ignore-path"] + ? path.relative(path.dirname(context.argv["ignore-path"]), filename) + : filename; + const fileIgnored = ignorer.filter([ignoreFilename]).length === 0; if ( fileIgnored && (context.argv["debug-check"] || diff --git a/tests_integration/__tests__/__snapshots__/ignore-in-subdirectories.js.snap b/tests_integration/__tests__/__snapshots__/ignore-in-subdirectories.js.snap new file mode 100644 index 000000000..42ee78196 --- /dev/null +++ b/tests_integration/__tests__/__snapshots__/ignore-in-subdirectories.js.snap @@ -0,0 +1,39 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`formats files when executing in a subdirectory (stderr) 1`] = `""`; + +exports[`formats files when executing in a subdirectory (stderr) 2`] = `""`; + +exports[`formats files when executing in a subdirectory (stdout) 1`] = ` +"should-not-ignore.js +" +`; + +exports[`formats files when executing in a subdirectory (stdout) 2`] = ` +"should-not-ignore.js +" +`; + +exports[`formats files when executing in a subdirectory (write) 1`] = `Array []`; + +exports[`formats files when executing in a subdirectory (write) 2`] = `Array []`; + +exports[`formats files when executing in a subdirectory and using stdin (stderr) 1`] = `""`; + +exports[`formats files when executing in a subdirectory and using stdin (write) 1`] = `Array []`; + +exports[`ignore files when executing in a subdirectory and using stdin (stderr) 1`] = `""`; + +exports[`ignore files when executing in a subdirectory and using stdin (write) 1`] = `Array []`; + +exports[`ignores files when executing in a subdirectory (stderr) 1`] = `""`; + +exports[`ignores files when executing in a subdirectory (stderr) 2`] = `""`; + +exports[`ignores files when executing in a subdirectory (stdout) 1`] = `""`; + +exports[`ignores files when executing in a subdirectory (stdout) 2`] = `""`; + +exports[`ignores files when executing in a subdirectory (write) 1`] = `Array []`; + +exports[`ignores files when executing in a subdirectory (write) 2`] = `Array []`; diff --git a/tests_integration/__tests__/ignore-in-subdirectories.js b/tests_integration/__tests__/ignore-in-subdirectories.js new file mode 100644 index 000000000..2b5a4937c --- /dev/null +++ b/tests_integration/__tests__/ignore-in-subdirectories.js @@ -0,0 +1,77 @@ +"use strict"; + +const runPrettier = require("../runPrettier"); + +expect.addSnapshotSerializer(require("../path-serializer")); + +describe("ignores files when executing in a subdirectory", () => { + runPrettier("cli/ignore-in-subdirectories/web1", [ + "ignore-me/should-ignore.js", + "--ignore-path", + "../.prettierignore", + "-l" + ]).test({ + status: 0 + }); + + runPrettier("cli/ignore-in-subdirectories/web1", [ + "ignore-me/subdirectory/should-ignore.js", + "--ignore-path", + "../.prettierignore", + "-l" + ]).test({ + status: 0 + }); +}); + +describe("formats files when executing in a subdirectory", () => { + runPrettier("cli/ignore-in-subdirectories/web1", [ + "should-not-ignore.js", + "--ignore-path", + "../.prettierignore", + "-l" + ]).test({ + status: 1 + }); + + runPrettier("cli/ignore-in-subdirectories/web2", [ + "should-not-ignore.js", + "--ignore-path", + "../.prettierignore", + "-l" + ]).test({ + status: 1 + }); +}); + +describe("ignore files when executing in a subdirectory and using stdin", () => { + runPrettier( + "cli/ignore-in-subdirectories/web1", + [ + "--ignore-path", + "../.prettierignore", + "--stdin-filepath", + "ignore-me/example.js" + ], + { + input: "hello_world( );" + } + ).test({ + stdout: "hello_world( );", + status: 0 + }); +}); + +describe("formats files when executing in a subdirectory and using stdin", () => { + runPrettier( + "cli/ignore-in-subdirectories/web1", + ["--ignore-path", "../.prettierignore", "--stdin-filepath", "example.js"], + { + input: "hello_world( );" + } + ).test({ + stdout: `hello_world(); +`, + status: 0 + }); +}); diff --git a/tests_integration/cli/ignore-in-subdirectories/.prettierignore b/tests_integration/cli/ignore-in-subdirectories/.prettierignore new file mode 100644 index 000000000..eea9c1cdc --- /dev/null +++ b/tests_integration/cli/ignore-in-subdirectories/.prettierignore @@ -0,0 +1 @@ +web1/ignore-me diff --git a/tests_integration/cli/ignore-in-subdirectories/web1/ignore-me/should-ignore.js b/tests_integration/cli/ignore-in-subdirectories/web1/ignore-me/should-ignore.js new file mode 100644 index 000000000..3bef913c9 --- /dev/null +++ b/tests_integration/cli/ignore-in-subdirectories/web1/ignore-me/should-ignore.js @@ -0,0 +1 @@ +var x = 'this should not be formatted'; diff --git a/tests_integration/cli/ignore-in-subdirectories/web1/ignore-me/subdirectory/should-ignore.js b/tests_integration/cli/ignore-in-subdirectories/web1/ignore-me/subdirectory/should-ignore.js new file mode 100644 index 000000000..3bef913c9 --- /dev/null +++ b/tests_integration/cli/ignore-in-subdirectories/web1/ignore-me/subdirectory/should-ignore.js @@ -0,0 +1 @@ +var x = 'this should not be formatted'; diff --git a/tests_integration/cli/ignore-in-subdirectories/web1/should-not-ignore.js b/tests_integration/cli/ignore-in-subdirectories/web1/should-not-ignore.js new file mode 100644 index 000000000..0bcbead64 --- /dev/null +++ b/tests_integration/cli/ignore-in-subdirectories/web1/should-not-ignore.js @@ -0,0 +1 @@ +var x = 'this should be formatted'; diff --git a/tests_integration/cli/ignore-in-subdirectories/web2/should-not-ignore.js b/tests_integration/cli/ignore-in-subdirectories/web2/should-not-ignore.js new file mode 100644 index 000000000..0bcbead64 --- /dev/null +++ b/tests_integration/cli/ignore-in-subdirectories/web2/should-not-ignore.js @@ -0,0 +1 @@ +var x = 'this should be formatted';