Skip to content

Commit 66ee628

Browse files
t3dotggclaude
andauthored
perf(scripts): check:changed finishes in about 10 s (pingdotgg#23581)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 9828cad commit 66ee628

4 files changed

Lines changed: 314 additions & 91 deletions

File tree

‎docs/operations/development.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ files to packages with [ci-changes.cjs](../../.github/scripts/ci-changes.cjs), a
160160
compares against the merge base with `origin/main`, or with a ref you pass. CI still runs the full
161161
test suites and the jobs the summary lists, so a local pass does not promise a green CI run.
162162

163+
The steps run side by side, so a run takes about as long as its slowest step, usually knip's
164+
exports pass at about 10 s. When the files are exactly those of the last passing run, it prints that
165+
run's summary instead of running again. Any change to a file, the install, or a generated file runs
166+
the checks again.
167+
163168
While you iterate, run single checks:
164169

165170
```sh

‎knip.jsonc‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
],
3333
"ignoreDependencies": ["esbuild"],
3434
// The `t3` npm package's files: copied into the package, never imported.
35-
"ignore": ["npm-launcher/**"],
35+
// knip-schemas.test.ts writes a throwaway project to .knip-test-*, which a
36+
// knip run beside it in check:changed must not report.
37+
"ignore": ["npm-launcher/**", ".knip-test-*/**"],
3638
},
3739
"apps/server": {
3840
// Vite+ pack entries and the launcher used by installed background services.

‎scripts/check-changed.ts‎

Lines changed: 154 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
//
1111
// Uncommitted and untracked files count. Affected packages come from
1212
// .github/scripts/ci-changes.cjs, the same mapping CI uses to gate its jobs.
13+
// The steps run side by side; each prints its output when it finishes. A
14+
// rerun on exactly the files of the last passing run prints that run's summary.
1315
import * as NodeChildProcess from "node:child_process";
16+
import * as NodeCrypto from "node:crypto";
1417
import * as NodeFS from "node:fs";
1518
import * as NodeOS from "node:os";
1619
import * as NodePath from "node:path";
@@ -19,8 +22,13 @@ import { decide, readWorkspace } from "../.github/scripts/ci-changes.cjs";
1922
const root = NodePath.resolve(import.meta.dirname, "..");
2023
const CODE = /\.[cm]?[jt]sx?$/;
2124

25+
// quotePath=false keeps non-ASCII paths as real paths instead of quoted escapes.
2226
const git = (...args: Array<string>) =>
23-
NodeChildProcess.execFileSync("git", args, { cwd: root, encoding: "utf8" })
27+
NodeChildProcess.execFileSync("git", ["-c", "core.quotePath=false", ...args], {
28+
cwd: root,
29+
encoding: "utf8",
30+
maxBuffer: 64 * 1024 * 1024,
31+
})
2432
.split("\n")
2533
.filter(Boolean);
2634

@@ -46,6 +54,75 @@ if (files.length === 0) {
4654
console.log(`check:changed: no changes since ${base.slice(0, 12)}.`);
4755
process.exit(0);
4856
}
57+
console.log(`check:changed: ${files.length} files since ${base.slice(0, 12)}`);
58+
59+
// The checked tree is the merge base plus the diff plus the untracked files.
60+
// Ignored generated sources, .astro types, generated declarations such as
61+
// apps/mobile/expo-env.d.ts, env files, and the install state can change results
62+
// too, so they are part of the key. Other ignored folders (iOS Pods, dist, the
63+
// Electron runtime) are large and no check reads them.
64+
// Computed again after a passing run, so edits made during the run are never
65+
// cached as passing.
66+
const inputKey = () => {
67+
const hash = NodeCrypto.createHash("sha256");
68+
// CI changes test behavior (vite.config.ts rejects .only under CI).
69+
hash.update(`${process.version}\0${base}\0${process.env.CI ?? ""}\0`);
70+
hash.update(
71+
NodeChildProcess.execFileSync(
72+
"git",
73+
["diff", "--binary", "--no-renames", "--no-ext-diff", "--no-textconv", base, "--"],
74+
{ cwd: root, maxBuffer: 1024 * 1024 * 1024 },
75+
),
76+
);
77+
const ignored = git(
78+
"ls-files",
79+
"--others",
80+
"--ignored",
81+
"--exclude-standard",
82+
"--",
83+
":(glob)**/.generated/**",
84+
":(glob)**/.astro/**",
85+
":(glob)**/*.d.ts",
86+
":(glob)**/.env*",
87+
":(exclude,glob)**/node_modules/**",
88+
);
89+
for (const file of [
90+
// Raw bytes of changed files too: .gitattributes normalizes line endings in
91+
// the diff, but the formatter reads the file as it is on disk.
92+
...git("diff", "--name-only", "--no-renames", base, "--"),
93+
...git("ls-files", "--others", "--exclude-standard"),
94+
...ignored,
95+
"node_modules/.modules.yaml",
96+
"node_modules/.pnpm/lock.yaml",
97+
]) {
98+
hash.update(`\0${file}\0`);
99+
const path = NodePath.join(root, file);
100+
// Untracked nested repositories are listed as directories.
101+
if (NodeFS.statSync(path, { throwIfNoEntry: false })?.isFile()) {
102+
hash.update(NodeFS.readFileSync(path));
103+
}
104+
}
105+
return hash.digest("hex");
106+
};
107+
const key = inputKey();
108+
const cachePath = NodePath.join(root, "node_modules/.cache/check-changed.json");
109+
// A missing or unreadable cache is a miss, never a failure.
110+
const readCache = () => {
111+
try {
112+
return JSON.parse(NodeFS.readFileSync(cachePath, "utf8")) as {
113+
key: string;
114+
summary: Array<string>;
115+
};
116+
} catch {
117+
return undefined;
118+
}
119+
};
120+
const cached = readCache();
121+
if (cached?.key === key) {
122+
console.log("Nothing changed since the last passing run.\n");
123+
for (const line of cached.summary) console.log(line);
124+
process.exit(0);
125+
}
49126

50127
const present = files.filter((file) => NodeFS.existsSync(NodePath.join(root, file)));
51128
const code = present.filter((file) => CODE.test(file));
@@ -66,12 +143,24 @@ for (const file of code) {
66143
}
67144
}
68145

69-
// knip:check takes about 35 s and cannot be scoped to a package. Any code
70-
// change can leave a file, export, or dependency unused (removing the last
71-
// import of a helper does), so it runs whenever code or a manifest changes.
146+
// knip:check cannot be scoped to a package. Any code change can leave a file,
147+
// export, or dependency unused (removing the last import of a helper does), so
148+
// it runs whenever code or a manifest changes.
72149
const runKnip = files.some(
73150
(file) => CODE.test(file) || NodePath.basename(file) === "package.json" || file === "knip.jsonc",
74151
);
152+
// CI runs knip:check as one script of `knip ... && knip ...` passes. The passes
153+
// do not depend on each other, so they run side by side here.
154+
const knipCheck: string = JSON.parse(
155+
NodeFS.readFileSync(NodePath.join(root, "package.json"), "utf8"),
156+
).scripts["knip:check"];
157+
const knipPasses = knipCheck.split(" && ").map((command, index, all) => {
158+
const [bin, ...args] = command.trim().split(/\s+/);
159+
if (bin !== "knip" || /["'$\\|;&<>]/.test(command)) {
160+
throw new Error(`check:changed cannot split knip:check: ${command}`);
161+
}
162+
return { name: `knip ${index + 1}/${all.length}`, args: ["exec", "knip", ...args] };
163+
});
75164

76165
// A version bump can strand a patch in pnpm-workspace.yaml: no resolved
77166
// version uses it and pnpm rejects the install with ERR_PNPM_UNUSED_PATCH.
@@ -153,14 +242,24 @@ const steps = [
153242
args: ["fmt", "--check", "--no-error-on-unmatched-pattern", ...present],
154243
},
155244
code.length > 0 && { name: "lint", args: ["lint", ...code] },
156-
runKnip && { name: "knip", args: ["run", "knip:check"] },
245+
...(runKnip ? knipPasses : []),
157246
...graphql.map((dir) => ({
158247
name: `graphql (${dir})`,
159248
args: ["run", "--filter", `./${dir}`, "generate", "--check"],
160249
})),
161250
affected.length > 0 && {
162251
name: `typecheck (${affected.length} packages)`,
163-
args: ["run", "--cache", "--concurrency-limit", "4", ...filters(affected), "typecheck"],
252+
// `tsc --noEmit` writes nothing another package reads, so dependency order
253+
// only made dependents wait: --parallel cut a contracts change from 13 s to 9 s.
254+
args: [
255+
"run",
256+
"--cache",
257+
"--parallel",
258+
"--concurrency-limit",
259+
"4",
260+
...filters(affected),
261+
"typecheck",
262+
],
164263
},
165264
// From the repo root, the root vite.config.ts hands each file to its package's config.
166265
tests.size > 0 && {
@@ -169,23 +268,36 @@ const steps = [
169268
},
170269
].filter((step) => step !== false);
171270

172-
console.log(`check:changed: ${files.length} files since ${base.slice(0, 12)}`);
173-
const results = steps.map((step) => {
174-
console.log(`\n> vp ${step.name}`);
175-
const started = performance.now();
176-
const status =
177-
"args" in step
178-
? NodeChildProcess.spawnSync("vp", step.args, { cwd: root, stdio: "inherit" }).status
179-
: step.run()
180-
? 0
181-
: 1;
182-
const seconds = ((performance.now() - started) / 1000).toFixed(1);
183-
return { name: step.name, ok: status === 0, seconds };
184-
});
271+
// Output is piped, so keep the colors a terminal would get.
272+
const env = process.stdout.isTTY ? { ...process.env, FORCE_COLOR: "1" } : process.env;
273+
const results = await Promise.all(
274+
steps.map(
275+
(step) =>
276+
new Promise<{ name: string; ok: boolean; seconds: string }>((resolve) => {
277+
const started = performance.now();
278+
const finish = (ok: boolean, output?: Buffer) => {
279+
const seconds = ((performance.now() - started) / 1000).toFixed(1);
280+
console.log(`\n> vp ${step.name} ${ok ? "pass" : "FAIL"} ${seconds}s`);
281+
if (output) process.stdout.write(output);
282+
resolve({ name: step.name, ok, seconds });
283+
};
284+
// In-process steps print their own output. The patch check is first and
285+
// takes about a second, so the commands start right after it.
286+
if ("run" in step) return finish(step.run());
287+
const output: Array<Buffer> = [];
288+
const child = NodeChildProcess.spawn("vp", step.args, { cwd: root, env });
289+
child.stdout.on("data", (chunk: Buffer) => output.push(chunk));
290+
child.stderr.on("data", (chunk: Buffer) => output.push(chunk));
291+
// A failed spawn emits "error", then "close" with a nonzero code.
292+
child.on("error", (error) => output.push(Buffer.from(`${error.message}\n`)));
293+
child.on("close", (status) => finish(status === 0, Buffer.concat(output)));
294+
}),
295+
),
296+
);
185297

186-
console.log("\ncheck:changed summary");
298+
const summary = ["check:changed summary"];
187299
for (const result of results) {
188-
console.log(` ${result.ok ? "pass" : "FAIL"} ${result.name} ${result.seconds}s`);
300+
summary.push(` ${result.ok ? "pass" : "FAIL"} ${result.name} ${result.seconds}s`);
189301
}
190302
const ciOnly = Object.entries({
191303
"full test suites": outputs.test || outputs.test_web || outputs.test_server,
@@ -194,7 +306,24 @@ const ciOnly = Object.entries({
194306
"mobile native lint": outputs.mobile_native,
195307
"release smoke": outputs.release_smoke,
196308
}).flatMap(([job, runs]) => (runs ? [job] : []));
197-
if (ciOnly.length > 0) console.log(` CI also runs: ${ciOnly.join(", ")}`);
198-
if (!runPatchCheck) console.log(" unused patch check skipped: no dependency files changed");
199-
if (!runKnip) console.log(" knip skipped: no code or manifest changed");
200-
if (results.some((result) => !result.ok)) process.exit(1);
309+
if (ciOnly.length > 0) summary.push(` CI also runs: ${ciOnly.join(", ")}`);
310+
if (!runPatchCheck) summary.push(" unused patch check skipped: no dependency files changed");
311+
if (!runKnip) summary.push(" knip skipped: no code or manifest changed");
312+
console.log(`\n${summary.join("\n")}`);
313+
// exitCode, not exit(): piped stdout is asynchronous on macOS, and exit()
314+
// would cut off a failing step's output.
315+
if (results.some((result) => !result.ok)) {
316+
process.exitCode = 1;
317+
} else if (inputKey() !== key) {
318+
console.log(" pass not cached: files changed during the run");
319+
} else {
320+
try {
321+
// Write then rename, so a concurrent run never reads half a file.
322+
const temp = `${cachePath}.${process.pid}`;
323+
NodeFS.mkdirSync(NodePath.dirname(cachePath), { recursive: true });
324+
NodeFS.writeFileSync(temp, JSON.stringify({ key, summary }));
325+
NodeFS.renameSync(temp, cachePath);
326+
} catch (error) {
327+
console.warn(` pass not cached: ${error instanceof Error ? error.message : String(error)}`);
328+
}
329+
}

0 commit comments

Comments
 (0)