@devries-cameron20 / t3code-1 / commits / bb9fde6fcd
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(scripts): test:cached no longer crashes when vp writes no cache summary (#28651)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Victor committed Nov 14, 2026 at 05:43 UTC bb9fde6fcda7f6f3416c3948ed802b56bd8d0cb5
3 files changed +104 -30
scripts/lib/cached-tests.test.ts
+59 -16
@@ -6,7 +6,7 @@ import * as NodeOS from "node:os";
6 import * as NodePath from "node:path";
7 import * as NodeURL from "node:url";
8
9 -import { cachedTestTasks, matchesTestPath } from "./cached-tests.ts";
9 +import { cachedTestTasks, matchesTestPath, readRunSummary } from "./cached-tests.ts";
10
11 describe("cachedTestTasks", () => {
12 it("selects every test for a checkout-root path", () => {
@@ -22,9 +22,11 @@ describe("cachedTestTasks", () => {
22 expect(matchesTestPath("apps/server-other/a.test.ts", ["apps/server"])).toBe(false);
23 });
24 it("keeps a file's cache task stable when unrelated tests are added", () => {
25 - const first = cachedTestTasks(JSON.stringify({ files: ["src/b.test.ts"], groupSize: 1 }));
25 + const first = cachedTestTasks(
26 + JSON.stringify({ run: "r", files: ["src/b.test.ts"], groupSize: 1 }),
27 + );
28 const second = cachedTestTasks(
27 - JSON.stringify({ files: ["src/a.test.ts", "src/b.test.ts"], groupSize: 1 }),
29 + JSON.stringify({ run: "r", files: ["src/a.test.ts", "src/b.test.ts"], groupSize: 1 }),
30 );
31 const key = Object.keys(first).find((key) => key !== "test-cache")!;
32 expect(second[key]).toEqual(first[key]);
@@ -32,24 +34,28 @@ describe("cachedTestTasks", () => {
34
35 it("keeps shell metacharacters in filenames out of the command", () => {
36 const tasks = cachedTestTasks(
35 - JSON.stringify({ files: ["src/a'$(touch injected).test.ts"], groupSize: 1 }),
37 + JSON.stringify({ run: "r", files: ["src/a'$(touch injected).test.ts"], groupSize: 1 }),
38 );
39 const task = Object.entries(tasks).find(([key]) => key !== "test-cache")![1];
40 expect(task.command).toBe("vp test run 'src/a'\"'\"'$(touch injected).test.ts'");
41 });
42
41 - it("rejects paths outside the checkout and empty groups", () => {
43 + it("rejects paths outside the checkout, empty groups, and unsafe run ids", () => {
44 for (const request of [
43 - { files: ["../elsewhere.test.ts"], groupSize: 1 },
44 - { files: ["/absolute.test.ts"], groupSize: 1 },
45 - { files: ["src/a.test.ts"], groupSize: 0 },
45 + { run: "r", files: ["../elsewhere.test.ts"], groupSize: 1 },
46 + { run: "r", files: ["/absolute.test.ts"], groupSize: 1 },
47 + { run: "r", files: ["src/a.test.ts"], groupSize: 0 },
48 + { files: ["src/a.test.ts"], groupSize: 1 },
49 + { run: "r; touch injected", files: ["src/a.test.ts"], groupSize: 1 },
50 ]) {
51 expect(() => cachedTestTasks(JSON.stringify(request))).toThrow();
52 }
53 });
54
55 it("accepts spec files discovered by Vitest", () => {
52 - const tasks = cachedTestTasks(JSON.stringify({ files: ["src/example.spec.ts"], groupSize: 1 }));
56 + const tasks = cachedTestTasks(
57 + JSON.stringify({ run: "r", files: ["src/example.spec.ts"], groupSize: 1 }),
58 + );
59 expect(
60 Object.values(tasks).some((task) => task.command === "vp test run 'src/example.spec.ts'"),
61 ).toBe(true);
@@ -59,11 +65,45 @@ describe("cachedTestTasks", () => {
65 expect(cachedTestTasks(undefined)).toEqual({});
66 });
67
68 + it("reads only the task summary of the given run", async () => {
69 + const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "cached-tests-"));
70 + const write = async (version: string, run: string) => {
71 + const file = NodePath.join(directory, version, "last-summary.json");
72 + await NodeFSP.mkdir(NodePath.dirname(file));
73 + const tasks = cachedTestTasks(JSON.stringify({ run, files: ["a.test.ts"], groupSize: 1 }));
74 + await NodeFSP.writeFile(
75 + file,
76 + JSON.stringify({
77 + tasks: Object.entries(tasks).map(([task_name, task]) => ({
78 + task_name,
79 + command: task.command,
80 + result: {},
81 + })),
82 + }),
83 + );
84 + };
85 + try {
86 + expect(readRunSummary(NodePath.join(directory, "missing"), "own")).toBeUndefined();
87 + await NodeFSP.writeFile(NodePath.join(directory, "file"), "");
88 + expect(readRunSummary(NodePath.join(directory, "file"), "own")).toBeUndefined();
89 + await write("v18", "other");
90 + expect(readRunSummary(directory, "own")).toBeUndefined();
91 + await write("v19", "own");
92 + await NodeFSP.mkdir(NodePath.join(directory, "corrupt"));
93 + await NodeFSP.writeFile(NodePath.join(directory, "corrupt", "last-summary.json"), "{");
94 + expect(readRunSummary(directory, "own")?.tasks).toHaveLength(3);
95 + } finally {
96 + await NodeFSP.rm(directory, { recursive: true, force: true });
97 + }
98 + });
99 +
100 it("reuses individual results, invalidates filesystem inputs, and never caches failures", async () => {
101 const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "cached-tests-"));
102 const root = NodePath.resolve(import.meta.dirname, "../..");
103 + let runId = 0;
104 const run = (files = ["a.test.ts", "b.test.ts"], uncached: Array<string> = []) =>
105 new Promise<number | null>((resolve, reject) => {
106 + runId += 1;
107 const child = NodeChildProcess.spawn(
108 process.execPath,
109 [NodePath.join(root, "node_modules/vite-plus/dist/bin.js"), "run", "test-cache"],
@@ -73,7 +113,12 @@ describe("cachedTestTasks", () => {
113 ...process.env,
114 NODE_OPTIONS: "",
115 VP_REMOTE_CACHE: "off",
76 - T3_CACHED_TESTS: JSON.stringify({ files, groupSize: 1, uncached }),
116 + T3_CACHED_TESTS: JSON.stringify({
117 + run: String(runId),
118 + files,
119 + groupSize: 1,
120 + uncached,
121 + }),
122 VITE_CACHE_PATH: NodePath.join(directory, "cache"),
123 PATH: `${NodePath.join(root, "node_modules/.bin")}${NodePath.delimiter}${process.env.PATH}`,
124 },
@@ -83,12 +128,10 @@ describe("cachedTestTasks", () => {
128 child.once("error", reject);
129 child.once("close", resolve);
130 });
86 - const summary = async () => {
87 - const value: { tasks: Array<{ result: Record<string, unknown> }> } = JSON.parse(
88 - await NodeFSP.readFile(NodePath.join(directory, "cache/v18/last-summary.json"), "utf8"),
89 - );
90 - return value.tasks.filter((task) => "CacheHit" in task.result).length;
91 - };
131 + const summary = async () =>
132 + readRunSummary(NodePath.join(directory, "cache"), String(runId))!.tasks.filter(
133 + (task) => task.result.CacheHit,
134 + ).length;
135 try {
136 await NodeFSP.mkdir(NodePath.join(directory, "node_modules"));
137 await NodeFSP.symlink(
scripts/lib/cached-tests.ts
+36 -1
@@ -15,6 +15,36 @@ interface CachedTestTask {
15 };
16 }
17
18 +interface TaskRunSummary {
19 + tasks: Array<{
20 + task_name: string;
21 + command: string;
22 + result: { CacheHit?: { source: string; saved_duration_ms: number } };
23 + }>;
24 +}
25 +
26 +// vp keeps one last-summary.json per cache, rewritten by every `vp run`, so a run is
27 +// recognized by an uncached task that every test depends on and that carries its id.
28 +const runTask = "test-cache:run";
29 +const runCommand = (run: string) => `node -e "" ${run}`;
30 +
31 +export function readRunSummary(cacheRoot: string, run: string): TaskRunSummary | undefined {
32 + try {
33 + for (const entry of NodeFS.readdirSync(cacheRoot)) {
34 + try {
35 + const summary: TaskRunSummary = JSON.parse(
36 + NodeFS.readFileSync(NodePath.join(cacheRoot, entry, "last-summary.json"), "utf8"),
37 + );
38 + if (summary.tasks.some((task) => task.command === runCommand(run))) return summary;
39 + } catch {
40 + continue;
41 + }
42 + }
43 + } catch {
44 + return undefined;
45 + }
46 +}
47 +
48 export function matchesTestPath(file: string, filters: Array<string>) {
49 return (
50 filters.length === 0 ||
@@ -36,7 +66,10 @@ export function cachedTestTasks(
66 !("groupSize" in request) ||
67 typeof request.groupSize !== "number" ||
68 !Number.isSafeInteger(request.groupSize) ||
39 - request.groupSize < 1
69 + request.groupSize < 1 ||
70 + !("run" in request) ||
71 + typeof request.run !== "string" ||
72 + !/^[\w-]+$/.test(request.run)
73 ) {
74 throw new Error("Invalid cached test request");
75 }
@@ -95,6 +128,7 @@ export function cachedTestTasks(
128 for (const group of groups) {
129 const key = NodeCrypto.createHash("sha256").update(group.join("\0")).digest("hex");
130 tasks[`test-cache:${key}`] = {
131 + dependsOn: [runTask],
132 command: `${command} ${group.map((file) => `'${NodePath.relative(directory, NodePath.join(root, file)).replaceAll("'", "'\"'\"'")}'`).join(" ")}`,
133 ...(directory === root ? {} : { cwd: NodePath.relative(root, directory) }),
134 cache: uncached.includes(group[0]!)
@@ -113,6 +147,7 @@ export function cachedTestTasks(
147 }
148 return {
149 ...tasks,
150 + [runTask]: { command: runCommand(request.run), cache: false },
151 "test-cache": { command: 'node -e ""', dependsOn: Object.keys(tasks), cache: false },
152 };
153 }
scripts/test-cached.ts
+9 -13
@@ -5,7 +5,7 @@ import * as NodeFS from "node:fs";
5 import * as NodePath from "node:path";
6 import * as NodeUtil from "node:util";
7
8 -import { matchesTestPath } from "./lib/cached-tests.ts";
8 +import { matchesTestPath, readRunSummary } from "./lib/cached-tests.ts";
9 import { shardedTestFiles } from "./lib/sharded-tests.ts";
10
11 const root = NodePath.resolve(import.meta.dirname, "..");
@@ -66,9 +66,11 @@ if (files.length === 0) {
66 console.log(
67 `Cached tests: ${files.length} files, ${groupSize} per task, concurrency ${concurrency}`,
68 );
69 +const run = NodeCrypto.randomBytes(16).toString("hex");
70 const env = {
71 ...process.env,
72 T3_CACHED_TESTS: JSON.stringify({
73 + run,
74 files,
75 groupSize,
76 uncached: values.uncached.map((file) => NodePath.relative(root, NodePath.resolve(file))),
@@ -90,24 +92,18 @@ process.exitCode = result.status ?? 1;
92 if (verifyHits > 0) {
93 const cacheRoot =
94 process.env.VITE_CACHE_PATH || NodePath.join(root, "node_modules/.vite/task-cache");
93 - const summaryFiles = NodeFS.readdirSync(cacheRoot, { withFileTypes: true })
94 - .filter((entry) => entry.isDirectory())
95 - .map((entry) => NodePath.join(cacheRoot, entry.name, "last-summary.json"))
96 - .filter((file) => NodeFS.existsSync(file))
97 - .sort((a, b) => NodeFS.statSync(b).mtimeMs - NodeFS.statSync(a).mtimeMs);
98 - const summary: {
99 - tasks: Array<{
100 - task_name: string;
101 - result: { CacheHit?: { source: string; saved_duration_ms: number } };
102 - }>;
103 - } = JSON.parse(NodeFS.readFileSync(summaryFiles[0]!, "utf8"));
95 + const summary = readRunSummary(cacheRoot, run);
96 + if (!summary) {
97 + console.error(`No task summary from this run in ${cacheRoot}`);
98 + process.exitCode ||= 1;
99 + }
100 const rank = (task: string) =>
101 NodeCrypto.createHash("sha256")
102 .update(
103 `${process.env.GITHUB_RUN_ID || "local"}:${process.env.GITHUB_RUN_ATTEMPT || "1"}:${task}`,
104 )
105 .digest("hex");
110 - const hits = summary.tasks.filter((task) => task.result.CacheHit);
106 + const hits = (summary?.tasks ?? []).filter((task) => task.result.CacheHit);
107 const verify = hits
108 .toSorted((a, b) => rank(a.task_name).localeCompare(rank(b.task_name)))
109 .slice(0, verifyHits);