Skip to content

Commit 201c4f1

Browse files
t3dotggclaude
andcommitted
fix(scripts): check:changed caches a pass only for the tree it checked
- Compute the input key again after a passing run. If files changed during the run, do not cache the pass under the old key. - A cache write that fails prints a warning. It no longer fails a passing run. - knip-schemas stops the other workers when one fails, so a failed knip run does not wait for their work. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent de01a37 commit 201c4f1

2 files changed

Lines changed: 61 additions & 50 deletions

File tree

‎scripts/check-changed.ts‎

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,39 +53,41 @@ console.log(`check:changed: ${files.length} files since ${base.slice(0, 12)}`);
5353
// Ignored files outside node_modules (generated sources, .astro types) and the
5454
// install state can change results too, so they are part of the key.
5555
// tsbuildinfo files are excluded: typecheck rewrites them on every run.
56-
const hash = NodeCrypto.createHash("sha256");
57-
const hashFile = (file: string) => {
58-
hash.update(`\0${file}\0`);
59-
const path = NodePath.join(root, file);
60-
if (NodeFS.existsSync(path)) hash.update(NodeFS.readFileSync(path));
56+
// Computed again after a passing run, so edits made during the run are never
57+
// cached as passing.
58+
const inputKey = () => {
59+
const hash = NodeCrypto.createHash("sha256");
60+
hash.update(`${process.version}\0${base}\0`);
61+
hash.update(
62+
NodeChildProcess.execFileSync("git", ["diff", "--binary", "--no-renames", base, "--"], {
63+
cwd: root,
64+
maxBuffer: 1024 * 1024 * 1024,
65+
}),
66+
);
67+
const ignored = git(
68+
"ls-files",
69+
"--others",
70+
"--ignored",
71+
"--exclude-standard",
72+
"--",
73+
":!node_modules/**",
74+
":!**/node_modules/**",
75+
":!.t3/**",
76+
":!**/*.tsbuildinfo",
77+
);
78+
for (const file of [
79+
...git("ls-files", "--others", "--exclude-standard"),
80+
...ignored,
81+
"node_modules/.modules.yaml",
82+
"node_modules/.pnpm/lock.yaml",
83+
]) {
84+
hash.update(`\0${file}\0`);
85+
const path = NodePath.join(root, file);
86+
if (NodeFS.existsSync(path)) hash.update(NodeFS.readFileSync(path));
87+
}
88+
return hash.digest("hex");
6189
};
62-
hash.update(`${process.version}\0${base}\0`);
63-
hash.update(
64-
NodeChildProcess.execFileSync("git", ["diff", "--binary", "--no-renames", base, "--"], {
65-
cwd: root,
66-
maxBuffer: 1024 * 1024 * 1024,
67-
}),
68-
);
69-
const ignored = git(
70-
"ls-files",
71-
"--others",
72-
"--ignored",
73-
"--exclude-standard",
74-
"--",
75-
":!node_modules/**",
76-
":!**/node_modules/**",
77-
":!.t3/**",
78-
":!**/*.tsbuildinfo",
79-
);
80-
for (const file of [
81-
...untracked,
82-
...ignored,
83-
"node_modules/.modules.yaml",
84-
"node_modules/.pnpm/lock.yaml",
85-
]) {
86-
hashFile(file);
87-
}
88-
const key = hash.digest("hex");
90+
const key = inputKey();
8991
const cachePath = NodePath.join(root, "node_modules/.cache/check-changed.json");
9092
const cached = NodeFS.existsSync(cachePath)
9193
? (JSON.parse(NodeFS.readFileSync(cachePath, "utf8")) as { key: string; summary: Array<string> })
@@ -214,7 +216,13 @@ console.log(`\n${summary.join("\n")}`);
214216
// would cut off a failing step's output.
215217
if (results.some((result) => !result.ok)) {
216218
process.exitCode = 1;
219+
} else if (inputKey() !== key) {
220+
console.log(" pass not cached: files changed during the run");
217221
} else {
218-
NodeFS.mkdirSync(NodePath.dirname(cachePath), { recursive: true });
219-
NodeFS.writeFileSync(cachePath, JSON.stringify({ key, summary }));
222+
try {
223+
NodeFS.mkdirSync(NodePath.dirname(cachePath), { recursive: true });
224+
NodeFS.writeFileSync(cachePath, JSON.stringify({ key, summary }));
225+
} catch (error) {
226+
console.warn(` pass not cached: ${error instanceof Error ? error.message : String(error)}`);
227+
}
220228
}

‎scripts/knip-schemas.ts‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,26 @@ const preprocess = async (options: Parameters<Preprocessor>[0]) => {
140140
files: [...files].map(([file, names]) => [file, [...names]]),
141141
});
142142
});
143+
const workers = shares
144+
.filter((share) => share.length > 0)
145+
.map((share) => new NodeWorkerThreads.Worker(new URL(import.meta.url), { workerData: share }));
143146
const results = await Promise.all(
144-
shares
145-
.filter((share) => share.length > 0)
146-
.map(
147-
(share) =>
148-
new Promise<ReturnType<typeof findSchemas>>((resolve, reject) => {
149-
const worker = new NodeWorkerThreads.Worker(new URL(import.meta.url), {
150-
workerData: share,
151-
});
152-
worker.once("message", resolve);
153-
worker.once("error", reject);
154-
worker.once("exit", (code) => {
155-
if (code !== 0) reject(new Error(`knip-schemas worker exited with code ${code}`));
156-
});
157-
}),
158-
),
159-
);
147+
workers.map(
148+
(worker) =>
149+
new Promise<ReturnType<typeof findSchemas>>((resolve, reject) => {
150+
worker.once("message", resolve);
151+
worker.once("error", reject);
152+
worker.once("exit", (code) => {
153+
if (code !== 0) reject(new Error(`knip-schemas worker exited with code ${code}`));
154+
});
155+
}),
156+
),
157+
).catch(async (error: unknown) => {
158+
// knip only sets an exit code on a failed preprocessor, so stop the
159+
// siblings or the failed run waits for their work.
160+
await Promise.allSettled(workers.map((worker) => worker.terminate()));
161+
throw error;
162+
});
160163

161164
for (const [filePath, names] of results.flat()) {
162165
for (const category of categories) {

0 commit comments

Comments
 (0)