Invalidate outdated cache file (#17100)

This commit is contained in:
SUZUKI Sosuke authored and GitHub committed 2025-02-12 16:32:51 +09:00
1 parent 9cf8ec23f6
commit 61c0129ce5
2 files changed
+22 -4

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
#### Fix CLI crash when cache for old version exists (#17100 by @sosukesuzuki)
Prettier 3.5 uses a different cache format than previous versions, Prettier stable crashes when reading existing cache file, Prettier main fixed the problem.
+19 -4
View File
@@ -1,6 +1,7 @@
// Inspired by LintResultsCache from ESLint
// https://github.com/eslint/eslint/blob/c2d0a830754b6099a3325e6d3348c3ba983a677a/lib/cli-engine/lint-result-cache.js
import fs from "node:fs";
import stringify from "fast-json-stable-stringify";
import fileEntryCache from "file-entry-cache";
import { version as prettierVersion } from "../index.js";
@@ -44,10 +45,24 @@ class FormatResultsCache {
constructor(cacheFileLocation, cacheStrategy) {
const useChecksum = cacheStrategy === "content";
this.#fileEntryCache = fileEntryCache.createFromFile(
/* filePath */ cacheFileLocation,
useChecksum,
);
try {
this.#fileEntryCache = fileEntryCache.createFromFile(
/* filePath */ cacheFileLocation,
useChecksum,
);
} catch {
// https://github.com/prettier/prettier/issues/17092
// If `createFromFile()` fails, it's probably because the format
// of cache file changed,it happened when we release v3.5.0
if (fs.existsSync(cacheFileLocation)) {
fs.unlinkSync(cacheFileLocation);
// retry
this.#fileEntryCache = fileEntryCache.createFromFile(
/* filePath */ cacheFileLocation,
useChecksum,
);
}
}
}
/**