Open History to see the last commit
1// Unknown paths run every job; recognized packages pull in their dependents.
2// scripts/check-changed.ts shares these decisions so local checks match CI.
3const fs = require("node:fs");
4const path = require("node:path");
5
6const root = path.resolve(__dirname, "../..");
7
8const INERT = [
9 /^docs\//,
10 /^\.(agents|cursor|vscode|devcontainer|macroscope|repos)\//,
11 /^[^/]+\.md$/,
12 /^LICENSE$/,
13];
14
15// Shared test setup and vendored mobile dependencies affect every package.
16const RUNS_EVERYTHING = [
17 /^packages\/shared\/src\/(HostProcess|testing\/longTempDir)\.ts$/,
18 /^apps\/mobile\/deps\//,
19];
20
21// Includes cross-package reads absent from package.json dependencies.
22const EXTRA_OWNERS = [
23 // Vite configs and tsconfigs of these apps import scripts/lib directly.
24 [/^scripts\/lib\//, ["@t3tools/desktop", "@t3tools/mobile", "@t3tools/web", "t3"]],
25 // scripts/build-desktop-artifact.ts reads the GNOME extension manifest.
26 [/^apps\/desktop\/gnome-extension\//, ["@t3tools/scripts"]],
27 [/^native\/browser-secret\//, ["@t3tools/desktop"]],
28 [/^native\/kde-snap-shot\//, ["@t3tools/desktop"]],
29 [/^native\/libghostty-vt\//, ["@t3tools/web"]],
30];
31const RUST = /^native\/(resource-monitor|kde-snap-shot|hyprland-snap-shot)\//;
32
33// These tests read other packages without declaring dependencies.
34const TEST_ONLY_DEPS = {
35 // Evals import the server's source to test its real model-facing text.
36 "@t3tools/evals": ["t3"],
37 "@t3tools/provider-pi": ["@t3tools/client-runtime"],
38 "@t3tools/scripts": [
39 "@t3tools/desktop",
40 "@t3tools/mobile",
41 "@t3tools/web",
42 "t3",
43 "@t3tools/provider-cursor",
44 ],
45};
46// The server bundles the web build without importing it.
47const BUILD_ONLY_DEPS = { t3: ["@t3tools/web"] };
48
49const MOBILE_NATIVE =
50 /^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\//;
51
52const OWN_TEST_JOB = new Set(["t3", "@t3tools/web", "@t3tools/monorepo"]);
53
54function readWorkspace() {
55 const patterns = [];
56 let inPackages = false;
57 for (const line of fs.readFileSync(path.join(root, "pnpm-workspace.yaml"), "utf8").split("\n")) {
58 if (/^\S/.test(line)) inPackages = line.startsWith("packages:");
59 const entry = inPackages && line.match(/^\s+-\s+["']?([^"'\s]+)/);
60 if (entry) patterns.push(entry[1]);
61 }
62
63 const dirs = patterns.flatMap((pattern) => {
64 if (!pattern.endsWith("/*")) return [pattern];
65 const parent = pattern.slice(0, -2);
66 return fs.readdirSync(path.join(root, parent)).map((name) => `${parent}/${name}`);
67 });
68
69 const packages = [];
70 for (const dir of dirs) {
71 const manifestPath = path.join(root, dir, "package.json");
72 if (!fs.existsSync(manifestPath)) continue;
73 const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
74 const deps = {
75 ...manifest.dependencies,
76 ...manifest.devDependencies,
77 ...manifest.peerDependencies,
78 };
79 packages.push({
80 name: manifest.name,
81 dir,
82 hasTest: Boolean(manifest.scripts?.test),
83 deps: Object.keys(deps).filter(
84 (dep) =>
85 String(deps[dep]).startsWith("workspace:") &&
86 !BUILD_ONLY_DEPS[manifest.name]?.includes(dep),
87 ),
88 });
89 }
90 return packages;
91}
92
93// [] means inert; null means unknown and requires every job.
94function ownersOf(file, packages) {
95 if (INERT.some((pattern) => pattern.test(file))) return [];
96 // A manifest can change dependencies or versions other packages read.
97 if (path.posix.basename(file) === "package.json") return null;
98 if (RUNS_EVERYTHING.some((pattern) => pattern.test(file))) return null;
99 const extra = EXTRA_OWNERS.flatMap(([pattern, names]) => (pattern.test(file) ? names : []));
100 // scripts/build-desktop-artifact.test.ts reads the crates' sources.
101 if (RUST.test(file)) return ["@t3tools/scripts", ...extra];
102
103 const owner = packages
104 .filter((pkg) => file.startsWith(`${pkg.dir}/`))
105 .sort((a, b) => b.dir.length - a.dir.length)[0];
106 if (owner) return [owner.name, ...extra];
107 return extra.length > 0 ? extra : null;
108}
109
110function decide(files, packages, full) {
111 const all = new Set(packages.map((pkg) => pkg.name));
112 let affected = new Set();
113 let everything = full || files === null;
114 let rust = false;
115
116 for (const file of files ?? []) {
117 if (RUST.test(file)) rust = true;
118 const owners = ownersOf(file, packages);
119 if (owners === null) {
120 everything = true;
121 console.error(`Runs everything: ${file}`);
122 } else {
123 for (const owner of owners) affected.add(owner);
124 }
125 }
126
127 // Release smoke reads scripts/ itself, not what the scripts tests read.
128 let scriptsChanged = everything;
129 if (everything) {
130 affected = all;
131 } else {
132 let grew = true;
133 while (grew) {
134 grew = false;
135 for (const pkg of packages) {
136 if (!affected.has(pkg.name) && pkg.deps.some((dep) => affected.has(dep))) {
137 affected.add(pkg.name);
138 grew = true;
139 }
140 }
141 }
142 scriptsChanged = affected.has("@t3tools/scripts");
143 for (const [name, deps] of Object.entries(TEST_ONLY_DEPS)) {
144 if (deps.some((dep) => affected.has(dep))) affected.add(name);
145 }
146 }
147
148 const tested = packages.filter(
149 (pkg) => pkg.hasTest && !OWN_TEST_JOB.has(pkg.name) && affected.has(pkg.name),
150 );
151 const has = (name) => affected.has(name);
152
153 return {
154 affected: [...affected].sort(),
155 outputs: {
156 typecheck: affected.size > 0,
157 build: has("@t3tools/desktop") || has("t3") || has("@t3tools/web"),
158 test: tested.length > 0,
159 // Empty means every package the Test job covers.
160 test_packages: everything ? "" : tested.map((pkg) => pkg.name).join(" "),
161 test_web: has("@t3tools/web"),
162 test_server: has("t3"),
163 rust: everything || rust,
164 release_smoke: scriptsChanged,
165 mobile_native: files === null || files.some((file) => MOBILE_NATIVE.test(file)),
166 },
167 };
168}
169
170function main() {
171 const args = process.argv.slice(2);
172 const pathsIndex = args.indexOf("--paths");
173 const files =
174 pathsIndex === -1
175 ? null
176 : fs
177 .readFileSync(args[pathsIndex + 1], "utf8")
178 .split("\n")
179 .filter(Boolean);
180 const { affected, outputs } = decide(files, readWorkspace(), args.includes("--full"));
181
182 console.error(`Affected packages: ${affected.join(", ") || "none"}`);
183 for (const [key, value] of Object.entries(outputs)) console.log(`${key}=${value}`);
184}
185
186module.exports = { readWorkspace, decide };
187
188if (require.main === module) main();