Skip to content

Commit ff4a4da

Browse files
t3dotggclaude
andcommitted
fix(scripts): a broken check:changed cache counts as a miss
A cache file with invalid JSON made every later run throw before any check ran. Reading the cache now treats any error as a miss, and the write goes to a temporary file that is renamed into place. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 201c4f1 commit ff4a4da

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

‎scripts/check-changed.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,18 @@ const inputKey = () => {
8989
};
9090
const key = inputKey();
9191
const cachePath = NodePath.join(root, "node_modules/.cache/check-changed.json");
92-
const cached = NodeFS.existsSync(cachePath)
93-
? (JSON.parse(NodeFS.readFileSync(cachePath, "utf8")) as { key: string; summary: Array<string> })
94-
: undefined;
92+
// A missing or unreadable cache is a miss, never a failure.
93+
const readCache = () => {
94+
try {
95+
return JSON.parse(NodeFS.readFileSync(cachePath, "utf8")) as {
96+
key: string;
97+
summary: Array<string>;
98+
};
99+
} catch {
100+
return undefined;
101+
}
102+
};
103+
const cached = readCache();
95104
if (cached?.key === key) {
96105
console.log("Nothing changed since the last passing run.\n");
97106
for (const line of cached.summary) console.log(line);
@@ -220,8 +229,11 @@ if (results.some((result) => !result.ok)) {
220229
console.log(" pass not cached: files changed during the run");
221230
} else {
222231
try {
232+
// Write then rename, so a concurrent run never reads half a file.
233+
const temp = `${cachePath}.${process.pid}`;
223234
NodeFS.mkdirSync(NodePath.dirname(cachePath), { recursive: true });
224-
NodeFS.writeFileSync(cachePath, JSON.stringify({ key, summary }));
235+
NodeFS.writeFileSync(temp, JSON.stringify({ key, summary }));
236+
NodeFS.renameSync(temp, cachePath);
225237
} catch (error) {
226238
console.warn(` pass not cached: ${error instanceof Error ? error.message : String(error)}`);
227239
}

0 commit comments

Comments
 (0)