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