Skip to content

Commit 0986278

Browse files
committed
perf(server): reuse compiled modules for CLI commands
1 parent 476590d commit 0986278

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎apps/server/src/bin.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* of evaluation) loads; everything else defers to the real CLI in ./binCli.ts.
66
*/
77
import { isEntrypoint } from "./entrypoint.ts";
8+
import { enableCliCompileCache } from "./cliCompileCache.ts";
89

910
if (
1011
isEntrypoint({
@@ -35,6 +36,7 @@ if (
3536
const { runAcpMcpCliFastPath } = await import("./mcp/AcpMcpStdioBridge.ts");
3637
await runAcpMcpCliFastPath(command, process.argv.slice(3));
3738
} else {
39+
enableCliCompileCache();
3840
// Windows desktop launches without stdin; attach before reading, except on fast paths using pipes.
3941
const { attachConsoleStdin } = await import("./cli/consoleStdin.ts");
4042
attachConsoleStdin();
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// @effect-diagnostics nodeBuiltinImport:off -- Resolves the real CLI entrypoint for subprocess tests.
2+
import * as NodeURL from "node:url";
3+
import * as NodeServices from "@effect/platform-node/NodeServices";
4+
import { expect, it } from "@effect/vitest";
5+
import * as Effect from "effect/Effect";
6+
import * as FileSystem from "effect/FileSystem";
7+
import * as Stream from "effect/Stream";
8+
import { ChildProcess, ChildProcessSpawner } from "effect/process";
9+
10+
it.live("enables CLI caching while respecting overrides and unavailable storage", () =>
11+
Effect.gen(function* () {
12+
const fs = yield* FileSystem.FileSystem;
13+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
14+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-cache-test-" });
15+
const report = `import { getCompileCacheDir } from 'node:module'; process.once('exit', () => console.error('CACHE=' + (getCompileCacheDir() ?? 'disabled')));`;
16+
const run = Effect.fn("cliCompileCache.test.run")(function* (env: Record<string, string>) {
17+
const child = yield* spawner.spawn(
18+
ChildProcess.make(
19+
process.execPath,
20+
[
21+
"--experimental-strip-types",
22+
"--import",
23+
`data:text/javascript,${encodeURIComponent(report)}`,
24+
NodeURL.fileURLToPath(new URL("./bin.ts", import.meta.url)),
25+
"--help",
26+
],
27+
{
28+
env: {
29+
XDG_CACHE_HOME: `${root}/cache`,
30+
T3CODE_HOME: `${root}/home`,
31+
NODE_COMPILE_CACHE: undefined,
32+
NODE_DISABLE_COMPILE_CACHE: undefined,
33+
APPIMAGE: "",
34+
...env,
35+
},
36+
},
37+
),
38+
);
39+
return yield* Effect.all(
40+
{
41+
stdout: child.stdout.pipe(Stream.decodeText(), Stream.mkString),
42+
stderr: child.stderr.pipe(Stream.decodeText(), Stream.mkString),
43+
exitCode: child.exitCode.pipe(Effect.map(Number)),
44+
},
45+
{ concurrency: "unbounded" },
46+
);
47+
}, Effect.scoped);
48+
const normal = yield* run({});
49+
expect(normal.exitCode).toBe(0);
50+
expect(normal.stderr).not.toContain("CACHE=disabled");
51+
const existing = yield* run({ NODE_COMPILE_CACHE: `${root}/explicit` });
52+
expect(existing.exitCode).toBe(0);
53+
expect(existing.stderr.replaceAll("\\", "/")).toContain(
54+
`${root}/explicit`.replaceAll("\\", "/"),
55+
);
56+
const disabled = yield* run({ NODE_DISABLE_COMPILE_CACHE: "1" });
57+
expect(disabled.exitCode).toBe(0);
58+
expect(disabled.stderr).toContain("CACHE=disabled");
59+
yield* fs.writeFileString(`${root}/blocked`, "not a directory");
60+
const unavailable = yield* run({ XDG_CACHE_HOME: `${root}/blocked` });
61+
expect(unavailable.exitCode).toBe(0);
62+
expect(unavailable.stderr).toContain("CACHE=disabled");
63+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
64+
);

‎apps/server/src/cliCompileCache.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @effect-diagnostics nodeBuiltinImport:off -- Runs before the CLI's Effect runtime loads.
2+
import * as NodeModule from "node:module";
3+
import * as NodeOS from "node:os";
4+
import * as NodePath from "node:path";
5+
import * as NodeSea from "node:sea";
6+
7+
export function enableCliCompileCache(): void {
8+
try {
9+
// AppImage paths change each launch; SEA has no separate modules to cache.
10+
if (process.env.APPIMAGE || NodeSea.isSea()) return;
11+
const cacheRoot =
12+
process.env.XDG_CACHE_HOME ||
13+
// oxlint-disable-next-line t3code/no-global-process-runtime -- Runs before the CLI's Effect runtime loads.
14+
(process.platform === "linux" ? NodePath.join(NodeOS.homedir(), ".cache") : NodeOS.tmpdir());
15+
NodeModule.enableCompileCache(NodePath.join(cacheRoot, "t3code", "compile-cache"));
16+
} catch {}
17+
}

0 commit comments

Comments
 (0)