GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): login-shell re-reads no longer pile up fnm multishell fo… · patroza/t3code@0180628

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src
    • os-jank.test.ts
    • os-jank.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/os-jank.test.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/os-jank.test.ts
+51-1Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@ import { CommandAvailability, WindowsShellEnvironment } from "@t3tools/shared/sh
55
import * as Effect from "effect/Effect";
66
// @effect-diagnostics-next-line nodeBuiltinImport:off -- Observe the existing synchronous shell probe without launching the user's login shell.
77
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";
810
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";
913
import { assert, it, vi } from "vite-plus/test";
1014

11-
import { fixPath, hydratePosixHome, refreshWindowsPath } from "./os-jank.ts";
15+
import {
16+
fixPath,
17+
hydratePosixHome,
18+
refreshPathFromLoginShell,
19+
refreshWindowsPath,
20+
} from "./os-jank.ts";
1221

1322
vi.mock("node:child_process", { spy: true });
1423

@@ -197,6 +206,47 @@ effectIt.effect(
197206
),
198207
);
199208

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+
200250
effectIt.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () =>
201251
Effect.gen(function* () {
202252
const env: NodeJS.ProcessEnv = { Path: "C:\\Windows" };
Collapse file

‎apps/server/src/os-jank.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/os-jank.ts
+26-2Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ function logPathHydrationWarning(message: string, error?: unknown): void {
3232
// __MISE_DIFF is mise's record of the PATH entries it added. It must travel
3333
// with the shell's PATH, or `mise x` can rebuild PATH behind a same-named
3434
// wrapper (`exec mise x claude -- claude`) that then re-runs itself forever.
35-
const LOGIN_SHELL_ENV_NAMES = ["PATH", "__MISE_DIFF", "JAVA_HOME", "T3CODE_FEATURE_FLAGS"] as const;
35+
const LOGIN_SHELL_ENV_NAMES = [
36+
"PATH",
37+
"__MISE_DIFF",
38+
"FNM_MULTISHELL_PATH",
39+
"JAVA_HOME",
40+
"T3CODE_FEATURE_FLAGS",
41+
] as const;
3642
const LOGIN_SHELL_TIMEOUT = Duration.seconds(5);
3743
const SLOW_LOGIN_SHELL_TIMEOUT_MS = 20_000;
3844

@@ -52,13 +58,31 @@ function applyLoginShellEnvironment(
5258
fallbackPath?: string,
5359
): void {
5460
const shellPath = shellEnvironment.PATH;
55-
const mergedPath = mergePathEntries(shellPath ?? fallbackPath, env.PATH, platform);
61+
// fnm gives every probe shell its own multishell folder; drop the last probe's on a re-read.
62+
const multishell = shellPath ? shellEnvironment.FNM_MULTISHELL_PATH : undefined;
63+
const staleMultishell = multishell ? env.FNM_MULTISHELL_PATH : undefined;
64+
// The probe inherits our PATH, so the stale folder is in the shell's PATH too.
65+
const withoutStale = (path: string | undefined) =>
66+
staleMultishell && staleMultishell !== multishell
67+
? path
68+
?.split(":")
69+
.filter((entry) => entry !== staleMultishell && !entry.startsWith(`${staleMultishell}/`))
70+
.join(":")
71+
: path;
72+
const mergedPath = mergePathEntries(
73+
withoutStale(shellPath ?? fallbackPath),
74+
withoutStale(env.PATH),
75+
platform,
76+
);
5677
if (mergedPath) {
5778
env.PATH = mergedPath;
5879
}
5980
if (shellPath && shellEnvironment.__MISE_DIFF) {
6081
env.__MISE_DIFF = shellEnvironment.__MISE_DIFF;
6182
}
83+
if (multishell) {
84+
env.FNM_MULTISHELL_PATH = multishell;
85+
}
6286
// Android SDK tools are Java programs. A keg-only JDK (Homebrew's openjdk)
6387
// is only reachable through the JAVA_HOME the shell profile sets.
6488
if (!env.JAVA_HOME && shellEnvironment.JAVA_HOME) {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.