diff --git a/codex-rs/core/src/shell.rs b/codex-rs/core/src/shell.rs index 48c760d667..8411137b5c 100644 --- a/codex-rs/core/src/shell.rs +++ b/codex-rs/core/src/shell.rs @@ -249,6 +249,11 @@ const POWERSHELL_FALLBACK_PATHS: &[&str] = #[cfg(not(windows))] const POWERSHELL_FALLBACK_PATHS: &[&str] = &[]; +#[cfg(windows)] +const CMD_FALLBACK_PATHS: &[&str] = &[r#"C:\Windows\System32\cmd.exe"#]; +#[cfg(not(windows))] +const CMD_FALLBACK_PATHS: &[&str] = &[]; + fn get_powershell_shell(path: Option<&PathBuf>) -> Option { let shell_path = get_shell_path(ShellType::PowerShell, path, "pwsh", PWSH_FALLBACK_PATHS) .or_else(|| { @@ -268,7 +273,7 @@ fn get_powershell_shell(path: Option<&PathBuf>) -> Option { } fn get_cmd_shell(path: Option<&PathBuf>) -> Option { - let shell_path = get_shell_path(ShellType::Cmd, path, "cmd", &[]); + let shell_path = get_shell_path(ShellType::Cmd, path, "cmd", CMD_FALLBACK_PATHS); shell_path.map(|shell_path| Shell { shell_type: ShellType::Cmd, @@ -315,7 +320,11 @@ pub fn default_user_shell() -> Shell { fn default_user_shell_from_path(user_shell_path: Option) -> Shell { if cfg!(windows) { - get_shell(ShellType::PowerShell, /*path*/ None).unwrap_or(ultimate_fallback_shell()) + // Prefer cmd.exe on Windows because sandboxed PowerShell startup can fail + // before the user command runs, which breaks all shell_command invocations. + get_shell(ShellType::Cmd, /*path*/ None) + .or_else(|| get_shell(ShellType::PowerShell, /*path*/ None)) + .unwrap_or(ultimate_fallback_shell()) } else { let user_default_shell = user_shell_path .and_then(|shell| detect_shell_type(&shell)) diff --git a/codex-rs/core/src/shell_tests.rs b/codex-rs/core/src/shell_tests.rs index f0974900b2..670b0839c8 100644 --- a/codex-rs/core/src/shell_tests.rs +++ b/codex-rs/core/src/shell_tests.rs @@ -168,15 +168,16 @@ async fn test_current_shell_detects_zsh() { } #[tokio::test] -async fn detects_powershell_as_default() { +async fn detects_cmd_as_default() { if !cfg!(windows) { return; } - let powershell_shell = default_user_shell(); - let shell_path = powershell_shell.shell_path; + let shell = default_user_shell(); + let shell_path = shell.shell_path; - assert!(shell_path.ends_with("pwsh.exe") || shell_path.ends_with("powershell.exe")); + assert_eq!(shell.shell_type, ShellType::Cmd); + assert!(shell_path.ends_with("cmd.exe")); } #[test]