Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - the test kills a real Node process to prove the cache is on disk.
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFSP from "node:fs/promises";
4import * as NodeOS from "node:os";
5import * as NodePath from "node:path";
6import { assert, it } from "@effect/vitest";
7
8it.each([false, true])(
9 "persists an enabled cache before forced exit (disabled: %s)",
10 async (disabled) => {
11 const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-compile-cache-"));
12 try {
13 const cacheDirectory = NodePath.join(directory, "cache");
14 const child = NodeChildProcess.spawnSync(
15 process.execPath,
16 [
17 "--input-type=module",
18 "--eval",
19 `import * as Effect from ${JSON.stringify(import.meta.resolve("effect/Effect"))};
20const { flushCompileCache } = await import(${JSON.stringify(new URL("./compileCache.ts", import.meta.url).href)});
21await Effect.runPromise(flushCompileCache);
22process.kill(process.pid, "SIGKILL");`,
23 ],
24 {
25 encoding: "utf8",
26 env: {
27 ...process.env,
28 NODE_COMPILE_CACHE: cacheDirectory,
29 NODE_DISABLE_COMPILE_CACHE: disabled ? "1" : undefined,
30 },
31 },
32 );
33 assert.equal(child.error, undefined);
34 assert.equal(child.stderr, "");
35 assert.notEqual(child.status, 0);
36 const entries = await NodeFSP.readdir(cacheDirectory, { recursive: true }).catch(
37 (error: NodeJS.ErrnoException) => {
38 if (error.code === "ENOENT") return [];
39 throw error;
40 },
41 );
42 const files = await Promise.all(
43 entries.map((entry) => NodeFSP.stat(NodePath.join(cacheDirectory, entry))),
44 );
45 assert.equal(
46 files.some((entry) => entry.isFile()),
47 !disabled,
48 );
49 } finally {
50 await NodeFSP.rm(directory, { recursive: true, force: true });
51 }
52 },
53);
54
55it.each([
56 { disabledBy: undefined },
57 { disabledBy: "NODE_DISABLE_COMPILE_CACHE" },
58 { disabledBy: "APPIMAGE" },
59] as const)(
60 "selects the CLI cache under the explicit T3 home (disabled by: $disabledBy)",
61 async ({ disabledBy }) => {
62 const disabled = disabledBy !== undefined;
63 const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-cli-cache-"));
64 try {
65 const child = NodeChildProcess.spawnSync(
66 process.execPath,
67 [
68 "--input-type=module",
69 "--eval",
70 `
71const { enableCliCompileCache } = await import(${JSON.stringify(new URL("./cliCompileCache.ts", import.meta.url).href)});
72enableCliCompileCache({ bundled: true, argv: ["serve", "--base-dir", ${JSON.stringify(directory)}], environment: process.env, version: "1.2.3" });
73await import(${JSON.stringify(import.meta.resolve("effect/Effect"))});
74const { flushCompileCache } = await import(${JSON.stringify(new URL("./compileCache.ts", import.meta.url).href)});
75const Effect = await import(${JSON.stringify(import.meta.resolve("effect/Effect"))});
76await Effect.runPromise(flushCompileCache);
77process.kill(process.pid, "SIGKILL");`,
78 ],
79 {
80 encoding: "utf8",
81 env: {
82 ...process.env,
83 NODE_COMPILE_CACHE: undefined,
84 NODE_DISABLE_COMPILE_CACHE:
85 disabledBy === "NODE_DISABLE_COMPILE_CACHE" ? "1" : undefined,
86 APPIMAGE: disabledBy === "APPIMAGE" ? "/opt/T3-Code.AppImage" : undefined,
87 },
88 },
89 );
90 assert.equal(child.error, undefined);
91 assert.equal(child.stderr, "");
92 const cacheDirectory = NodePath.join(directory, "compile-cache", "1.2.3");
93 const entries = await NodeFSP.readdir(cacheDirectory, {
94 recursive: true,
95 }).catch((error: NodeJS.ErrnoException) => {
96 if (error.code === "ENOENT") return [];
97 throw error;
98 });
99 const files = await Promise.all(
100 entries.map((entry) => NodeFSP.stat(NodePath.join(cacheDirectory, entry))),
101 );
102 assert.equal(
103 files.some((entry) => entry.isFile()),
104 !disabled,
105 );
106 } finally {
107 await NodeFSP.rm(directory, { recursive: true, force: true });
108 }
109 },
110);
111
112it("removes another version's CLI cache directory only after a week unused", async () => {
113 const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-cli-cache-"));
114 try {
115 const root = NodePath.join(directory, "compile-cache");
116 for (const version of ["1.2.2", "1.2.4"]) {
117 await NodeFSP.mkdir(NodePath.join(root, version, "node"), { recursive: true });
118 await NodeFSP.writeFile(NodePath.join(root, version, "node", "entry"), version);
119 }
120 // 1.2.2 was last used eight days ago; 1.2.4 is another install still in use.
121 const { mtimeMs } = await NodeFSP.stat(NodePath.join(root, "1.2.2"));
122 const eightDaysAgo = (mtimeMs - 8 * 24 * 60 * 60 * 1000) / 1000;
123 await NodeFSP.utimes(NodePath.join(root, "1.2.2"), eightDaysAgo, eightDaysAgo);
124 const child = NodeChildProcess.spawnSync(
125 process.execPath,
126 [
127 "--input-type=module",
128 "--eval",
129 `
130const { enableCliCompileCache } = await import(${JSON.stringify(new URL("./cliCompileCache.ts", import.meta.url).href)});
131enableCliCompileCache({ bundled: true, argv: ["serve", "--base-dir", ${JSON.stringify(directory)}], environment: process.env, version: "1.2.3" });`,
132 ],
133 {
134 encoding: "utf8",
135 env: {
136 ...process.env,
137 NODE_COMPILE_CACHE: undefined,
138 NODE_DISABLE_COMPILE_CACHE: undefined,
139 },
140 },
141 );
142 assert.equal(child.error, undefined);
143 assert.equal(child.stderr, "");
144 assert.deepEqual((await NodeFSP.readdir(root)).sort(), ["1.2.3", "1.2.4"]);
145 } finally {
146 await NodeFSP.rm(directory, { recursive: true, force: true });
147 }
148});