perf(scripts): check:changed finishes in about 10 s #23581
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 9 commits into
main fromrefs/pull/23581/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- scripts/check-changed.ts lines 192 to 193OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -108,2 +175,19 @@108- return { name: step.name, ok: status === 0, seconds };109-});175+// Output is piped, so keep the colors a terminal would get.176+const env = process.stdout.isTTY ? { ...process.env, FORCE_COLOR: "1" } : process.env;177+const results = await Promise.all(178+ steps.map(179+ (step) =>180+ new Promise<{ name: string; ok: boolean; seconds: string }>((resolve) => {181+ const started = performance.now();182+ const output: Array<Buffer> = [];183+ const child = NodeChildProcess.spawn("vp", step.args, { cwd: root, env });184+ child.stdout.on("data", (chunk: Buffer) => output.push(chunk));185+ child.stderr.on("data", (chunk: Buffer) => output.push(chunk));186+ // A failed spawn emits "error", then "close" with a nonzero code.187+ child.on("error", (error) => output.push(Buffer.from(`${error.message}\n`)));188+ child.on("close", (status) => {189+ const ok = status === 0;190+ const seconds = ((performance.now() - started) / 1000).toFixed(1);191+ console.log(`\n> vp ${step.name} ${ok ? "pass" : "FAIL"} ${seconds}s`);192+ process.stdout.write(Buffer.concat(output));193+ resolve({ name: step.name, ok, seconds });
- scripts/check-changed.ts lines 214 to 215OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -119,6 +207,9 @@119207 "mobile native lint": outputs.mobile_native,120208 "release smoke": outputs.release_smoke,121209 }).flatMap(([job, runs]) => (runs ? [job] : []));122-if (ciOnly.length > 0) console.log(` CI also runs: ${ciOnly.join(", ")}`);123-if (!runKnip) console.log(" knip skipped: no code or manifest changed");210+if (ciOnly.length > 0) summary.push(` CI also runs: ${ciOnly.join(", ")}`);211+if (!runKnip) summary.push(" knip skipped: no code or manifest changed");212+console.log(`\n${summary.join("\n")}`);124213 if (results.some((result) => !result.ok)) process.exit(1);214+NodeFS.mkdirSync(NodePath.dirname(cachePath), { recursive: true });215+NodeFS.writeFileSync(cachePath, JSON.stringify({ key, summary }));
- scripts/check-changed.ts lines 218 to 219OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -119,6 +207,13 @@119207 "mobile native lint": outputs.mobile_native,120208 "release smoke": outputs.release_smoke,121209 }).flatMap(([job, runs]) => (runs ? [job] : []));122-if (ciOnly.length > 0) console.log(` CI also runs: ${ciOnly.join(", ")}`);123-if (!runKnip) console.log(" knip skipped: no code or manifest changed");124-if (results.some((result) => !result.ok)) process.exit(1);210+if (ciOnly.length > 0) summary.push(` CI also runs: ${ciOnly.join(", ")}`);211+if (!runKnip) summary.push(" knip skipped: no code or manifest changed");212+console.log(`\n${summary.join("\n")}`);213+// exitCode, not exit(): piped stdout is asynchronous on macOS, and exit()214+// would cut off a failing step's output.215+if (results.some((result) => !result.ok)) {216+ process.exitCode = 1;217+} else {218+ NodeFS.mkdirSync(NodePath.dirname(cachePath), { recursive: true });219+ NodeFS.writeFileSync(cachePath, JSON.stringify({ key, summary }));
- scripts/check-changed.ts lines 92 to 94OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -46,0 +74,21 @@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");89+};90+const key = inputKey();91+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;
- @@ -44,3 +47,21 @@4447 console.log(`check:changed: no changes since ${base.slice(0, 12)}.`);4548 process.exit(0);4649 }50+console.log(`check:changed: ${files.length} files since ${base.slice(0, 12)}`);51+52+// The checked tree is the merge base plus the diff plus the untracked files.53+// Ignored files outside node_modules (generated sources, .astro types) and the54+// install state can change results too, so they are part of the key.55+// tsbuildinfo files are excluded: typecheck rewrites them on every run.56+// Computed again after a passing run, so edits made during the run are never57+// 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(scripts/check-changed.ts lines 60 to 60OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -44,3 +47,14 @@4447 console.log(`check:changed: no changes since ${base.slice(0, 12)}.`);4548 process.exit(0);4649 }50+console.log(`check:changed: ${files.length} files since ${base.slice(0, 12)}`);51+52+// The checked tree is the merge base plus the diff plus the untracked files.53+// Ignored files outside node_modules (generated sources, .astro types) and the54+// install state can change results too, so they are part of the key.55+// tsbuildinfo files are excluded: typecheck rewrites them on every run.56+// Computed again after a passing run, so edits made during the run are never57+// cached as passing.58+const inputKey = () => {59+ const hash = NodeCrypto.createHash("sha256");60+ hash.update(`${process.version}\0${base}\0`);
- scripts/check-changed.ts lines 112 to 115Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -46,0 +78,21 @@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");89+};90+const key = inputKey();91+const cachePath = NodePath.join(root, "node_modules/.cache/check-changed.json");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+ };
- @@ -46,0 +71,21 @@71+ "--ignored",72+ "--exclude-standard",73+ "--",74+ ":(glob)**/.generated/**",75+ ":(glob)**/.astro/**",76+ ":(glob)**/.env*",77+ ":!**/node_modules/**",78+ );79+ for (const file of [80+ // Raw bytes of changed files too: .gitattributes normalizes line endings in81+ // the diff, but the formatter reads the file as it is on disk.82+ ...git("diff", "--name-only", "--no-renames", base, "--"),83+ ...git("ls-files", "--others", "--exclude-standard"),84+ ...ignored,85+ "node_modules/.modules.yaml",86+ "node_modules/.pnpm/lock.yaml",87+ ]) {88+ hash.update(`\0${file}\0`);89+ const path = NodePath.join(root, file);90+ // Untracked nested repositories are listed as directories.91+ if (NodeFS.statSync(path, { throwIfNoEntry: false })?.isFile()) {
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed