11import * as NodeServices from "@effect/platform-node/NodeServices" ;
22import { assert , describe , it } from "@effect/vitest" ;
33import * as Effect from "effect/Effect" ;
4+ import * as Exit from "effect/Exit" ;
5+ import * as Fiber from "effect/Fiber" ;
6+ import * as TestClock from "effect/testing/TestClock" ;
47import * as Layer from "effect/Layer" ;
58import * as Logger from "effect/Logger" ;
9+ import * as Option from "effect/Option" ;
10+ import type * as Tracer from "effect/Tracer" ;
611import * as PlatformError from "effect/PlatformError" ;
712import * as Schema from "effect/Schema" ;
813import * as Sink from "effect/Sink" ;
@@ -67,7 +72,7 @@ function withProcessEnv<A, E, R>(
6772function runShellEnvironment ( input : {
6873 readonly env : NodeJS . ProcessEnv ;
6974 readonly platform : NodeJS . Platform ;
70- readonly handler : ( command : ChildProcess . Command ) => string ;
75+ readonly handler : ( command : ChildProcess . Command ) => string | Effect . Effect < string > ;
7176 readonly failure ?: PlatformError . PlatformError ;
7277} ) {
7378 const environmentLayer = Layer . succeed (
@@ -79,9 +84,13 @@ function runShellEnvironment(input: {
7984 const spawnerLayer = Layer . succeed (
8085 ChildProcessSpawner . ChildProcessSpawner ,
8186 ChildProcessSpawner . make ( ( command ) =>
82- input . failure === undefined
83- ? Effect . succeed ( makeProcess ( input . handler ( command ) ) )
84- : Effect . fail ( input . failure ) ,
87+ Effect . suspend ( ( ) => {
88+ if ( input . failure !== undefined ) return Effect . fail ( input . failure ) ;
89+ const output = input . handler ( command ) ;
90+ return ( Effect . isEffect ( output ) ? output : Effect . succeed ( output ) ) . pipe (
91+ Effect . map ( makeProcess ) ,
92+ ) ;
93+ } ) ,
8594 ) ,
8695 ) ;
8796
@@ -129,6 +138,75 @@ describe("DesktopShellEnvironment", () => {
129138 } ) ,
130139 ) ;
131140
141+ it . effect ( "retries a timed-out login shell before starting with an incomplete PATH" , ( ) =>
142+ Effect . gen ( function * ( ) {
143+ const env = { SHELL : "/bin/zsh" , PATH : "/usr/bin:/bin:/usr/sbin:/sbin" } ;
144+ let attempts = 0 ;
145+ const capture = yield * runShellEnvironment ( {
146+ env,
147+ platform : "darwin" ,
148+ handler : ( command ) => {
149+ if ( command . _tag !== "StandardCommand" || command . command === "/bin/launchctl" ) return "" ;
150+ attempts ++ ;
151+ return attempts === 1 ? Effect . never : envOutput ( { PATH : "/opt/homebrew/bin:/usr/bin" } ) ;
152+ } ,
153+ } ) . pipe ( Effect . forkChild ) ;
154+
155+ yield * TestClock . adjust ( "5 seconds" ) ;
156+ yield * Fiber . join ( capture ) ;
157+ assert . equal ( attempts , 2 ) ;
158+ assert . equal ( env . PATH , "/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin" ) ;
159+ } ) ,
160+ ) ;
161+
162+ it . effect ( "fails capture when both login shell and launchctl provide no PATH" , ( ) =>
163+ Effect . gen ( function * ( ) {
164+ const env = { SHELL : "/bin/zsh" , PATH : "/usr/bin" } ;
165+ const spans = new Map < string , Tracer . Span > ( ) ;
166+ const result = yield * runShellEnvironment ( {
167+ env,
168+ platform : "darwin" ,
169+ handler : ( ) =>
170+ Effect . gen ( function * ( ) {
171+ let span : Tracer . AnySpan = yield * Effect . currentSpan . pipe ( Effect . orDie ) ;
172+ while ( span . _tag === "Span" ) {
173+ spans . set ( span . name , span ) ;
174+ if ( Option . isNone ( span . parent ) ) break ;
175+ span = span . parent . value ;
176+ }
177+ return "" ;
178+ } ) ,
179+ } ) . pipe ( Effect . exit ) ;
180+ assert . isTrue ( Exit . isFailure ( result ) ) ;
181+ for ( const name of [ "installPosixEnvironment" , "installIntoProcess" ] ) {
182+ const status = spans . get ( `desktop.shellEnvironment.${ name } ` ) ?. status ;
183+ assert . equal ( status ?. _tag , "Ended" ) ;
184+ if ( status ?. _tag === "Ended" ) assert . isTrue ( Exit . isFailure ( status . exit ) ) ;
185+ }
186+ assert . equal ( env . PATH , "/usr/bin" ) ;
187+ } ) ,
188+ ) ;
189+
190+ it . effect ( "bounds repeated timeouts and still tries launchctl" , ( ) =>
191+ Effect . gen ( function * ( ) {
192+ const env = { SHELL : "/bin/zsh" , PATH : "/usr/bin" } ;
193+ const commands : string [ ] = [ ] ;
194+ const capture = yield * runShellEnvironment ( {
195+ env,
196+ platform : "darwin" ,
197+ handler : ( command ) => {
198+ if ( command . _tag !== "StandardCommand" ) return "" ;
199+ commands . push ( command . command ) ;
200+ return command . command === "/bin/launchctl" ? "" : Effect . never ;
201+ } ,
202+ } ) . pipe ( Effect . exit , Effect . forkChild ) ;
203+
204+ yield * TestClock . adjust ( "10 seconds" ) ;
205+ assert . isTrue ( Exit . isFailure ( yield * Fiber . join ( capture ) ) ) ;
206+ assert . deepEqual ( commands , [ "/bin/zsh" , "/bin/zsh" , "/bin/launchctl" ] ) ;
207+ } ) ,
208+ ) ;
209+
132210 it . effect ( "preserves inherited POSIX values when present" , ( ) =>
133211 Effect . gen ( function * ( ) {
134212 const env : NodeJS . ProcessEnv = {
@@ -294,7 +372,13 @@ describe("DesktopShellEnvironment", () => {
294372 } ,
295373 } ) ;
296374
297- assert . deepEqual ( commands , [ "/opt/homebrew/bin/nu" , "/bin/zsh" , "/bin/launchctl" ] ) ;
375+ assert . deepEqual ( commands , [
376+ "/opt/homebrew/bin/nu" ,
377+ "/opt/homebrew/bin/nu" ,
378+ "/bin/zsh" ,
379+ "/bin/zsh" ,
380+ "/bin/launchctl" ,
381+ ] ) ;
298382 assert . equal ( env . PATH , "/opt/homebrew/bin:/usr/bin" ) ;
299383 } ) ,
300384 ) ;
@@ -429,12 +513,13 @@ describe("DesktopShellEnvironment", () => {
429513 handler : ( ) => "" ,
430514 failure : cause ,
431515 } ) . pipe (
516+ Effect . exit ,
432517 Effect . andThen (
433518 Effect . sync ( ( ) => {
434519 const errors = messages
435520 . flatMap ( ( message ) => ( Array . isArray ( message ) ? message : [ message ] ) )
436521 . filter ( isDesktopShellEnvironmentCommandError ) ;
437- assert . lengthOf ( errors , 1 ) ;
522+ assert . lengthOf ( errors , 2 ) ;
438523 assert . equal ( errors [ 0 ] ?. probe , "login-shell" ) ;
439524 assert . equal ( errors [ 0 ] ?. executable , "bash" ) ;
440525 assert . equal ( errors [ 0 ] ?. argumentCount , 2 ) ;
0 commit comments