Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { it as effectIt } from "@effect/vitest";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import { CommandAvailability, WindowsShellEnvironment } from "@t3tools/shared/shell";
5import * as Effect from "effect/Effect";
6// @effect-diagnostics-next-line nodeBuiltinImport:off -- Observe the existing synchronous shell probe without launching the user's login shell.
7import * as NodeChildProcess from "node:child_process";
8// @effect-diagnostics-next-line nodeBuiltinImport:off -- The fake login shell is a script the real probe spawns.
9import * as NodeFS from "node:fs";
10import * as NodeOS from "node:os";
11// @effect-diagnostics-next-line nodeBuiltinImport:off -- The fake login shell is a script the real probe spawns.
12import * as NodePath from "node:path";
13import { assert, it, vi } from "vite-plus/test";
14
15import {
16 fixPath,
17 hydratePosixHome,
18 refreshPathFromLoginShell,
19 refreshWindowsPath,
20} from "./os-jank.ts";
21
22vi.mock("node:child_process", { spy: true });
23
24it("hydrates HOME independently of a blank process HOME", () => {
25 const originalHome = process.env.HOME;
26 const env: NodeJS.ProcessEnv = { HOME: " " };
27
28 try {
29 process.env.HOME = " ";
30 hydratePosixHome(env);
31 } finally {
32 if (originalHome === undefined) {
33 delete process.env.HOME;
34 } else {
35 process.env.HOME = originalHome;
36 }
37 }
38
39 assert.equal(env.HOME, NodeOS.userInfo().homedir);
40});
41
42it("preserves an explicitly configured HOME", () => {
43 const env: NodeJS.ProcessEnv = { HOME: "/custom/home" };
44
45 hydratePosixHome(env, () => {
46 throw new Error("HOME lookup should not run");
47 });
48
49 assert.equal(env.HOME, "/custom/home");
50});
51
52effectIt.effect("hydrates PATH when the desktop handoff is absent", () =>
53 Effect.acquireUseRelease(
54 Effect.sync(() =>
55 vi
56 .spyOn(NodeChildProcess, "execFileSync")
57 .mockReturnValue("__T3CODE_ENV_PATH_START__\n/opt/tools/bin\n__T3CODE_ENV_PATH_END__\n"),
58 ),
59 (probe) =>
60 Effect.gen(function* () {
61 const env: NodeJS.ProcessEnv = { SHELL: "/bin/bash", PATH: "/usr/bin" };
62
63 yield* fixPath().pipe(
64 Effect.provideService(HostProcess.Platform, "linux"),
65 Effect.provideService(HostProcess.Environment, env),
66 Effect.provide(NodeServices.layer),
67 );
68
69 assert.equal(probe.mock.calls.length, 1);
70 assert.equal(env.PATH, "/opt/tools/bin:/usr/bin");
71 }),
72 (probe) => Effect.sync(() => probe.mockRestore()),
73 ),
74);
75
76const timedOut = (file: string) =>
77 Object.assign(new Error(`spawnSync ${file} ETIMEDOUT`), { code: "ETIMEDOUT" });
78
79effectIt.effect("waits longer for a login shell that times out at startup", () =>
80 Effect.acquireUseRelease(
81 Effect.sync(() =>
82 vi.spyOn(NodeChildProcess, "execFileSync").mockImplementation((file, _args, options) => {
83 // This profile takes 8 seconds while the machine is under load.
84 if ((options?.timeout ?? 0) < 8_000) throw timedOut(String(file));
85 return "__T3CODE_ENV_PATH_START__\n/opt/tools/bin\n__T3CODE_ENV_PATH_END__\n";
86 }),
87 ),
88 () =>
89 Effect.gen(function* () {
90 const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
91
92 yield* fixPath().pipe(
93 Effect.provideService(HostProcess.Platform, "linux"),
94 Effect.provideService(HostProcess.Environment, env),
95 Effect.provide(NodeServices.layer),
96 );
97
98 assert.equal(env.PATH, "/opt/tools/bin:/usr/bin");
99 }),
100 (probe) => Effect.sync(() => probe.mockRestore()),
101 ),
102);
103
104effectIt.effect("waits longer for only one hung login shell", () =>
105 Effect.acquireUseRelease(
106 Effect.sync(() =>
107 vi.spyOn(NodeChildProcess, "execFileSync").mockImplementation((file) => {
108 throw timedOut(String(file));
109 }),
110 ),
111 (probe) =>
112 Effect.gen(function* () {
113 const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
114
115 yield* fixPath().pipe(
116 Effect.provideService(HostProcess.Platform, "linux"),
117 Effect.provideService(HostProcess.Environment, env),
118 Effect.provide(NodeServices.layer),
119 );
120
121 const timeouts = probe.mock.calls.map(([, , options]) => options?.timeout);
122 assert.equal(timeouts.filter((timeout) => (timeout ?? 0) > 5_000).length, 1);
123 assert.equal(env.PATH, "/usr/bin");
124 }),
125 (probe) => Effect.sync(() => probe.mockRestore()),
126 ),
127);
128
129effectIt.effect("keeps mise's PATH record with the login shell PATH", () =>
130 Effect.acquireUseRelease(
131 Effect.sync(() =>
132 vi
133 .spyOn(NodeChildProcess, "execFileSync")
134 .mockReturnValue(
135 [
136 "__T3CODE_ENV_PATH_START__",
137 "/home/me/.local/bin:/mise/installs/claude/latest:/mise/shims",
138 "__T3CODE_ENV_PATH_END__",
139 "__T3CODE_ENV___MISE_DIFF_START__",
140 "mise-diff",
141 "__T3CODE_ENV___MISE_DIFF_END__",
142 ].join("\n"),
143 ),
144 ),
145 () =>
146 Effect.gen(function* () {
147 const env: NodeJS.ProcessEnv = { SHELL: "/bin/bash", PATH: "/usr/bin" };
148
149 yield* fixPath().pipe(
150 Effect.provideService(HostProcess.Platform, "linux"),
151 Effect.provideService(HostProcess.Environment, env),
152 Effect.provide(NodeServices.layer),
153 );
154
155 assert.equal(env.__MISE_DIFF, "mise-diff");
156 }),
157 (probe) => Effect.sync(() => probe.mockRestore()),
158 ),
159);
160
161effectIt.effect(
162 "hydrates missing JAVA_HOME and feature flags from the login shell and keeps explicit ones",
163 () =>
164 Effect.acquireUseRelease(
165 Effect.sync(() =>
166 vi
167 .spyOn(NodeChildProcess, "execFileSync")
168 .mockReturnValue(
169 [
170 "__T3CODE_ENV_PATH_START__",
171 "/opt/homebrew/bin",
172 "__T3CODE_ENV_PATH_END__",
173 "__T3CODE_ENV_JAVA_HOME_START__",
174 "/opt/homebrew/opt/openjdk@17",
175 "__T3CODE_ENV_JAVA_HOME_END__",
176 "__T3CODE_ENV_T3CODE_FEATURE_FLAGS_START__",
177 "tanstack_source",
178 "__T3CODE_ENV_T3CODE_FEATURE_FLAGS_END__",
179 ].join("\n"),
180 ),
181 ),
182 () =>
183 Effect.gen(function* () {
184 const missing: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
185 const explicit: NodeJS.ProcessEnv = {
186 SHELL: "/bin/zsh",
187 PATH: "/usr/bin",
188 JAVA_HOME: "/custom/jdk",
189 T3CODE_FEATURE_FLAGS: "",
190 };
191
192 for (const env of [missing, explicit]) {
193 yield* fixPath().pipe(
194 Effect.provideService(HostProcess.Platform, "darwin"),
195 Effect.provideService(HostProcess.Environment, env),
196 Effect.provide(NodeServices.layer),
197 );
198 }
199
200 assert.equal(missing.JAVA_HOME, "/opt/homebrew/opt/openjdk@17");
201 assert.equal(explicit.JAVA_HOME, "/custom/jdk");
202 assert.equal(missing.T3CODE_FEATURE_FLAGS, "tanstack_source");
203 assert.equal(explicit.T3CODE_FEATURE_FLAGS, "");
204 }),
205 (probe) => Effect.sync(() => probe.mockRestore()),
206 ),
207);
208
209// Like a profile running `eval "$(fnm env)"`: each login shell gets a new multishell folder.
210const fnmLoginShell = (dir: string) => {
211 const shell = NodePath.join(dir, "bash");
212 NodeFS.writeFileSync(
213 shell,
214 [
215 "#!/bin/sh",
216 `n=$(cat "${dir}/count" 2>/dev/null || echo 0); n=$((n + 1)); echo "$n" > "${dir}/count"`,
217 `multishell="/run/user/1000/fnm_multishells/$((1000 + n))_17956/bin"`,
218 // `fnm env` prepends to the server's PATH the shell inherited (handed over in `path`).
219 `path="$multishell:/home/me/.local/share/fnm:$(cat "${dir}/path")"`,
220 'printf "__T3CODE_ENV_PATH_START__\\n%s\\n__T3CODE_ENV_PATH_END__\\n" "$path"',
221 'printf "__T3CODE_ENV_FNM_MULTISHELL_PATH_START__\\n%s\\n__T3CODE_ENV_FNM_MULTISHELL_PATH_END__\\n" "${multishell%/bin}"',
222 ].join("\n"),
223 { mode: 0o755 },
224 );
225 return shell;
226};
227
228effectIt.effect("re-reading an fnm login shell does not pile up its multishell folders", () =>
229 Effect.gen(function* () {
230 const dir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-fnm-"));
231 const env: NodeJS.ProcessEnv = { SHELL: fnmLoginShell(dir), PATH: "/usr/bin" };
232
233 for (let read = 0; read < 3; read++) {
234 NodeFS.writeFileSync(NodePath.join(dir, "path"), env.PATH ?? "");
235 yield* refreshPathFromLoginShell().pipe(
236 Effect.provideService(HostProcess.Platform, "linux"),
237 Effect.provideService(HostProcess.Environment, env),
238 Effect.provide(NodeServices.layer),
239 );
240 }
241 NodeFS.rmSync(dir, { recursive: true, force: true });
242
243 const multishells = (env.PATH ?? "")
244 .split(":")
245 .filter((entry) => entry.includes("fnm_multishells"));
246 assert.isAtMost(multishells.length, 1, env.PATH);
247 }),
248);
249
250effectIt.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () =>
251 Effect.gen(function* () {
252 const env: NodeJS.ProcessEnv = { Path: "C:\\Windows" };
253
254 yield* fixPath().pipe(
255 Effect.provideService(HostProcess.Platform, "win32"),
256 Effect.provideService(HostProcess.Environment, env),
257 Effect.provideService(WindowsShellEnvironment, () => Effect.succeed({ PATH: "C:\\Tools" })),
258 Effect.provideService(CommandAvailability, () => Effect.succeed(true)),
259 Effect.provide(NodeServices.layer),
260 );
261
262 assert.deepEqual(env, { PATH: "C:\\Tools;C:\\Windows" });
263 }),
264);
265
266effectIt.effect("adds a directory an installer wrote to the registry after launch", () =>
267 Effect.gen(function* () {
268 const env: NodeJS.ProcessEnv = { Path: "C:\\Tools;C:\\Windows" };
269 const installed = "C:\\Users\\me\\AppData\\Local\\cursor-agent";
270
271 yield* refreshWindowsPath().pipe(
272 Effect.provideService(HostProcess.Platform, "win32"),
273 Effect.provideService(HostProcess.Environment, env),
274 Effect.provideService(WindowsShellEnvironment, () =>
275 Effect.succeed({ PATH: `C:\\Windows;${installed}` }),
276 ),
277 );
278
279 assert.deepEqual(env, { PATH: `C:\\Tools;C:\\Windows;${installed}` });
280 }),
281);