@@ -133,7 +133,12 @@ const serve = (tries) => {
133133serve(40);
134134` ;
135135
136- // Runs the remote half of an SSH command (`sh -l -s -- <key>`) as a local shell
136+ // The launch's POSIX bootstrap without the account login shell around it, so the
137+ // test machine's login profile cannot move HOME.
138+ const launchStateKey = ( command : ChildProcess . Command ) =>
139+ / e x e c s h - s - - ( \S + ) " / u. exec ( commandArgs ( command ) . join ( " " ) ) ?. [ 1 ] ;
140+
141+ // Runs the remote half of an SSH launch as a local shell
137142// whose HOME is the given directory. Every launch after the first starts once
138143// a server is running (or after 10s), which lines up the race deterministically.
139144const localRemoteShellLayer = ( home : string ) =>
@@ -151,7 +156,7 @@ const localRemoteShellLayer = (home: string) =>
151156 return spawner . spawn (
152157 ChildProcess . make (
153158 "sh" ,
154- [ "-c" , `${ gate } exec sh -s -- "$1"` , "sh" , ... commandArgs ( command ) . slice ( - 1 ) ] ,
159+ [ "-c" , `${ gate } exec sh -s -- "$1"` , "sh" , launchStateKey ( command ) ?? "" ] ,
155160 {
156161 ...command . options ,
157162 env : { ...command . options . env , HOME : home } ,
@@ -296,12 +301,8 @@ describe("ssh tunnel scripts", () => {
296301 username : "julius" ,
297302 port : 2222 ,
298303 } as const ;
299- const spawnedCommands : Array < ReadonlyArray < string > > = [ ] ;
300- const spawner = ChildProcessSpawner . make ( ( command ) =>
301- Effect . sync ( ( ) => {
302- spawnedCommands . push ( commandArgs ( command ) ) ;
303- return makeSuccessfulProcess ( 'loaded nvm default\n{"remotePort":3774}\n' ) ;
304- } ) ,
304+ const spawner = ChildProcessSpawner . make ( ( ) =>
305+ Effect . succeed ( makeSuccessfulProcess ( 'loaded nvm default\n{"remotePort":3774}\n' ) ) ,
305306 ) ;
306307 const layerSpawner = Layer . succeed ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ;
307308 const layerProcess = Layer . merge (
@@ -312,10 +313,112 @@ describe("ssh tunnel scripts", () => {
312313 return Effect . gen ( function * ( ) {
313314 const result = yield * SshTunnel . launchOrReuseRemoteServer ( target , undefined , ARCHIVE ) ;
314315 assert . equal ( result . remotePort , 3774 ) ;
315- assert . deepEqual ( spawnedCommands [ 0 ] ?. slice ( - 5 , - 1 ) , [ "sh" , "-l" , "-s" , "--" ] ) ;
316316 } ) . pipe ( Effect . provide ( layerProcess ) ) ;
317317 } ) ;
318318
319+ it . effect . skipIf ( HostProcess . Platform . defaultValue ( ) === "win32" ) (
320+ "loads login settings without changing the POSIX bootstrap or its state key" ,
321+ ( ) =>
322+ Effect . gen ( function * ( ) {
323+ const fs = yield * FileSystem . FileSystem ;
324+ const directory = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-ssh-login-" } ) ;
325+ const startupFile = `${ directory } /bash-env` ;
326+ const bash = `${ directory } /bash` ;
327+ // System profiles can reset HOME. Suppress profile reads while BASH_ENV
328+ // checks that the launcher still requests Bash login mode.
329+ yield * fs . writeFileString ( bash , '#!/bin/sh\nexec /bin/bash --noprofile "$@"\n' ) ;
330+ yield * fs . chmod ( bash , 0o755 ) ;
331+ yield * fs . writeFileString (
332+ startupFile ,
333+ "if shopt -q login_shell; then export T3_SSH_LOGIN_ENV=loaded; fi\n" ,
334+ ) ;
335+ const target = { alias : "login-test" , hostname : "login-test" , username : null , port : null } ;
336+ let remoteCommand = "" ;
337+ const capture = ChildProcessSpawner . make ( ( command ) =>
338+ Effect . sync ( ( ) => {
339+ const args = commandArgs ( command ) ;
340+ remoteCommand = args . slice ( args . indexOf ( target . alias ) + 1 ) . join ( " " ) ;
341+ return makeSuccessfulProcess ( '{"remotePort":3774}\n' ) ;
342+ } ) ,
343+ ) ;
344+ yield * SshTunnel . launchOrReuseRemoteServer ( target , undefined , ARCHIVE ) . pipe (
345+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , capture ) ,
346+ ) ;
347+ yield * fs . writeFileString (
348+ `${ directory } /.zprofile` ,
349+ "if [[ -o login ]]; then export T3_SSH_LOGIN_ENV=loaded; fi\n" ,
350+ ) ;
351+ const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
352+ const cases = [
353+ { shell : bash , outerShell : "/bin/sh" , expected : "loaded" } ,
354+ { shell : "/bin/sh" , outerShell : "/bin/sh" , expected : "missing" } ,
355+ { shell : "" , outerShell : "/bin/sh" , expected : "missing" } ,
356+ { shell : "/unavailable/csh" , outerShell : "/bin/sh" , expected : "missing" } ,
357+ { shell : "/unavailable/tcsh" , outerShell : "/bin/sh" , expected : "missing" } ,
358+ { shell : "/unavailable/pwsh" , outerShell : "/bin/sh" , expected : "missing" } ,
359+ ] ;
360+ if ( yield * fs . exists ( "/bin/zsh" ) ) {
361+ cases . push ( { shell : "/bin/zsh" , outerShell : "/bin/zsh" , expected : "loaded" } ) ;
362+ }
363+ if ( yield * fs . exists ( "/bin/tcsh" ) ) {
364+ cases . push ( { shell : "/bin/tcsh" , outerShell : "/bin/tcsh" , expected : "missing" } ) ;
365+ }
366+ const environment = yield * HostProcess . Environment ;
367+ for ( const folder of ( environment . PATH ?? "" ) . split ( ":" ) ) {
368+ const nu = `${ folder } /nu` ;
369+ if ( folder && ( yield * fs . exists ( nu ) ) ) {
370+ cases . push ( { shell : nu , outerShell : nu , expected : "missing" } ) ;
371+ break ;
372+ }
373+ }
374+ for ( const { shell, outerShell, expected } of cases ) {
375+ const child = yield * spawner . spawn (
376+ ChildProcess . make ( outerShell , [ "-c" , remoteCommand ] , {
377+ env : {
378+ PATH : environment . PATH ?? "/usr/bin:/bin" ,
379+ HOME : directory ,
380+ XDG_CONFIG_HOME : directory ,
381+ SHELL : shell ,
382+ BASH_ENV : startupFile ,
383+ ZDOTDIR : directory ,
384+ T3_SSH_LOGIN_ENV : "" ,
385+ } ,
386+ stdin : {
387+ stream : Stream . make (
388+ new TextEncoder ( ) . encode ( 'printf "%s:%s" "${T3_SSH_LOGIN_ENV:-missing}" "$1"' ) ,
389+ ) ,
390+ endOnDone : true ,
391+ } ,
392+ } ) ,
393+ ) ;
394+ const [ stdout , stderr , exitCode ] = yield * Effect . all (
395+ [
396+ child . stdout . pipe (
397+ Stream . decodeText ( ) ,
398+ Stream . runFold (
399+ ( ) => "" ,
400+ ( a , b ) => a + b ,
401+ ) ,
402+ ) ,
403+ child . stderr . pipe (
404+ Stream . decodeText ( ) ,
405+ Stream . runFold (
406+ ( ) => "" ,
407+ ( a , b ) => a + b ,
408+ ) ,
409+ ) ,
410+ child . exitCode ,
411+ ] ,
412+ { concurrency : "unbounded" } ,
413+ ) ;
414+ assert . equal ( Number ( exitCode ) , 0 , stderr ) ;
415+ // First-run shell messages can precede the bootstrap response.
416+ const response = stdout . trim ( ) . split ( / \r ? \n / u) . at ( - 1 ) ?? "" ;
417+ assert . match ( response , new RegExp ( `^${ expected } :[a-f0-9]{16}$` ) , shell || "unset SHELL" ) ;
418+ }
419+ } ) . pipe ( Effect . provide ( Layer . merge ( NodeServices . layer , NodeCrypto . layer ) ) ) ,
420+ ) ;
421+
319422 it . effect ( "accepts pretty-printed pairing JSON after remote shell startup noise" , ( ) => {
320423 const target = {
321424 alias : "devbox" ,
@@ -795,7 +898,7 @@ describe("ssh tunnel scripts", () => {
795898 }
796899 return tunnel ;
797900 }
798- if ( args . some ( ( arg , index ) => arg === "-s" && args [ index + 1 ] === "-- ") ) {
901+ if ( args . includes ( "-c ") ) {
799902 if ( isTarget ) {
800903 launches += 1 ;
801904 remoteRunning = true ;
@@ -972,17 +1075,20 @@ server.listen(Number(flag("--port") ?? 0), "127.0.0.1", () => {
9721075 const fakeServer = `${ root } /fake-t3.cjs` ;
9731076 yield * fs . makeDirectory ( home , { recursive : true } ) ;
9741077 yield * fs . writeFileString ( fakeServer , FAKE_SERVER ) ;
975- // Runs the remote command locally under the temp HOME, without -l so the test
976- // machine's login profile cannot move HOME. setsid keeps a launched server alive
1078+ // Runs the remote command locally under the temp HOME. setsid keeps a launched server alive
9771079 // when the command's process group goes away, as sshd would.
9781080 const sshAsLocalShell = ChildProcessSpawner . make ( ( command ) => {
9791081 if ( command . _tag !== "StandardCommand" || command . command !== "ssh" ) {
9801082 return realSpawner . spawn ( command ) ;
9811083 }
9821084 if ( command . args . includes ( "-G" ) ) return Effect . succeed ( makeSuccessfulProcess ( "" ) ) ;
983- const remoteArgs = command . args . slice ( command . args . indexOf ( "--" ) + 2 ) ;
1085+ const stateKey = launchStateKey ( command ) ;
1086+ const remoteArgs =
1087+ stateKey === undefined
1088+ ? command . args . slice ( command . args . indexOf ( "--" ) + 2 )
1089+ : [ "sh" , "-s" , "--" , stateKey ] ;
9841090 return realSpawner . spawn (
985- ChildProcess . make ( "setsid" , [ "-w" , ...remoteArgs . filter ( ( arg ) => arg !== "-l" ) ] , {
1091+ ChildProcess . make ( "setsid" , [ "-w" , ...remoteArgs ] , {
9861092 ...command . options ,
9871093 env : { PATH : process . env . PATH ?? "" , HOME : home } ,
9881094 extendEnv : false ,
0 commit comments