@@ -5,10 +5,19 @@ import { CommandAvailability, WindowsShellEnvironment } from "@t3tools/shared/sh |
5 | 5 | import * as Effect from "effect/Effect"; |
6 | 6 | // @effect-diagnostics-next-line nodeBuiltinImport:off -- Observe the existing synchronous shell probe without launching the user's login shell. |
7 | 7 | import * as NodeChildProcess from "node:child_process"; |
| 8 | +// @effect-diagnostics-next-line nodeBuiltinImport:off -- The fake login shell is a script the real probe spawns. |
| 9 | +import * as NodeFS from "node:fs"; |
8 | 10 | import * as NodeOS from "node:os"; |
| 11 | +// @effect-diagnostics-next-line nodeBuiltinImport:off -- The fake login shell is a script the real probe spawns. |
| 12 | +import * as NodePath from "node:path"; |
9 | 13 | import { assert, it, vi } from "vite-plus/test"; |
10 | 14 |
|
11 | | -import { fixPath, hydratePosixHome, refreshWindowsPath } from "./os-jank.ts"; |
| 15 | +import { |
| 16 | + fixPath, |
| 17 | + hydratePosixHome, |
| 18 | + refreshPathFromLoginShell, |
| 19 | + refreshWindowsPath, |
| 20 | +} from "./os-jank.ts"; |
12 | 21 |
|
13 | 22 | vi.mock("node:child_process", { spy: true }); |
14 | 23 |
|
@@ -197,6 +206,47 @@ effectIt.effect( |
197 | 206 | ), |
198 | 207 | ); |
199 | 208 |
|
| 209 | +// Like a profile running `eval "$(fnm env)"`: each login shell gets a new multishell folder. |
| 210 | +const 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 | + |
| 228 | +effectIt.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 | + |
200 | 250 | effectIt.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () => |
201 | 251 | Effect.gen(function* () { |
202 | 252 | const env: NodeJS.ProcessEnv = { Path: "C:\\Windows" }; |
|