From 305e997faf6385026e308c2196d9443d3406f4ea Mon Sep 17 00:00:00 2001 From: David Wiesen Date: Wed, 29 Apr 2026 10:02:02 -0700 Subject: [PATCH] Prefer real pwsh executable on Windows --- codex-rs/core/src/shell.rs | 36 +++++++++++++++++++++++++++++--- codex-rs/core/src/shell_tests.rs | 13 ++++++++++-- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/codex-rs/core/src/shell.rs b/codex-rs/core/src/shell.rs index 48c760d667..a0a52992f6 100644 --- a/codex-rs/core/src/shell.rs +++ b/codex-rs/core/src/shell.rs @@ -1,5 +1,7 @@ use crate::shell_detect::detect_shell_type; use crate::shell_snapshot::ShellSnapshot; +use codex_shell_command::powershell::try_find_powershell_executable_blocking; +use codex_shell_command::powershell::try_find_pwsh_executable_blocking; use serde::Deserialize; use serde::Serialize; use std::path::PathBuf; @@ -250,15 +252,27 @@ const POWERSHELL_FALLBACK_PATHS: &[&str] = const POWERSHELL_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(|| { + let shell_path = if path.is_some() { + get_shell_path(ShellType::PowerShell, path, "pwsh", PWSH_FALLBACK_PATHS).or_else(|| { get_shell_path( ShellType::PowerShell, path, "powershell", POWERSHELL_FALLBACK_PATHS, ) - }); + }) + } else { + preferred_powershell_path() + .or_else(|| get_shell_path(ShellType::PowerShell, path, "pwsh", PWSH_FALLBACK_PATHS)) + .or_else(|| { + get_shell_path( + ShellType::PowerShell, + path, + "powershell", + POWERSHELL_FALLBACK_PATHS, + ) + }) + }; shell_path.map(|shell_path| Shell { shell_type: ShellType::PowerShell, @@ -267,6 +281,22 @@ fn get_powershell_shell(path: Option<&PathBuf>) -> Option { }) } +#[cfg(windows)] +fn preferred_powershell_path() -> Option { + // Resolve a real PowerShell executable instead of trusting PATH lookup to + // hand back an execution alias or batch shim like `pwsh.cmd`. + try_find_pwsh_executable_blocking() + .map(|path| path.into_path_buf()) + .or_else(|| { + try_find_powershell_executable_blocking().map(|path| path.into_path_buf()) + }) +} + +#[cfg(not(windows))] +fn preferred_powershell_path() -> Option { + None +} + fn get_cmd_shell(path: Option<&PathBuf>) -> Option { let shell_path = get_shell_path(ShellType::Cmd, path, "cmd", &[]); diff --git a/codex-rs/core/src/shell_tests.rs b/codex-rs/core/src/shell_tests.rs index f0974900b2..5078fbbac2 100644 --- a/codex-rs/core/src/shell_tests.rs +++ b/codex-rs/core/src/shell_tests.rs @@ -1,4 +1,5 @@ use super::*; +use codex_shell_command::powershell::try_find_pwsh_executable_blocking; use std::path::PathBuf; use std::process::Command; @@ -176,7 +177,11 @@ async fn detects_powershell_as_default() { let powershell_shell = default_user_shell(); let shell_path = powershell_shell.shell_path; - assert!(shell_path.ends_with("pwsh.exe") || shell_path.ends_with("powershell.exe")); + if let Some(pwsh_path) = try_find_pwsh_executable_blocking() { + assert_eq!(shell_path, pwsh_path.into_path_buf()); + } else { + assert!(shell_path.ends_with("powershell.exe")); + } } #[test] @@ -188,5 +193,9 @@ fn finds_powershell() { let powershell_shell = get_shell(ShellType::PowerShell, /*path*/ None).unwrap(); let shell_path = powershell_shell.shell_path; - assert!(shell_path.ends_with("pwsh.exe") || shell_path.ends_with("powershell.exe")); + if let Some(pwsh_path) = try_find_pwsh_executable_blocking() { + assert_eq!(shell_path, pwsh_path.into_path_buf()); + } else { + assert!(shell_path.ends_with("powershell.exe")); + } }