Skip to content

Commit 61c0129

Browse files
authored
Invalidate outdated cache file (#17100)
1 parent 9cf8ec2 commit 61c0129

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

changelog_unreleased/cli/17100.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#### Fix CLI crash when cache for old version exists (#17100 by @sosukesuzuki)
2+
3+
Prettier 3.5 uses a different cache format than previous versions, Prettier stable crashes when reading existing cache file, Prettier main fixed the problem.

src/cli/format-results-cache.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Inspired by LintResultsCache from ESLint
22
// https://github.com/eslint/eslint/blob/c2d0a830754b6099a3325e6d3348c3ba983a677a/lib/cli-engine/lint-result-cache.js
33

4+
import fs from "node:fs";
45
import stringify from "fast-json-stable-stringify";
56
import fileEntryCache from "file-entry-cache";
67
import { version as prettierVersion } from "../index.js";
@@ -44,10 +45,24 @@ class FormatResultsCache {
4445
constructor(cacheFileLocation, cacheStrategy) {
4546
const useChecksum = cacheStrategy === "content";
4647

47-
this.#fileEntryCache = fileEntryCache.createFromFile(
48-
/* filePath */ cacheFileLocation,
49-
useChecksum,
50-
);
48+
try {
49+
this.#fileEntryCache = fileEntryCache.createFromFile(
50+
/* filePath */ cacheFileLocation,
51+
useChecksum,
52+
);
53+
} catch {
54+
// https://github.com/prettier/prettier/issues/17092
55+
// If `createFromFile()` fails, it's probably because the format
56+
// of cache file changed,it happened when we release v3.5.0
57+
if (fs.existsSync(cacheFileLocation)) {
58+
fs.unlinkSync(cacheFileLocation);
59+
// retry
60+
this.#fileEntryCache = fileEntryCache.createFromFile(
61+
/* filePath */ cacheFileLocation,
62+
useChecksum,
63+
);
64+
}
65+
}
5166
}
5267

5368
/**

0 commit comments

Comments
 (0)