Skip to content

Commit 7bcbe74

Browse files
steipetePeter Steinberger
andauthored
fix(plugins): keep deferred jiti transforms in a durable user cache (#117355)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 9e43844 commit 7bcbe74

3 files changed

Lines changed: 125 additions & 18 deletions

File tree

‎src/plugins/plugin-module-loader-cache.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ describe("getCachedPluginModuleLoader", () => {
280280
tryNative: false,
281281
});
282282
expect(options.fsCache).toEqual(expect.any(String));
283-
expect(String(options.fsCache)).toContain(`${path.sep}jiti${path.sep}openclaw${path.sep}`);
283+
expect(String(options.fsCache)).toContain(`${path.sep}openclaw${path.sep}jiti${path.sep}`);
284284
expect(options.alias).toEqual({
285285
alpha: "/repo/alpha.js",
286286
zeta: "/repo/zeta.js",

‎src/plugins/sdk-alias.test.ts‎

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
bundledPluginRoot,
1010
} from "openclaw/plugin-sdk/test-fixtures";
1111
import { afterAll, describe, expect, it, vi } from "vitest";
12-
import { withEnv } from "../test-utils/env.js";
12+
import { withEnv, withEnvAsync } from "../test-utils/env.js";
1313
import {
1414
buildPluginLoaderAliasMap,
1515
createPluginLoaderModuleCacheKey,
@@ -2352,18 +2352,121 @@ describe("buildPluginLoaderAliasMap memoization", () => {
23522352
});
23532353

23542354
describe("buildPluginLoaderJitiOptions", () => {
2355-
it("adds the versioned fs cache directory to plugin loader jiti options", () => {
2355+
it("scopes the jiti cache to the durable user cache and OpenClaw install", () => {
23562356
const root = createTrustedOpenClawPackageFixture("2.0.0");
23572357
const tmpDir = path.join(root, "tmp");
2358+
const cacheRoot = path.join(root, "cache");
23582359

2359-
const options = withEnv({ TMPDIR: tmpDir }, () =>
2360+
const options = withEnv({ TMPDIR: tmpDir, XDG_CACHE_HOME: ` ${cacheRoot} ` }, () =>
23602361
buildPluginLoaderJitiOptions(
23612362
{ "openclaw/plugin-sdk/core": path.join(root, "dist", "plugin-sdk", "core.js") },
23622363
{ modulePath: path.join(root, "dist", "plugins", "loader.js") },
23632364
),
23642365
);
23652366

2366-
expect(options.fsCache).toContain(path.join(tmpDir, "jiti", "openclaw", "2.0.0"));
2367+
expect(options.fsCache).toContain(path.join(cacheRoot, "openclaw", "jiti", "2.0.0"));
2368+
expect(options.fsCache).not.toContain(tmpDir);
2369+
});
2370+
2371+
it.each(["", " ", "relative/cache"])(
2372+
"ignores non-absolute XDG cache roots (%j)",
2373+
(xdgCacheHome) => {
2374+
const root = createTrustedOpenClawPackageFixture("2.0.0");
2375+
const homeDir = path.join(root, "home");
2376+
const options = withEnv(
2377+
{
2378+
XDG_CACHE_HOME: xdgCacheHome,
2379+
LOCALAPPDATA: undefined,
2380+
OPENCLAW_HOME: homeDir,
2381+
},
2382+
() => buildPluginLoaderJitiOptions({}, { modulePath: path.join(root, "dist", "plugins") }),
2383+
);
2384+
const platformCacheRoot =
2385+
process.platform === "win32"
2386+
? path.join(homeDir, "AppData", "Local")
2387+
: process.platform === "darwin"
2388+
? path.join(homeDir, "Library", "Caches")
2389+
: path.join(homeDir, ".cache");
2390+
2391+
expect(options.fsCache).toContain(path.join(platformCacheRoot, "openclaw", "jiti", "2.0.0"));
2392+
},
2393+
);
2394+
2395+
it.skipIf(process.platform !== "win32")(
2396+
"uses an absolute Windows local application-data cache root",
2397+
() => {
2398+
const root = createTrustedOpenClawPackageFixture("2.0.0");
2399+
const localAppData = path.join(root, "local-app-data");
2400+
const options = withEnv(
2401+
{
2402+
XDG_CACHE_HOME: undefined,
2403+
LOCALAPPDATA: ` ${localAppData} `,
2404+
OPENCLAW_HOME: path.join(root, "home"),
2405+
},
2406+
() => buildPluginLoaderJitiOptions({}, { modulePath: path.join(root, "dist", "plugins") }),
2407+
);
2408+
2409+
expect(options.fsCache).toContain(path.join(localAppData, "openclaw", "jiti", "2.0.0"));
2410+
},
2411+
);
2412+
2413+
it.each(["JITI_FS_CACHE", "JITI_CACHE"])("honors the %s filesystem-cache opt-out", (envKey) => {
2414+
const root = createTrustedOpenClawPackageFixture("2.0.0");
2415+
const options = withEnv(
2416+
{
2417+
JITI_FS_CACHE: envKey === "JITI_FS_CACHE" ? "false" : undefined,
2418+
JITI_CACHE: envKey === "JITI_CACHE" ? "false" : undefined,
2419+
XDG_CACHE_HOME: path.join(root, "cache"),
2420+
},
2421+
() => buildPluginLoaderJitiOptions({}, { modulePath: path.join(root, "dist", "plugins") }),
2422+
);
2423+
2424+
expect(options.fsCache).toBe(false);
2425+
});
2426+
2427+
it("keeps deferred jiti imports working after the temporary directory is deleted", async () => {
2428+
const root = createTrustedOpenClawPackageFixture("2.0.0");
2429+
const transientTmpRoot = path.join(root, "tmp");
2430+
const durableCacheRoot = path.join(root, "cache");
2431+
const sourceRoot = path.join(root, "source");
2432+
const parentModulePath = path.join(sourceRoot, "parent.ts");
2433+
const childModulePath = path.join(sourceRoot, "child.ts");
2434+
mkdirSafeDir(sourceRoot);
2435+
mkdirSafeDir(transientTmpRoot);
2436+
fs.writeFileSync(
2437+
parentModulePath,
2438+
'export const loadChild = () => import("./child.ts");\n',
2439+
"utf-8",
2440+
);
2441+
fs.writeFileSync(childModulePath, 'export const marker = "still-delivered";\n', "utf-8");
2442+
const createJiti = await getCreateJiti();
2443+
2444+
await withEnvAsync(
2445+
{
2446+
TMPDIR: transientTmpRoot,
2447+
XDG_CACHE_HOME: durableCacheRoot,
2448+
JITI_FS_CACHE: undefined,
2449+
JITI_CACHE: undefined,
2450+
},
2451+
async () => {
2452+
const options = buildPluginLoaderJitiOptions({}, { modulePath: parentModulePath });
2453+
const loadSourceModule = createJiti(parentModulePath, {
2454+
...options,
2455+
tryNative: false,
2456+
});
2457+
const parent = loadSourceModule(parentModulePath) as {
2458+
loadChild: () => Promise<{ marker: string }>;
2459+
};
2460+
2461+
fs.rmSync(transientTmpRoot, { recursive: true, force: true });
2462+
2463+
await expect(parent.loadChild()).resolves.toMatchObject({ marker: "still-delivered" });
2464+
expect(String(options.fsCache)).toContain(path.join(durableCacheRoot, "openclaw", "jiti"));
2465+
expect(fs.readdirSync(String(options.fsCache)).some((file) => file.includes("child"))).toBe(
2466+
true,
2467+
);
2468+
},
2469+
);
23672470
});
23682471

23692472
it("pre-normalizes and marks alias maps for source transforms", () => {

‎src/plugins/sdk-alias.ts‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import os from "node:os";
44
import path from "node:path";
55
import { fileURLToPath } from "node:url";
66
import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/string-coerce";
7+
import { resolveRequiredHomeDir } from "../infra/home-dir.js";
78
import { tryReadJsonSync } from "../infra/json-files.js";
89
import { resolveOpenClawPackageRootSync } from "../infra/openclaw-root.js";
910
import { resolveOpenClawDevSourceRoot } from "./dev-source-root.js";
@@ -51,18 +52,21 @@ function sanitizeJitiCachePathSegment(value: string): string {
5152
return normalized.length > 0 ? normalized : "unknown";
5253
}
5354

54-
function resolveJitiFsCacheTmpDir(): string {
55-
let tmpDir = os.tmpdir();
56-
if (process.env.TMPDIR && tmpDir === process.cwd() && !process.env.JITI_RESPECT_TMPDIR_ENV) {
57-
const originalTmpDir = process.env.TMPDIR;
58-
delete process.env.TMPDIR;
59-
try {
60-
tmpDir = os.tmpdir();
61-
} finally {
62-
process.env.TMPDIR = originalTmpDir;
63-
}
55+
function resolveJitiFsCacheRoot(): string {
56+
const xdgCacheHome = process.env.XDG_CACHE_HOME?.trim();
57+
if (xdgCacheHome && path.isAbsolute(xdgCacheHome)) {
58+
return xdgCacheHome;
59+
}
60+
const homeDir = resolveRequiredHomeDir(process.env, os.homedir);
61+
if (process.platform === "win32") {
62+
const localAppData = process.env.LOCALAPPDATA?.trim();
63+
return localAppData && path.isAbsolute(localAppData)
64+
? localAppData
65+
: path.join(homeDir, "AppData", "Local");
6466
}
65-
return tmpDir;
67+
return process.platform === "darwin"
68+
? path.join(homeDir, "Library", "Caches")
69+
: path.join(homeDir, ".cache");
6670
}
6771

6872
function readJitiBooleanEnv(name: string, defaultValue: boolean): boolean {
@@ -141,9 +145,9 @@ function resolvePluginLoaderJitiFsCacheDir(params: LoaderModuleResolveParams = {
141145
// Package installs should have package.json; keep cache setup best-effort.
142146
}
143147
return path.join(
144-
resolveJitiFsCacheTmpDir(),
145-
"jiti",
148+
resolveJitiFsCacheRoot(),
146149
"openclaw",
150+
"jiti",
147151
version,
148152
sanitizeJitiCachePathSegment(installMarker),
149153
);

0 commit comments

Comments
 (0)