Fix cache not invalidated when files are ignored or restored (#18029)

This commit is contained in:
Konv Suu authored and GitHub committed 2026-06-27 17:45:39 +08:00
1 parent ae594b849f
commit fe040aeef9
2 files changed
+54 -1

No files matched your search

+4
View File
@@ -321,6 +321,10 @@ async function formatFiles(context) {
context.argv.check ||
context.argv.listDifferent)
) {
// If the file is ignored remove it from the cache
// - it won't be formatted anymore
// - if it has been restored, we shouldn't keep the old cache entry
formatResultsCache?.removeFormatResultsCache(filename);
continue;
}
+50 -1
View File
@@ -19,6 +19,7 @@ describe("--cache option", () => {
const nonDefaultCacheFileName = ".non-default-cache-file";
const directoryNameAsCacheFile = "directory-as-cache-file";
const nonDefaultCacheFilePath = path.join(dir, nonDefaultCacheFileName);
const prettierIgnorePath = path.join(dir, ".prettierignore");
const contentA = `function a() {
console.log("this is a.js")
@@ -374,6 +375,54 @@ describe("--cache option", () => {
await runCliWithoutGitignore(dir, ["--write", "*.js"]);
await expect(fs.stat(defaultCacheFile)).rejects.toThrow();
});
it("invalidates cache when previously ignored files are restored", async () => {
await runCliWithoutGitignore(dir, [
"--write",
"--cache",
"--cache-strategy",
"metadata",
"*.js",
]);
await fs.writeFile(prettierIgnorePath, ["b.js"].join("\n"));
await fs.writeFile(
path.join(dir, "b.js"),
"const b = 'this is new content';",
);
const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [
"--write",
"--cache",
"--cache-strategy",
"metadata",
"*.js",
]);
expect(secondStdout.split("\n")).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(unchanged\) \(cached\)$/u),
]),
);
await fs.rm(prettierIgnorePath, { force: true });
const { stdout: thirdStdout } = await runCliWithoutGitignore(dir, [
"--write",
"--cache",
"--cache-strategy",
"metadata",
"*.js",
]);
expect(thirdStdout.split("\n")).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(unchanged\) \(cached\)$/u),
expect.stringMatching(/^b\.js .+ms$/u),
]),
);
});
});
describe("--cache-strategy content", () => {
@@ -701,7 +750,7 @@ describe("--cache option", () => {
await expect(fs.stat(nonDefaultCacheFilePath)).resolves.not.toThrow();
});
it("does'nt format when cache is available", async () => {
it("doesn't format when cache is available", async () => {
const cliArguments = [
"--cache",
"--write",