Open History to see the last commit
1// Decides which CI jobs a change needs. Prints `key=value` lines for
2// $GITHUB_OUTPUT; a job runs unless its key is "false".
3//
4// node ci-changes.cjs --paths <file> gate by the listed paths
5// node ci-changes.cjs --paths <file> --full run every Linux job, gate only the macOS lint
6// node ci-changes.cjs changed paths unknown: run everything
7//
8// Every changed path maps to workspace packages, which then pull in their
9// dependents from package.json. A path this file does not recognize runs
10// everything, so a new root file or directory can only cost minutes.
11const fs = require("node:fs");
12const path = require("node:path");
13
14const root = path.resolve(__dirname, "../..");
15
16// Never read by a build, typecheck, or test. Lint still formats them.
17const INERT = [
18 /^docs\//,
19 /^\.(agents|cursor|vscode|devcontainer|macroscope|repos)\//,
20 /^[^/]+\.md$/,
21 /^LICENSE$/,
22];
23
24// Run everything: the root vite.config.ts loads the first as setup for every
25// package's tests, and the vendored mobile tarballs are installed dependencies.
26const RUNS_EVERYTHING = [
27 /^packages\/shared\/src\/(HostProcess|testing\/longTempDir)\.ts$/,
28 /^apps\/mobile\/deps\//,
29];
30
31// Paths outside a package, or files inside one that other packages read
32// through relative imports rather than a package.json dependency.
33const EXTRA_OWNERS = [
34 // Vite configs and tsconfigs of these apps import scripts/lib directly.
35 [/^scripts\/lib\//, ["@t3tools/desktop", "@t3tools/mobile", "@t3tools/web", "t3"]],
36 // scripts/build-desktop-artifact.ts reads the GNOME extension manifest.
37 [/^apps\/desktop\/gnome-extension\//, ["@t3tools/scripts"]],
38 [/^native\/browser-secret\//, ["@t3tools/desktop"]],
39 [/^native\/kde-snap-shot\//, ["@t3tools/desktop"]],
40 [/^native\/libghostty-vt\//, ["@t3tools/web"]],
41];
42const RUST = /^native\/(resource-monitor|kde-snap-shot|hyprland-snap-shot)\//;
43
44// Tests that read another package's files without declaring it. The scripts
45// tests read app manifests, assets, and sources (cursor-sdk-packaging,
46// third-party-licenses, brand-assets, build-desktop-artifact).
47const TEST_ONLY_DEPS = {
48 "@t3tools/provider-pi": ["@t3tools/client-runtime"],
49 "@t3tools/scripts": [
50 "@t3tools/desktop",
51 "@t3tools/mobile",
52 "@t3tools/web",
53 "t3",
54 "@t3tools/provider-cursor",
55 ],
56};
57// Declared dependencies that only feed a build step: the server bundles the
58// built web app (apps/server/vite.config.ts) but never imports it.
59const BUILD_ONLY_DEPS = { t3: ["@t3tools/web"] };
60
61// Sources scripts/mobile-native-static-check.ts lints, plus the tool and rule
62// configuration that decides how it lints them, plus the root package.json
63// that defines the lint:mobile command.
64const MOBILE_NATIVE =
65 /^apps\/mobile\/.*\.(swift|kt|kts)$|^apps\/mobile\/(\.swiftlint\.yml|detekt\.yml|\.editorconfig|Brewfile)$|^scripts\/mobile-native-static-check\.ts$|^package\.json$|^\.github\/workflows\/ci\.yml$|^\.github\/scripts\/ci-changes\.cjs$|^\.github\/actions\/ci-changes\//;
66
67// Jobs that test every package except these, which have jobs of their own.
68const OWN_TEST_JOB = new Set(["t3", "@t3tools/web", "@t3tools/monorepo"]);
69
70function readWorkspace() {
71 const patterns = [];
72 let inPackages = false;
73 for (const line of fs.readFileSync(path.join(root, "pnpm-workspace.yaml"), "utf8").split("\n")) {
74 if (/^\S/.test(line)) inPackages = line.startsWith("packages:");
75 const entry = inPackages && line.match(/^\s+-\s+["']?([^"'\s]+)/);
76 if (entry) patterns.push(entry[1]);
77 }
78
79 const dirs = patterns.flatMap((pattern) => {
80 if (!pattern.endsWith("/*")) return [pattern];
81 const parent = pattern.slice(0, -2);
82 return fs.readdirSync(path.join(root, parent)).map((name) => `${parent}/${name}`);
83 });
84
85 const packages = [];
86 for (const dir of dirs) {
87 const manifestPath = path.join(root, dir, "package.json");
88 if (!fs.existsSync(manifestPath)) continue;
89 const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
90 const deps = {
91 ...manifest.dependencies,
92 ...manifest.devDependencies,
93 ...manifest.peerDependencies,
94 };
95 packages.push({
96 name: manifest.name,
97 dir,
98 hasTest: Boolean(manifest.scripts?.test),
99 deps: Object.keys(deps).filter(
100 (dep) =>
101 String(deps[dep]).startsWith("workspace:") &&
102 !BUILD_ONLY_DEPS[manifest.name]?.includes(dep),
103 ),
104 });
105 }
106 return packages;
107}
108
109// Returns the packages a path touches, [] for an inert path, or null when the
110// path is not understood and everything must run.
111function ownersOf(file, packages) {
112 if (INERT.some((pattern) => pattern.test(file))) return [];
113 // A manifest can change dependencies or versions other packages read.
114 if (path.posix.basename(file) === "package.json") return null;
115 if (RUNS_EVERYTHING.some((pattern) => pattern.test(file))) return null;
116 const extra = EXTRA_OWNERS.flatMap(([pattern, names]) => (pattern.test(file) ? names : []));
117 // scripts/build-desktop-artifact.test.ts reads the crates' sources.
118 if (RUST.test(file)) return ["@t3tools/scripts", ...extra];
119
120 const owner = packages
121 .filter((pkg) => file.startsWith(`${pkg.dir}/`))
122 .sort((a, b) => b.dir.length - a.dir.length)[0];
123 if (owner) return [owner.name, ...extra];
124 return extra.length > 0 ? extra : null;
125}
126
127function decide(files, packages, full) {
128 const all = new Set(packages.map((pkg) => pkg.name));
129 let affected = new Set();
130 let everything = full || files === null;
131 let rust = false;
132
133 for (const file of files ?? []) {
134 if (RUST.test(file)) rust = true;
135 const owners = ownersOf(file, packages);
136 if (owners === null) {
137 everything = true;
138 console.error(`Runs everything: ${file}`);
139 } else {
140 for (const owner of owners) affected.add(owner);
141 }
142 }
143
144 // Release smoke reads scripts/ itself, not what the scripts tests read.
145 let scriptsChanged = everything;
146 if (everything) {
147 affected = all;
148 } else {
149 // Add dependents until nothing changes.
150 let grew = true;
151 while (grew) {
152 grew = false;
153 for (const pkg of packages) {
154 if (!affected.has(pkg.name) && pkg.deps.some((dep) => affected.has(dep))) {
155 affected.add(pkg.name);
156 grew = true;
157 }
158 }
159 }
160 scriptsChanged = affected.has("@t3tools/scripts");
161 for (const [name, deps] of Object.entries(TEST_ONLY_DEPS)) {
162 if (deps.some((dep) => affected.has(dep))) affected.add(name);
163 }
164 }
165
166 const tested = packages.filter(
167 (pkg) => pkg.hasTest && !OWN_TEST_JOB.has(pkg.name) && affected.has(pkg.name),
168 );
169 const has = (name) => affected.has(name);
170
171 return {
172 affected: [...affected].sort(),
173 outputs: {
174 typecheck: affected.size > 0,
175 build: has("@t3tools/desktop") || has("t3") || has("@t3tools/web"),
176 test: tested.length > 0,
177 // Empty means every package the Test job covers.
178 test_packages: everything ? "" : tested.map((pkg) => pkg.name).join(" "),
179 test_web: has("@t3tools/web"),
180 test_server: has("t3"),
181 rust: everything || rust,
182 release_smoke: scriptsChanged,
183 mobile_native: files === null || files.some((file) => MOBILE_NATIVE.test(file)),
184 },
185 };
186}
187
188function main() {
189 const args = process.argv.slice(2);
190 const pathsIndex = args.indexOf("--paths");
191 const files =
192 pathsIndex === -1
193 ? null
194 : fs
195 .readFileSync(args[pathsIndex + 1], "utf8")
196 .split("\n")
197 .filter(Boolean);
198 const { affected, outputs } = decide(files, readWorkspace(), args.includes("--full"));
199
200 console.error(`Affected packages: ${affected.join(", ") || "none"}`);
201 for (const [key, value] of Object.entries(outputs)) console.log(`${key}=${value}`);
202}
203
204main();