Open History to see the last commit
1import * as HostProcess from "@t3tools/shared/HostProcess";
2import {
3 listLoginShellCandidates,
4 mergePathEntries,
5 readEnvironmentFromLoginShell,
6 readPathFromLaunchctl,
7 resolveWindowsEnvironment,
8 setWindowsPath,
9} from "@t3tools/shared/shell";
10import * as Effect from "effect/Effect";
11import * as FileSystem from "effect/FileSystem";
12import * as Path from "effect/Path";
13import * as NodeOS from "node:os";
14
15function logPathHydrationWarning(message: string, error?: unknown): void {
16 process.stderr.write(
17 `[server] ${message} ${error instanceof Error ? error.message : (error ?? "")}\n`,
18 );
19}
20
21function hydratePosixPath(env: NodeJS.ProcessEnv, platform: NodeJS.Platform): void {
22 let shellEnvironment: Partial<Record<string, string>> = {};
23 for (const shell of listLoginShellCandidates(platform, env.SHELL)) {
24 try {
25 // mise's record of the PATH entries it added. It must travel with the
26 // shell's PATH, or `mise x` can rebuild PATH behind a same-named wrapper
27 // (`exec mise x claude -- claude`) that then re-runs itself forever.
28 shellEnvironment = readEnvironmentFromLoginShell(shell, ["PATH", "__MISE_DIFF"]);
29 } catch (error) {
30 logPathHydrationWarning(`Failed to read PATH from login shell ${shell}.`, error);
31 }
32
33 if (shellEnvironment.PATH) break;
34 }
35
36 const shellPath = shellEnvironment.PATH;
37 const launchctlPath = platform === "darwin" && !shellPath ? readPathFromLaunchctl() : undefined;
38 const mergedPath = mergePathEntries(shellPath ?? launchctlPath, env.PATH, platform);
39 if (mergedPath) {
40 env.PATH = mergedPath;
41 }
42 if (shellPath && shellEnvironment.__MISE_DIFF) {
43 env.__MISE_DIFF = shellEnvironment.__MISE_DIFF;
44 }
45}
46
47export function hydratePosixHome(
48 env: NodeJS.ProcessEnv,
49 resolveHomeDir = () => NodeOS.userInfo().homedir,
50): void {
51 if ((env.HOME?.trim() ?? "").length > 0) return;
52
53 const homeDir = resolveHomeDir();
54 if (homeDir.length > 0) {
55 env.HOME = homeDir;
56 }
57}
58
59export const fixPath = Effect.fn("fixPath")(function* (options?: {
60 readonly shellEnvironmentPrepared?: boolean | undefined;
61}): Effect.fn.Return<void, never, FileSystem.FileSystem | Path.Path> {
62 const platform = yield* HostProcess.Platform;
63 const env = yield* HostProcess.Environment;
64
65 if (platform === "win32") {
66 const repairedEnvironment = yield* resolveWindowsEnvironment(env).pipe(
67 Effect.catchDefect((defect) =>
68 Effect.sync(() => {
69 logPathHydrationWarning("Failed to hydrate PATH from the user environment.", defect);
70 return {} as Partial<NodeJS.ProcessEnv>;
71 }),
72 ),
73 );
74 for (const [key, value] of Object.entries(repairedEnvironment)) {
75 if (value === undefined) continue;
76 if (key === "PATH") {
77 setWindowsPath(env, value);
78 } else {
79 env[key] = value;
80 }
81 }
82 return;
83 }
84
85 if (platform !== "darwin" && platform !== "linux") return;
86
87 yield* Effect.sync(() => hydratePosixHome(env)).pipe(
88 Effect.catchDefect((defect) =>
89 Effect.sync(() => {
90 logPathHydrationWarning("Failed to hydrate HOME from the user account.", defect);
91 }),
92 ),
93 );
94 if (options?.shellEnvironmentPrepared === true) return;
95 yield* Effect.sync(() => hydratePosixPath(env, platform)).pipe(
96 Effect.catchDefect((defect) =>
97 Effect.sync(() => {
98 logPathHydrationWarning("Failed to hydrate PATH from the user environment.", defect);
99 }),
100 ),
101 );
102});
103
104export const expandHomePath = Effect.fn(function* (input: string) {
105 const { join } = yield* Path.Path;
106 const home = yield* HostProcess.HomeDirectory;
107 if (input === "~") {
108 return home;
109 }
110 if (input.startsWith("~/") || input.startsWith("~\\")) {
111 return join(home, input.slice(2));
112 }
113 return input;
114});
115
116export const resolveBaseDir = Effect.fn(function* (raw: string | undefined) {
117 const { join, resolve } = yield* Path.Path;
118 if (!raw || raw.trim().length === 0) {
119 return join(yield* HostProcess.HomeDirectory, ".t3");
120 }
121 return resolve(yield* expandHomePath(raw.trim()));
122});