Skip to content

Commit bb60b41

Browse files
committed
fix(test): isolate Vitest caches across linked checkouts
1 parent 5b532a7 commit bb60b41

8 files changed

Lines changed: 105 additions & 25 deletions

‎scripts/test-extension-batch.mts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import path from "node:path";
55
import pMap from "p-map";
66
import { collectVitestExcludePatterns } from "../test/vitest/vitest.pattern-file.ts";
7+
import { resolveVitestFsModuleCacheRoot } from "../test/vitest/vitest.performance-config.ts";
78
import {
89
createExtensionTestProcessTargetChunks,
910
listExtensionTestFilesForRoots,
@@ -102,9 +103,7 @@ function createGroupEnv({
102103
return {
103104
...baseEnv,
104105
[FS_MODULE_CACHE_PATH_ENV_KEY]: path.join(
105-
process.cwd(),
106-
"node_modules",
107-
".experimental-vitest-cache",
106+
resolveVitestFsModuleCacheRoot(),
108107
"extension-batch",
109108
sanitizeCacheSegment(`${groupIndex}-${group.config}`),
110109
),

‎scripts/test-projects.test-support.mts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
import { isVoiceCallExtensionRoot } from "../test/vitest/vitest.extension-voice-call-paths.mjs";
6161
import { isWhatsAppExtensionRoot } from "../test/vitest/vitest.extension-whatsapp-paths.mjs";
6262
import { isZaloExtensionRoot } from "../test/vitest/vitest.extension-zalo-paths.mjs";
63+
import { resolveVitestFsModuleCacheRoot } from "../test/vitest/vitest.performance-config.ts";
6364
import {
6465
isPluginSdkLightTarget,
6566
pluginSdkLightTestFiles,
@@ -4146,8 +4147,7 @@ export function applyParallelVitestCachePaths<T extends VitestSpecShape>(
41464147
const configuredCacheRoot = baseEnv[FS_MODULE_CACHE_PATH_ENV_KEY]?.trim() || undefined;
41474148
// CI publishes a persistent cache root, not a writer-safe leaf. Every
41484149
// concurrent Vitest process still needs its own live directory below it.
4149-
const cacheRoot =
4150-
configuredCacheRoot ?? path.join(cwd, "node_modules", ".experimental-vitest-cache");
4150+
const cacheRoot = configuredCacheRoot ?? resolveVitestFsModuleCacheRoot(cwd);
41514151
return specs.map((spec, index) => {
41524152
const specCachePath = spec.env?.[FS_MODULE_CACHE_PATH_ENV_KEY]?.trim();
41534153
if (specCachePath && specCachePath !== configuredCacheRoot) {

‎test/scripts/test-extension.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,8 @@ describe("scripts/test-extension.mts", () => {
607607
OPENCLAW_EXTENSION_BATCH_PARALLEL: "2",
608608
OPENCLAW_VITEST_FS_MODULE_CACHE_PATH: path.join(
609609
process.cwd(),
610-
"node_modules",
611-
".experimental-vitest-cache",
610+
".cache",
611+
"vitest",
612612
"extension-batch",
613613
"0-heavy",
614614
),

‎test/scripts/test-group-report.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,8 +1335,8 @@ describe("scripts/test-group-report run plans", () => {
13351335
);
13361336

13371337
expect(specs.map((spec) => spec.env.OPENCLAW_VITEST_FS_MODULE_CACHE_PATH)).toEqual([
1338-
path.join("/repo", "node_modules", ".experimental-vitest-cache", "0-a.ts"),
1339-
path.join("/repo", "node_modules", ".experimental-vitest-cache", "1-b.ts"),
1338+
path.join("/repo", ".cache", "vitest", "0-a.ts"),
1339+
path.join("/repo", ".cache", "vitest", "1-b.ts"),
13401340
]);
13411341
expect(specs.map((spec) => spec.vitestArgs)).toEqual([[], []]);
13421342
});

‎test/scripts/test-projects.test.ts‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4329,18 +4329,8 @@ describe("scripts/test-projects Vitest cache isolation", () => {
43294329
);
43304330

43314331
expect(specs.map((spec) => spec.env.OPENCLAW_VITEST_FS_MODULE_CACHE_PATH)).toEqual([
4332-
path.join(
4333-
"/repo",
4334-
"node_modules",
4335-
".experimental-vitest-cache",
4336-
"0-test-vitest-vitest.unit-fast.config.ts",
4337-
),
4338-
path.join(
4339-
"/repo",
4340-
"node_modules",
4341-
".experimental-vitest-cache",
4342-
"1-test-vitest-vitest.extension-memory.config.ts",
4343-
),
4332+
path.join("/repo", ".cache", "vitest", "0-test-vitest-vitest.unit-fast.config.ts"),
4333+
path.join("/repo", ".cache", "vitest", "1-test-vitest-vitest.extension-memory.config.ts"),
43444334
]);
43454335
});
43464336

‎test/vitest-performance-config.test.ts‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
// Vitest performance config tests validate performance test project setup.
2-
import { describe, expect, it } from "vitest";
2+
import { spawnSync } from "node:child_process";
3+
import fs from "node:fs";
4+
import { createRequire } from "node:module";
5+
import path from "node:path";
6+
import { afterEach, describe, expect, it } from "vitest";
7+
import { useAutoCleanupTempDirTracker } from "./helpers/temp-dir.js";
38
import { loadVitestExperimentalConfig } from "./vitest/vitest.performance-config.ts";
49

510
describe("loadVitestExperimentalConfig", () => {
611
it("enables the filesystem module cache by default", () => {
712
expect(loadVitestExperimentalConfig({}, "linux")).toEqual({
813
experimental: {
914
fsModuleCache: true,
15+
fsModuleCachePath: path.join(process.cwd(), ".cache", "vitest", "default"),
1016
},
1117
});
1218
});
@@ -22,6 +28,7 @@ describe("loadVitestExperimentalConfig", () => {
2228
).toEqual({
2329
experimental: {
2430
fsModuleCache: true,
31+
fsModuleCachePath: path.join(process.cwd(), ".cache", "vitest", "default"),
2532
},
2633
});
2734
});
@@ -57,6 +64,7 @@ describe("loadVitestExperimentalConfig", () => {
5764
).toEqual({
5865
experimental: {
5966
fsModuleCache: true,
67+
fsModuleCachePath: path.join(process.cwd(), ".cache", "vitest", "default"),
6068
},
6169
});
6270
});
@@ -84,6 +92,7 @@ describe("loadVitestExperimentalConfig", () => {
8492
).toEqual({
8593
experimental: {
8694
fsModuleCache: true,
95+
fsModuleCachePath: path.join(process.cwd(), ".cache", "vitest", "default"),
8796
importDurations: { print: true },
8897
printImportBreakdown: true,
8998
},
@@ -94,3 +103,73 @@ describe("loadVitestExperimentalConfig", () => {
94103
expect(loadVitestExperimentalConfig({ RUNNER_OS: "Windows" }, "linux")).toStrictEqual({});
95104
});
96105
});
106+
107+
describe("filesystem module cache ownership", () => {
108+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
109+
110+
it("preserves another checkout's cache when shared dependencies change", () => {
111+
const root = tempDirs.make("oc-vitest-cache-ownership-");
112+
const sharedModules = path.join(root, "shared", "node_modules");
113+
fs.mkdirSync(path.join(sharedModules, ".pnpm"), { recursive: true });
114+
const lockfile = path.join(sharedModules, ".pnpm", "lock.yaml");
115+
fs.writeFileSync(lockfile, "lockfileVersion: 1\n");
116+
const cli = path.join(
117+
path.dirname(createRequire(import.meta.url).resolve("vitest/package.json")),
118+
"vitest.mjs",
119+
);
120+
const prepareCheckout = (name: string) => {
121+
const checkout = path.join(root, name);
122+
fs.mkdirSync(checkout);
123+
// Stop Vite's workspace search before it reaches this repository. The
124+
// symlink points only at this fixture's dependencies and writable cache.
125+
fs.writeFileSync(
126+
path.join(checkout, "package.json"),
127+
JSON.stringify({ name, type: "module", workspaces: [] }),
128+
);
129+
fs.symlinkSync(sharedModules, path.join(checkout, "node_modules"), "junction");
130+
fs.writeFileSync(
131+
path.join(checkout, "fixture.test.js"),
132+
'test("runs the fixture", () => expect(2 + 2).toBe(4));\n',
133+
);
134+
const cacheConfig = loadVitestExperimentalConfig({}, "linux", checkout);
135+
const config = {
136+
root: checkout,
137+
test: {
138+
globals: true,
139+
include: ["fixture.test.js"],
140+
maxWorkers: 1,
141+
...cacheConfig,
142+
},
143+
};
144+
fs.writeFileSync(
145+
path.join(checkout, "vitest.config.mjs"),
146+
`export default ${JSON.stringify(config)};\n`,
147+
);
148+
return { checkout, cacheConfig };
149+
};
150+
const run = (checkout: string) => {
151+
const result = spawnSync(
152+
process.execPath,
153+
[cli, "run", "--config", path.join(checkout, "vitest.config.mjs")],
154+
{
155+
cwd: checkout,
156+
encoding: "utf8",
157+
env: { PATH: process.env.PATH, SystemRoot: process.env.SystemRoot, CI: "1", HOME: root },
158+
timeout: 15_000,
159+
},
160+
);
161+
expect(result.status, `${result.error ?? ""}\n${result.stdout}\n${result.stderr}`).toBe(0);
162+
};
163+
const first = prepareCheckout("first");
164+
const second = prepareCheckout("second");
165+
run(first.checkout);
166+
const firstCache =
167+
first.cacheConfig.experimental?.fsModuleCachePath ??
168+
path.join(sharedModules, ".experimental-vitest-cache");
169+
const sentinel = path.join(firstCache, "first-checkout-sentinel");
170+
fs.writeFileSync(sentinel, "owned by first checkout");
171+
fs.writeFileSync(lockfile, "lockfileVersion: 2\n");
172+
run(second.checkout);
173+
expect(fs.readFileSync(sentinel, "utf8")).toBe("owned by first checkout");
174+
});
175+
});

‎test/vitest/vitest.performance-config.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Vitest performance config helper normalizes performance test environment settings.
2+
import path from "node:path";
23
type EnvMap = Record<string, string | undefined>;
34

45
const isEnabled = (value: string | undefined): boolean => {
@@ -28,9 +29,16 @@ type VitestExperimentalConfig = {
2829
};
2930
};
3031

32+
// Linked worktrees may share node_modules; invalidating that cache would remove
33+
// another checkout's transforms. Keep writable cache ownership in this checkout.
34+
export function resolveVitestFsModuleCacheRoot(cwd = process.cwd()): string {
35+
return path.join(cwd, ".cache", "vitest");
36+
}
37+
3138
export function loadVitestExperimentalConfig(
3239
env: EnvMap = process.env,
3340
platform: NodeJS.Platform = process.platform,
41+
cwd = process.cwd(),
3442
): VitestExperimentalConfig {
3543
const experimental: {
3644
fsModuleCache?: true;
@@ -46,8 +54,12 @@ export function loadVitestExperimentalConfig(
4654
if (windowsEnv && isEnabled(env.OPENCLAW_VITEST_FS_MODULE_CACHE)) {
4755
experimental.fsModuleCache = true;
4856
}
49-
if (experimental.fsModuleCache && env.OPENCLAW_VITEST_FS_MODULE_CACHE_PATH?.trim()) {
50-
experimental.fsModuleCachePath = env.OPENCLAW_VITEST_FS_MODULE_CACHE_PATH.trim();
57+
if (experimental.fsModuleCache) {
58+
// The default leaf cannot contain the scheduler's concurrent cache leaves:
59+
// Vitest recursively removes its selected directory when lockfiles change.
60+
experimental.fsModuleCachePath =
61+
env.OPENCLAW_VITEST_FS_MODULE_CACHE_PATH?.trim() ||
62+
path.join(resolveVitestFsModuleCacheRoot(cwd), "default");
5163
}
5264
if (isEnabled(env.OPENCLAW_VITEST_IMPORT_DURATIONS)) {
5365
experimental.importDurations = { print: true };

‎test/vitest/vitest.shared.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,6 @@ export const sharedVitestConfig = {
584584
"src/infra/tailscale.ts",
585585
],
586586
},
587-
...loadVitestExperimentalConfig(),
587+
...loadVitestExperimentalConfig(process.env, process.platform, repoRoot),
588588
},
589589
};

0 commit comments

Comments
 (0)