@@ -270,8 +270,16 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
270270 const environment = yield * HostProcess . Environment ;
271271 const workingDirectory = yield * HostProcess . WorkingDirectory ;
272272 const hostPlatform = yield * HostProcess . Platform ;
273- const sdk = yield * androidSdk ;
274- const hostEnvironment = deviceHostEnvironment ( environment , sdk . root , hostPlatform , path ) ;
273+ // Resolved per use: the desktop shell snapshot can select another SDK after the host exists.
274+ const currentSdk = androidSdk . pipe (
275+ Effect . provideService ( FileSystem . FileSystem , fs ) ,
276+ Effect . provideService ( Path . Path , path ) ,
277+ Effect . provideService ( HostProcess . Environment , environment ) ,
278+ Effect . provideService ( HostProcess . Platform , hostPlatform ) ,
279+ ) ;
280+ const currentHostEnvironment = currentSdk . pipe (
281+ Effect . map ( ( sdk ) => deviceHostEnvironment ( environment , sdk . root , hostPlatform , path ) ) ,
282+ ) ;
275283 const startLock = yield * Semaphore . make ( 1 ) ;
276284 const runningRef = yield * Ref . make < RunningHost | null > ( null ) ;
277285 const restartDelayRef = yield * Ref . make ( 0 ) ;
@@ -433,7 +441,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
433441 shell : false ,
434442 stdout : "pipe" ,
435443 stderr : "pipe" ,
436- env : yield * hubEnvironment ( hostEnvironment ) . pipe (
444+ env : yield * hubEnvironment ( yield * currentHostEnvironment ) . pipe (
437445 Effect . provideService ( FileSystem . FileSystem , fs ) ,
438446 Effect . provideService ( Path . Path , path ) ,
439447 Effect . provideService ( ProcessRunner . ProcessRunner , runner ) ,
@@ -536,7 +544,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
536544 yield * fs . makeDirectory ( stateDir , { recursive : true } ) . pipe ( Effect . ignore ) ;
537545 const existing = yield * readDaemonFile ( ) . pipe ( Effect . option ) ;
538546 const daemonEnvironment : NodeJS . ProcessEnv = {
539- ...hostEnvironment ,
547+ ...( yield * currentHostEnvironment ) ,
540548 AGENT_DEVICE_STATE_DIR : stateDir ,
541549 AGENT_DEVICE_DAEMON_SERVER_MODE : "http" ,
542550 // Disable the five-minute idle timeout; the server owns shutdown.
@@ -643,6 +651,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
643651 AGENT_DEVICE_VERSION ,
644652 ] ) ;
645653 if ( ! entryPath ) return ;
654+ const hostEnvironment = yield * currentHostEnvironment ;
646655 const nodePath = yield * resolveNodeExecutable ( "Local device support" , hostEnvironment ) . pipe (
647656 Effect . provideService ( HostProcess . Platform , hostPlatform ) ,
648657 ) ;
@@ -675,7 +684,10 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
675684 const alive = yield * running . hub . child . isRunning . pipe ( Effect . orElseSucceed ( ( ) => false ) ) ;
676685 if ( alive ) return running ;
677686 }
678- const nodePath = yield * resolveNodeExecutable ( "Local device support" , hostEnvironment ) . pipe (
687+ const nodePath = yield * resolveNodeExecutable (
688+ "Local device support" ,
689+ yield * currentHostEnvironment ,
690+ ) . pipe (
679691 Effect . provideService ( FileSystem . FileSystem , fs ) ,
680692 Effect . provideService ( Path . Path , path ) ,
681693 Effect . provideService ( HostProcess . Platform , hostPlatform ) ,
@@ -807,34 +819,35 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
807819 } ;
808820
809821 const run : DeviceHost . DeviceHostReady [ "run" ] = ( command , args , options ) =>
810- runner
811- . run ( {
812- command :
813- command === "emulator" && sdk . root
814- ? path . join (
815- sdk . root ,
816- "emulator" ,
817- hostPlatform === "win32" ? "emulator.exe" : "emulator" ,
818- )
819- : command ,
820- args,
821- cwd : workingDirectory ,
822- env : hostEnvironment ,
823- timeout : Duration . millis ( options ?. timeoutMs ?? 20_000 ) ,
824- timeoutBehavior : "timedOutResult" ,
825- ...( options ?. stdin === undefined ? { } : { stdin : options . stdin } ) ,
826- ...( options ?. maxOutputBytes === undefined
827- ? { }
828- : { maxOutputBytes : options . maxOutputBytes } ) ,
829- } )
830- . pipe (
831- Effect . map ( ( result ) => ( {
832- stdout : result . stdout ,
833- stderr : result . stderr ,
834- code : result . timedOut ? 124 : Number ( result . code ?? 127 ) ,
835- } ) ) ,
836- Effect . catch ( ( cause ) => Effect . succeed ( { stdout : "" , stderr : String ( cause ) , code : 127 } ) ) ,
837- ) ;
822+ currentSdk . pipe (
823+ Effect . flatMap ( ( sdk ) =>
824+ runner . run ( {
825+ command :
826+ command === "emulator" && sdk . root
827+ ? path . join (
828+ sdk . root ,
829+ "emulator" ,
830+ hostPlatform === "win32" ? "emulator.exe" : "emulator" ,
831+ )
832+ : command ,
833+ args,
834+ cwd : workingDirectory ,
835+ env : deviceHostEnvironment ( environment , sdk . root , hostPlatform , path ) ,
836+ timeout : Duration . millis ( options ?. timeoutMs ?? 20_000 ) ,
837+ timeoutBehavior : "timedOutResult" ,
838+ ...( options ?. stdin === undefined ? { } : { stdin : options . stdin } ) ,
839+ ...( options ?. maxOutputBytes === undefined
840+ ? { }
841+ : { maxOutputBytes : options . maxOutputBytes } ) ,
842+ } ) ,
843+ ) ,
844+ Effect . map ( ( result ) => ( {
845+ stdout : result . stdout ,
846+ stderr : result . stderr ,
847+ code : result . timedOut ? 124 : Number ( result . code ?? 127 ) ,
848+ } ) ) ,
849+ Effect . catch ( ( cause ) => Effect . succeed ( { stdout : "" , stderr : String ( cause ) , code : 127 } ) ) ,
850+ ) ;
838851
839852 const toReady = ( running : RunningHost ) : DeviceHost . DeviceHostReady => ( {
840853 hub : { origin : running . hub . origin } satisfies DeviceHost . DeviceHubEndpoint ,
0 commit comments