Compare commits

...

1 Commits

Author SHA1 Message Date
David Wiesen
305e997faf Prefer real pwsh executable on Windows 2026-04-29 10:02:02 -07:00
2 changed files with 44 additions and 5 deletions

View File

@@ -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<Shell> {
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<Shell> {
})
}
#[cfg(windows)]
fn preferred_powershell_path() -> Option<PathBuf> {
// 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<PathBuf> {
None
}
fn get_cmd_shell(path: Option<&PathBuf>) -> Option<Shell> {
let shell_path = get_shell_path(ShellType::Cmd, path, "cmd", &[]);

View File

@@ -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"));
}
}