11import * as HostProcess from "@t3tools/shared/HostProcess" ;
22import {
3+ buildEnvironmentCaptureCommand ,
4+ extractEnvironmentValues ,
35 listLoginShellCandidates ,
46 mergePathEntries ,
57 readEnvironmentFromLoginShell ,
68 readPathFromLaunchctl ,
79 resolveWindowsEnvironment ,
810 setWindowsPath ,
911} from "@t3tools/shared/shell" ;
12+ import * as Duration from "effect/Duration" ;
1013import * as Effect from "effect/Effect" ;
1114import * as FileSystem from "effect/FileSystem" ;
15+ import * as Option from "effect/Option" ;
1216import * as Path from "effect/Path" ;
17+ import * as Stream from "effect/Stream" ;
18+ import * as ChildProcess from "effect/process/ChildProcess" ;
19+ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
1320// @effect -diagnostics-next-line nodeBuiltinImport:off -- the shared login shell probe runs synchronously through execFileSync.
1421import * as NodeChildProcess from "node:child_process" ;
1522import * as NodeOS from "node:os" ;
@@ -20,30 +27,49 @@ function logPathHydrationWarning(message: string, error?: unknown): void {
2027 ) ;
2128}
2229
23- // mise's record of the PATH entries it added. It must travel with the
24- // shell's PATH, or `mise x` can rebuild PATH behind a same-named wrapper
25- // (`exec mise x claude -- claude`) that then re-runs itself forever.
26- const LOGIN_SHELL_ENVIRONMENT_NAMES = [ "PATH" , "__MISE_DIFF" , "JAVA_HOME" ] ;
30+ // __MISE_DIFF is mise's record of the PATH entries it added. It must travel
31+ // with the shell's PATH, or `mise x` can rebuild PATH behind a same-named
32+ // wrapper (`exec mise x claude -- claude`) that then re-runs itself forever.
33+ const LOGIN_SHELL_ENV_NAMES = [ "PATH" , "__MISE_DIFF" , "JAVA_HOME" ] as const ;
34+ const LOGIN_SHELL_TIMEOUT = Duration . seconds ( 5 ) ;
2735const SLOW_LOGIN_SHELL_TIMEOUT_MS = 20_000 ;
2836
2937function readSlowLoginShellEnvironment ( shell : string ) : Partial < Record < string , string > > {
30- return readEnvironmentFromLoginShell (
31- shell ,
32- LOGIN_SHELL_ENVIRONMENT_NAMES ,
33- ( file , args , options ) =>
34- NodeChildProcess . execFileSync ( file , args , {
35- ...options ,
36- timeout : SLOW_LOGIN_SHELL_TIMEOUT_MS ,
37- } ) ,
38+ return readEnvironmentFromLoginShell ( shell , LOGIN_SHELL_ENV_NAMES , ( file , args , options ) =>
39+ NodeChildProcess . execFileSync ( file , args , {
40+ ...options ,
41+ timeout : SLOW_LOGIN_SHELL_TIMEOUT_MS ,
42+ } ) ,
3843 ) ;
3944}
4045
46+ function applyLoginShellEnvironment (
47+ env : NodeJS . ProcessEnv ,
48+ platform : NodeJS . Platform ,
49+ shellEnvironment : Partial < Record < string , string > > ,
50+ fallbackPath ?: string ,
51+ ) : void {
52+ const shellPath = shellEnvironment . PATH ;
53+ const mergedPath = mergePathEntries ( shellPath ?? fallbackPath , env . PATH , platform ) ;
54+ if ( mergedPath ) {
55+ env . PATH = mergedPath ;
56+ }
57+ if ( shellPath && shellEnvironment . __MISE_DIFF ) {
58+ env . __MISE_DIFF = shellEnvironment . __MISE_DIFF ;
59+ }
60+ // Android SDK tools are Java programs. A keg-only JDK (Homebrew's openjdk)
61+ // is only reachable through the JAVA_HOME the shell profile sets.
62+ if ( ! env . JAVA_HOME && shellEnvironment . JAVA_HOME ) {
63+ env . JAVA_HOME = shellEnvironment . JAVA_HOME ;
64+ }
65+ }
66+
4167function hydratePosixPath ( env : NodeJS . ProcessEnv , platform : NodeJS . Platform ) : void {
4268 let shellEnvironment : Partial < Record < string , string > > = { } ;
4369 let waitedForSlowShell = false ;
4470 for ( const shell of listLoginShellCandidates ( platform , env . SHELL ) ) {
4571 try {
46- shellEnvironment = readEnvironmentFromLoginShell ( shell , LOGIN_SHELL_ENVIRONMENT_NAMES ) ;
72+ shellEnvironment = readEnvironmentFromLoginShell ( shell , LOGIN_SHELL_ENV_NAMES ) ;
4773 } catch ( error ) {
4874 logPathHydrationWarning ( `Failed to read PATH from login shell ${ shell } .` , error ) ;
4975 // A profile can be slow only while the machine boots. Wait for it once,
@@ -65,20 +91,9 @@ function hydratePosixPath(env: NodeJS.ProcessEnv, platform: NodeJS.Platform): vo
6591 if ( shellEnvironment . PATH ) break ;
6692 }
6793
68- const shellPath = shellEnvironment . PATH ;
69- const launchctlPath = platform === "darwin" && ! shellPath ? readPathFromLaunchctl ( ) : undefined ;
70- const mergedPath = mergePathEntries ( shellPath ?? launchctlPath , env . PATH , platform ) ;
71- if ( mergedPath ) {
72- env . PATH = mergedPath ;
73- }
74- if ( shellPath && shellEnvironment . __MISE_DIFF ) {
75- env . __MISE_DIFF = shellEnvironment . __MISE_DIFF ;
76- }
77- // Android SDK tools are Java programs. A keg-only JDK (Homebrew's openjdk)
78- // is only reachable through the JAVA_HOME the shell profile sets.
79- if ( ! env . JAVA_HOME && shellEnvironment . JAVA_HOME ) {
80- env . JAVA_HOME = shellEnvironment . JAVA_HOME ;
81- }
94+ const launchctlPath =
95+ platform === "darwin" && ! shellEnvironment . PATH ? readPathFromLaunchctl ( ) : undefined ;
96+ applyLoginShellEnvironment ( env , platform , shellEnvironment , launchctlPath ) ;
8297}
8398
8499export function hydratePosixHome (
@@ -140,6 +155,68 @@ export const fixPath = Effect.fn("fixPath")(function* (options?: {
140155 ) ;
141156} ) ;
142157
158+ const readLoginShellOutput = Effect . fn ( "readLoginShellOutput" ) (
159+ function * ( shell : string ) {
160+ const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
161+ const child = yield * spawner . spawn (
162+ ChildProcess . make (
163+ shell ,
164+ [ "-ilc" , buildEnvironmentCaptureCommand ( shell , LOGIN_SHELL_ENV_NAMES ) ] ,
165+ {
166+ stdin : "ignore" ,
167+ stdout : "pipe" ,
168+ stderr : "ignore" ,
169+ // A profile that traps SIGTERM would otherwise hold the scope open
170+ // past the timeout while cleanup waits for the exit.
171+ forceKillAfter : "1 second" ,
172+ } ,
173+ ) ,
174+ ) ;
175+ const [ stdout , exitCode ] = yield * Effect . all (
176+ [ Stream . mkString ( Stream . decodeText ( child . stdout ) ) , child . exitCode ] ,
177+ { concurrency : 2 } ,
178+ ) ;
179+ if ( exitCode === 0 ) return stdout ;
180+ yield * Effect . logWarning (
181+ `Login shell ${ shell } exited with code ${ exitCode } while reading PATH.` ,
182+ ) ;
183+ return "" ;
184+ } ,
185+ Effect . scoped ,
186+ Effect . timeoutOption ( LOGIN_SHELL_TIMEOUT ) ,
187+ ) ;
188+
189+ // Runs even when the desktop prepared the environment: that capture was taken
190+ // once at launch. Only the user's own shell counts; a fallback shell's PATH is
191+ // not their environment. Failure leaves PATH alone.
192+ export const refreshPathFromLoginShell = Effect . fn ( "refreshPathFromLoginShell" ) ( function * ( ) {
193+ const platform = yield * HostProcess . Platform ;
194+ if ( platform !== "darwin" && platform !== "linux" ) return ;
195+ const env = yield * HostProcess . Environment ;
196+ const [ shell ] = listLoginShellCandidates ( platform , env . SHELL ) ;
197+ if ( ! shell ) return ;
198+
199+ const output = yield * readLoginShellOutput ( shell ) . pipe (
200+ Effect . catchCause ( ( cause ) =>
201+ Effect . logWarning ( `Failed to read PATH from login shell ${ shell } .` , cause ) . pipe (
202+ Effect . as ( Option . some ( "" ) ) ,
203+ ) ,
204+ ) ,
205+ ) ;
206+ if ( Option . isNone ( output ) ) {
207+ yield * Effect . logWarning (
208+ `Timed out reading PATH from login shell ${ shell } after ${ Duration . format ( LOGIN_SHELL_TIMEOUT ) } ; PATH is unchanged.` ,
209+ ) ;
210+ return ;
211+ }
212+ const shellEnvironment = extractEnvironmentValues ( output . value , LOGIN_SHELL_ENV_NAMES ) ;
213+ if ( shellEnvironment . PATH ) {
214+ applyLoginShellEnvironment ( env , platform , shellEnvironment ) ;
215+ } else {
216+ yield * Effect . logWarning ( `Login shell ${ shell } printed no PATH; PATH is unchanged.` ) ;
217+ }
218+ } ) ;
219+
143220export const expandHomePath = Effect . fn ( function * ( input : string ) {
144221 const { join } = yield * Path . Path ;
145222 const home = yield * HostProcess . HomeDirectory ;
0 commit comments