Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,

const ignoreManualFilesNames = options.ignore ? options.ignorePath || [] : [];
const ignoreManualFilesPaths = ignoreManualFilesNames.map((fileName) => path.resolve(fileName));
const ignoreManualFilesContents = await Promise.all(ignoreManualFilesPaths.map((filePath) => fs.readFile(filePath, "utf8")));
const ignoreManualFilesContents = await Promise.all(ignoreManualFilesPaths.map(async (filePath) => {
try {
return await fs.readFile(filePath, "utf8");
} catch {
// Treat a missing file as if it was empty (no ignores)
return "";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im not a huge fan of creating these empty strings, but if we don't, ignoreManual will end up empty and we'll apply default gitignore etc

prettier doesn't do that, it treats a non-existent file as if it existed (i.e. it turns off the defaults)

Copy link
Collaborator

@fabiospampinato fabiospampinato Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably the behavior in v4 is better, like you point the CLI to something that doesn't exist and we should just silently fail? 🤔 Pretty weird, but ok we can align with that.


I'll change it to be a one-liner in an amending commit though, as that kinda breaks how the code flows visually, which I hate.

}
}));
const ignoreManualFoldersPaths = ignoreManualFilesPaths.map(path.dirname);
const ignoreManual = getIgnoreBys(ignoreManualFoldersPaths, ignoreManualFilesContents.map(castArray));

Expand Down
1 change: 1 addition & 0 deletions test/__fixtures__/ignore-path/file-info-test/.customignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignored-by-customignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignored-by-prettierignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignored-by-customignore.js
1 change: 1 addition & 0 deletions test/__fixtures__/ignore-path/ignore-path-test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignored-by-prettierignore.js
60 changes: 60 additions & 0 deletions test/__tests__/ignore-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { runCli } from "../utils";
import fs from "node:fs/promises";

// `.js` files are ignored in `.gitignore`
const files = [
"ignored-by-customignore.js",
"ignored-by-gitignore.js",
"ignored-by-prettierignore.js",
].map(
(file) =>
new URL(`../__fixtures__/ignore-path/ignore-path-test/${file}`, import.meta.url),
);
const clean = () =>
Promise.all(files.map((file) => fs.rm(file, { force: true })));
const setup = () =>
Promise.all(files.map((file) => fs.writeFile(file, " a+ b")));

beforeAll(async () => {
await setup();
});
afterAll(clean);

const getUnformattedFiles = async (args) => {
const { stdout } = await runCli("ignore-path/ignore-path-test/", [
"**/*.js",
"-l",
...args,
]);
return stdout ? stdout.split("\n").sort() : [];
};

test("custom ignore path", async () => {
expect(await getUnformattedFiles(["--ignore-path", ".customignore"])).toEqual(
["ignored-by-gitignore.js", "ignored-by-prettierignore.js"],
);
});

test("ignore files by .prettierignore and .gitignore by default", async () => {
expect(
await getUnformattedFiles(["--ignore-path", ".non-exists-ignore-file"]),
).toEqual([
"ignored-by-customignore.js",
"ignored-by-gitignore.js",
"ignored-by-prettierignore.js",
]);
expect(await getUnformattedFiles([])).toEqual([]);
});

test("multiple `--ignore-path`", async () => {
expect(
await getUnformattedFiles([
"--ignore-path",
".customignore",
"--ignore-path",
".prettierignore",
"--ignore-path",
".non-exists-ignore-file",
]),
).toEqual(["ignored-by-gitignore.js"]);
});