@@ -117,6 +117,8 @@ const LOCALE_ENV_NAMES = ["LANG", "LC_ALL", "LC_CTYPE"] as const;
117117const FALLBACK_LC_CTYPE = "en_US.UTF-8" ;
118118const WINDOWS_SHELL_CANDIDATES = [ "pwsh.exe" , "powershell.exe" ] as const ;
119119const LOGIN_SHELL_TIMEOUT = Duration . seconds ( 5 ) ;
120+ // A profile can be slow only while the machine boots; one longer wait covers it.
121+ const SLOW_LOGIN_SHELL_TIMEOUT = Duration . seconds ( 20 ) ;
120122const LAUNCHCTL_TIMEOUT = Duration . seconds ( 2 ) ;
121123const PROCESS_TERMINATE_GRACE = Duration . seconds ( 1 ) ;
122124
@@ -261,7 +263,11 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
261263 readonly env : NodeJS . ProcessEnv ;
262264 readonly timeout : Duration . Duration ;
263265 readonly shell ?: boolean ;
264- } ) : Effect . fn . Return < string , never , ChildProcessSpawner . ChildProcessSpawner > {
266+ } ) : Effect . fn . Return <
267+ string ,
268+ DesktopShellEnvironmentCommandTimeoutError ,
269+ ChildProcessSpawner . ChildProcessSpawner
270+ > {
265271 const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
266272 const fiber = yield * Effect . scoped (
267273 Effect . gen ( function * ( ) {
@@ -317,29 +323,42 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
317323 return output . value ;
318324 }
319325
320- const error = new DesktopShellEnvironmentCommandTimeoutError ( {
326+ return yield * new DesktopShellEnvironmentCommandTimeoutError ( {
321327 probe : input . probe ,
322328 executable : executableName ( input . command ) ,
323329 argumentCount : input . args . length ,
324330 timeoutMs : Duration . toMillis ( input . timeout ) ,
325331 } ) ;
326- yield * logShellEnvironmentCommandError ( error ) ;
327- return "" ;
328332} ) ;
329333
334+ const orEmptyOnTimeout = < R > (
335+ effect : Effect . Effect < string , DesktopShellEnvironmentCommandTimeoutError , R > ,
336+ ) : Effect . Effect < string , never , R > =>
337+ effect . pipe (
338+ Effect . catchTags ( {
339+ DesktopShellEnvironmentCommandTimeoutError : ( error ) =>
340+ logShellEnvironmentCommandError ( error ) . pipe ( Effect . as ( "" ) ) ,
341+ } ) ,
342+ ) ;
343+
330344const readLoginShellEnvironment = (
331345 shell : string ,
332346 names : ReadonlyArray < string > ,
333347 env : NodeJS . ProcessEnv ,
334- ) : Effect . Effect < EnvironmentPatch , never , ChildProcessSpawner . ChildProcessSpawner > =>
348+ timeout : Duration . Duration ,
349+ ) : Effect . Effect <
350+ EnvironmentPatch ,
351+ DesktopShellEnvironmentCommandTimeoutError ,
352+ ChildProcessSpawner . ChildProcessSpawner
353+ > =>
335354 names . length === 0
336355 ? Effect . succeed ( { } )
337356 : runCommandOutput ( {
338357 probe : "login-shell" ,
339358 command : shell ,
340359 args : [ "-ilc" , buildEnvironmentCaptureCommand ( shell , names ) ] ,
341360 env,
342- timeout : LOGIN_SHELL_TIMEOUT ,
361+ timeout,
343362 } ) . pipe ( Effect . map ( ( output ) => extractEnvironment ( output , names ) ) ) ;
344363
345364const readLaunchctlPath = ( env : NodeJS . ProcessEnv ) =>
@@ -349,7 +368,7 @@ const readLaunchctlPath = (env: NodeJS.ProcessEnv) =>
349368 command : "/bin/launchctl" ,
350369 args : [ "getenv" , "PATH" ] ,
351370 timeout : LAUNCHCTL_TIMEOUT ,
352- } ) . pipe ( Effect . map ( trimNonEmpty ) ) ;
371+ } ) . pipe ( orEmptyOnTimeout , Effect . map ( trimNonEmpty ) ) ;
353372
354373const readWindowsEnvironment = Effect . fn ( "desktop.shellEnvironment.readWindowsEnvironment" ) (
355374 function * (
@@ -374,7 +393,7 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEn
374393 args,
375394 env,
376395 timeout : LOGIN_SHELL_TIMEOUT ,
377- } ) ;
396+ } ) . pipe ( orEmptyOnTimeout ) ;
378397 const environment = extractEnvironment ( output , names ) ;
379398 if ( Object . keys ( environment ) . length > 0 ) {
380399 return environment ;
@@ -429,10 +448,27 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
429448 const userShells = [ config . env . SHELL ?. trim ( ) , readUserLoginShell ( ) ] ;
430449
431450 let answeredBy : string | undefined ;
451+ let waitedForSlowShell = false ;
432452 for ( const shell of listLoginShellCandidates ( config . platform , config . env . SHELL ) ) {
453+ const read = ( timeout : Duration . Duration ) =>
454+ readLoginShellEnvironment ( shell , LOGIN_SHELL_ENV_NAMES , config . env , timeout ) ;
433455 Object . assign (
434456 shellEnvironment ,
435- yield * readLoginShellEnvironment ( shell , LOGIN_SHELL_ENV_NAMES , config . env ) ,
457+ yield * read ( LOGIN_SHELL_TIMEOUT ) . pipe (
458+ Effect . catchTags ( {
459+ DesktopShellEnvironmentCommandTimeoutError : ( error ) => {
460+ const retry = ! waitedForSlowShell ;
461+ waitedForSlowShell = true ;
462+ return logShellEnvironmentCommandError ( error ) . pipe (
463+ Effect . andThen ( retry ? read ( SLOW_LOGIN_SHELL_TIMEOUT ) : Effect . succeed ( { } ) ) ,
464+ ) ;
465+ } ,
466+ } ) ,
467+ Effect . catchTags ( {
468+ DesktopShellEnvironmentCommandTimeoutError : ( error ) =>
469+ logShellEnvironmentCommandError ( error ) . pipe ( Effect . as ( { } ) ) ,
470+ } ) ,
471+ ) ,
436472 ) ;
437473 if ( shellEnvironment . PATH ) {
438474 answeredBy = shell ;
0 commit comments