@@ -1242,6 +1242,33 @@ const fn login_shell_flag(no_login_shell: bool) -> &'static str {
12421242 if no_login_shell { "-c" } else { "-lc" }
12431243}
12441244
1245+ /// Build the shell command for an SSH session using a shell that exists in the
1246+ /// sandbox image (minimal images such as Alpine ship only `/bin/sh`, not bash).
1247+ ///
1248+ /// `no_command_arg` is appended only when no explicit command is given: `-i`
1249+ /// for an interactive PTY session, or `None` for the non-PTY stdin path (a
1250+ /// bare shell already reads piped stdin line-by-line). With an explicit
1251+ /// command the login-shell flag is used per `no_login_shell`.
1252+ fn build_ssh_shell_command (
1253+ shell : & str ,
1254+ command : Option < String > ,
1255+ no_login_shell : bool ,
1256+ no_command_arg : Option < & str > ,
1257+ ) -> Command {
1258+ let mut cmd = Command :: new ( shell) ;
1259+ match command {
1260+ None => {
1261+ if let Some ( arg) = no_command_arg {
1262+ cmd. arg ( arg) ;
1263+ }
1264+ }
1265+ Some ( command) => {
1266+ cmd. arg ( login_shell_flag ( no_login_shell) ) . arg ( command) ;
1267+ }
1268+ }
1269+ cmd
1270+ }
1271+
12451272#[ allow( clippy:: too_many_arguments) ]
12461273fn spawn_pty_shell (
12471274 policy : & SandboxPolicy ,
@@ -1276,18 +1303,11 @@ fn spawn_pty_shell(
12761303 let mut reader = master. try_clone ( ) ?;
12771304 let mut writer = master. try_clone ( ) ?;
12781305
1279- let mut cmd = command. map_or_else (
1280- || {
1281- let mut c = Command :: new ( "/bin/bash" ) ;
1282- c. arg ( "-i" ) ;
1283- c
1284- } ,
1285- |command| {
1286- let mut c = Command :: new ( "/bin/bash" ) ;
1287- c. arg ( login_shell_flag ( no_login_shell) ) . arg ( command) ;
1288- c
1289- } ,
1290- ) ;
1306+ // Resolve a shell present in the sandbox image; interactive PTY sessions
1307+ // pass `-i` when no command is given. Runs in the supervisor, so it
1308+ // inspects the sandbox filesystem.
1309+ let shell = openshell_core:: shell:: detect_login_shell ( ) ;
1310+ let mut cmd = build_ssh_shell_command ( & shell, command, no_login_shell, Some ( "-i" ) ) ;
12911311
12921312 let term = if pty. term . is_empty ( ) {
12931313 "xterm-256color"
@@ -1437,27 +1457,13 @@ fn spawn_pipe_exec(
14371457) -> anyhow:: Result < mpsc:: Sender < Vec < u8 > > > {
14381458 // Resolve a shell present in the sandbox image; minimal images (e.g. Alpine)
14391459 // don't ship bash, only `/bin/sh`. Runs in the supervisor, so it inspects
1440- // the sandbox filesystem.
1460+ // the sandbox filesystem. No command → read from stdin with no `-i`:
1461+ // interactive mode reads .bashrc, writes prompts to stderr, and can add
1462+ // just enough latency for VS Code Remote-SSH's platform detection to time
1463+ // out and fall back to "windows". A plain shell with piped stdin already
1464+ // reads commands line-by-line (script mode), which is what VS Code expects.
14411465 let shell = openshell_core:: shell:: detect_login_shell ( ) ;
1442- let mut cmd = command. map_or_else (
1443- || {
1444- // No command — read from stdin. Do *not* pass `-i`; interactive
1445- // mode reads .bashrc, writes prompts to stderr, and can introduce
1446- // just enough latency for VS Code Remote-SSH's platform detection
1447- // to time out and fall back to "windows". Plain shell with piped
1448- // stdin already reads commands line-by-line (script mode), which is
1449- // exactly what VS Code's local server expects.
1450- Command :: new ( & shell)
1451- } ,
1452- |command| {
1453- let mut c = Command :: new ( & shell) ;
1454- // Login shell (-l) sources .profile/.bashrc so tool env vars
1455- // (VIRTUAL_ENV, etc.) are available. Callers that need a predictable
1456- // environment opt out via OPENSHELL_NO_LOGIN_SHELL → plain -c.
1457- c. arg ( login_shell_flag ( no_login_shell) ) . arg ( command) ;
1458- c
1459- } ,
1460- ) ;
1466+ let mut cmd = build_ssh_shell_command ( & shell, command, no_login_shell, None ) ;
14611467
14621468 let ( session_user, session_home) = session_user_and_home ( policy, workspace. home ( ) ) ;
14631469 apply_child_env (
@@ -1801,8 +1807,40 @@ fn is_loopback_host(host: &str) -> bool {
18011807) ]
18021808mod tests {
18031809 use super :: * ;
1810+ use std:: ffi:: OsStr ;
18041811 use std:: process:: Stdio ;
18051812
1813+ /// Regression test: SSH sessions run the shell they are given, never a
1814+ /// hardcoded bash, so sh-only images (e.g. Alpine) work. Covers both the
1815+ /// interactive PTY path (`-i` when no command) and the non-PTY path.
1816+ #[ test]
1817+ fn build_ssh_shell_command_uses_given_shell ( ) {
1818+ // PTY, no command → given shell + interactive flag.
1819+ let cmd = build_ssh_shell_command ( "/bin/sh" , None , false , Some ( "-i" ) ) ;
1820+ assert_eq ! ( cmd. get_program( ) , OsStr :: new( "/bin/sh" ) ) ;
1821+ assert_eq ! ( cmd. get_args( ) . collect:: <Vec <_>>( ) , vec![ OsStr :: new( "-i" ) ] ) ;
1822+
1823+ // Non-PTY, no command → bare shell, no args (reads piped stdin).
1824+ let cmd = build_ssh_shell_command ( "/bin/sh" , None , false , None ) ;
1825+ assert_eq ! ( cmd. get_program( ) , OsStr :: new( "/bin/sh" ) ) ;
1826+ assert_eq ! ( cmd. get_args( ) . count( ) , 0 ) ;
1827+
1828+ // Explicit command → login-shell flag + command, still on the given shell.
1829+ let cmd = build_ssh_shell_command ( "/bin/sh" , Some ( "echo hi" . into ( ) ) , false , Some ( "-i" ) ) ;
1830+ assert_eq ! ( cmd. get_program( ) , OsStr :: new( "/bin/sh" ) ) ;
1831+ assert_eq ! (
1832+ cmd. get_args( ) . collect:: <Vec <_>>( ) ,
1833+ vec![ OsStr :: new( "-lc" ) , OsStr :: new( "echo hi" ) ]
1834+ ) ;
1835+
1836+ // OPENSHELL_NO_LOGIN_SHELL → plain -c.
1837+ let cmd = build_ssh_shell_command ( "/bin/sh" , Some ( "echo hi" . into ( ) ) , true , None ) ;
1838+ assert_eq ! (
1839+ cmd. get_args( ) . collect:: <Vec <_>>( ) ,
1840+ vec![ OsStr :: new( "-c" ) , OsStr :: new( "echo hi" ) ]
1841+ ) ;
1842+ }
1843+
18061844 /// Regression test: the direct-tcpip connect path sets `TCP_NODELAY`.
18071845 #[ tokio:: test]
18081846 async fn connect_in_netns_sets_tcp_nodelay ( ) {
0 commit comments