Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off globalConsole:off
2// Runs locally what CI runs for this branch's changes: format and lint of the
3// changed files, knip, typecheck of the affected packages and their dependents,
4// and the test files beside the changed files.
5//
6// vp run check:changed compare against the merge base with origin/main
7// vp run check:changed <ref> compare against <ref>
8//
9// Uncommitted and untracked files count. The package mapping comes from
10// .github/scripts/ci-changes.cjs, so this typechecks what CI would typecheck.
11import * as NodeChildProcess from "node:child_process";
12import * as NodeFS from "node:fs";
13import * as NodePath from "node:path";
14import { decide, readWorkspace } from "../.github/scripts/ci-changes.cjs";
15
16const root = NodePath.resolve(import.meta.dirname, "..");
17const CODE = /\.[cm]?[jt]sx?$/;
18
19const git = (...args: Array<string>) =>
20 NodeChildProcess.execFileSync("git", args, { cwd: root, encoding: "utf8" })
21 .split("\n")
22 .filter(Boolean);
23
24const base = process.argv[2] ?? git("merge-base", "HEAD", "origin/main")[0] ?? "origin/main";
25// --no-renames lists a rename as its old and new path, as CI does.
26const untracked = git("ls-files", "--others", "--exclude-standard");
27const files = [
28 ...new Set([...git("diff", "--name-only", "--no-renames", base, "--"), ...untracked]),
29].sort();
30
31if (files.length === 0) {
32 console.log(`check:changed: no changes since ${base.slice(0, 12)}.`);
33 process.exit(0);
34}
35
36const present = files.filter((file) => NodeFS.existsSync(NodePath.join(root, file)));
37const code = present.filter((file) => CODE.test(file));
38const packages = readWorkspace();
39const { affected, outputs } = decide(files, packages, false);
40
41// Changed test files, and the test files beside a changed source file
42// (foo.ts -> foo.test.ts, foo.browser.test.tsx). Vitest's import-graph
43// `related` reaches most of a package: one leaf server file pulled in 252 of
44// 406 server test files and took 7 minutes. CI runs the rest.
45const TEST = /\.test\.[cm]?[jt]sx?$/;
46const tests = new Set(code.filter((file) => TEST.test(file)));
47for (const file of code) {
48 const dir = NodePath.dirname(file);
49 const prefix = `${NodePath.basename(file).replace(CODE, "")}.`;
50 for (const entry of NodeFS.readdirSync(NodePath.join(root, dir))) {
51 if (entry.startsWith(prefix) && TEST.test(entry)) tests.add(NodePath.join(dir, entry));
52 }
53}
54
55// knip:check takes about 35 s and cannot be scoped to a package, so it runs
56// only when the change can leave a file, export, or dependency unused.
57const addedOrDeleted = new Set([
58 ...git("diff", "--name-only", "--no-renames", "--diff-filter=AD", base, "--"),
59 ...untracked,
60]);
61const changedExport =
62 code.length > 0 &&
63 git("diff", "--unified=0", base, "--", ...code).some((line) =>
64 /^[+-](?!\+\+ |-- ).*\bexport\b/.test(line),
65 );
66const runKnip =
67 changedExport ||
68 files.some(
69 (file) =>
70 (CODE.test(file) && addedOrDeleted.has(file)) ||
71 NodePath.basename(file) === "package.json" ||
72 file === "knip.jsonc",
73 );
74
75const filters = (names: ReadonlyArray<string>) => names.flatMap((name) => ["--filter", name]);
76const steps = [
77 present.length > 0 && {
78 name: "format",
79 args: ["fmt", "--check", "--no-error-on-unmatched-pattern", ...present],
80 },
81 code.length > 0 && { name: "lint", args: ["lint", ...code] },
82 runKnip && { name: "knip", args: ["run", "knip:check"] },
83 affected.length > 0 && {
84 name: `typecheck (${affected.length} packages)`,
85 args: ["run", "--cache", "--concurrency-limit", "4", ...filters(affected), "typecheck"],
86 },
87 // From the repo root, the root vite.config.ts hands each file to its package's config.
88 tests.size > 0 && {
89 name: `test (${tests.size} files)`,
90 args: ["test", "run", "--passWithNoTests", ...tests],
91 },
92].filter((step) => step !== false);
93
94console.log(`check:changed: ${files.length} files since ${base.slice(0, 12)}`);
95const results = steps.map((step) => {
96 console.log(`\n> vp ${step.name}`);
97 const started = performance.now();
98 const { status } = NodeChildProcess.spawnSync("vp", step.args, { cwd: root, stdio: "inherit" });
99 const seconds = ((performance.now() - started) / 1000).toFixed(1);
100 return { name: step.name, ok: status === 0, seconds };
101});
102
103console.log("\ncheck:changed summary");
104for (const result of results) {
105 console.log(` ${result.ok ? "pass" : "FAIL"} ${result.name} ${result.seconds}s`);
106}
107const ciOnly = (["build", "rust", "mobile_native", "release_smoke"] as const).filter(
108 (job) => outputs[job],
109);
110if (ciOnly.length > 0) console.log(` CI also runs: ${ciOnly.join(", ")}`);
111if (!runKnip) console.log(" knip skipped: no files, exports, or dependencies changed");
112if (results.some((result) => !result.ok)) process.exit(1);