mirror of
https://github.com/openai/codex.git
synced 2026-10-30 19:18:49 +00:00
fix windows shell resolution for PowerShell
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
use crate::shell_detect::detect_shell_type;
|
||||
use crate::shell_snapshot::ShellSnapshot;
|
||||
#[cfg(windows)]
|
||||
use codex_shell_command::powershell::try_find_powershell_executable_blocking;
|
||||
#[cfg(windows)]
|
||||
use codex_shell_command::powershell::try_find_pwsh_executable_blocking;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::path::PathBuf;
|
||||
@@ -250,6 +254,10 @@ const POWERSHELL_FALLBACK_PATHS: &[&str] =
|
||||
const POWERSHELL_FALLBACK_PATHS: &[&str] = &[];
|
||||
|
||||
fn get_powershell_shell(path: Option<&PathBuf>) -> Option<Shell> {
|
||||
#[cfg(windows)]
|
||||
let shell_path = get_windows_powershell_shell_path(path);
|
||||
|
||||
#[cfg(not(windows))]
|
||||
let shell_path = get_shell_path(ShellType::PowerShell, path, "pwsh", PWSH_FALLBACK_PATHS)
|
||||
.or_else(|| {
|
||||
get_shell_path(
|
||||
@@ -267,6 +275,41 @@ fn get_powershell_shell(path: Option<&PathBuf>) -> Option<Shell> {
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn get_windows_powershell_shell_path(path: Option<&PathBuf>) -> Option<PathBuf> {
|
||||
if let Some(path) = path.and_then(file_exists)
|
||||
&& !is_windows_batch_script(&path)
|
||||
{
|
||||
return Some(path);
|
||||
}
|
||||
|
||||
let default_shell_path = get_user_shell_path();
|
||||
if let Some(default_shell_path) = default_shell_path
|
||||
&& detect_shell_type(&default_shell_path) == Some(ShellType::PowerShell)
|
||||
&& file_exists(&default_shell_path).is_some()
|
||||
&& !is_windows_batch_script(&default_shell_path)
|
||||
{
|
||||
return Some(default_shell_path);
|
||||
}
|
||||
|
||||
try_find_pwsh_executable_blocking()
|
||||
.map(|path| path.into_path_buf())
|
||||
.or_else(|| try_find_powershell_executable_blocking().map(|path| path.into_path_buf()))
|
||||
.or_else(|| {
|
||||
POWERSHELL_FALLBACK_PATHS
|
||||
.iter()
|
||||
.find_map(|path| file_exists(&PathBuf::from(path)))
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn is_windows_batch_script(path: &std::path::Path) -> bool {
|
||||
matches!(
|
||||
path.extension().and_then(|extension| extension.to_str()),
|
||||
Some(extension) if extension.eq_ignore_ascii_case("cmd") || extension.eq_ignore_ascii_case("bat")
|
||||
)
|
||||
}
|
||||
|
||||
fn get_cmd_shell(path: Option<&PathBuf>) -> Option<Shell> {
|
||||
let shell_path = get_shell_path(ShellType::Cmd, path, "cmd", &[]);
|
||||
|
||||
|
||||
@@ -190,3 +190,14 @@ fn finds_powershell() {
|
||||
|
||||
assert!(shell_path.ends_with("pwsh.exe") || shell_path.ends_with("powershell.exe"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn windows_batch_shell_wrappers_are_rejected() {
|
||||
if !cfg!(windows) {
|
||||
return;
|
||||
}
|
||||
|
||||
assert!(is_windows_batch_script(std::path::Path::new("pwsh.cmd")));
|
||||
assert!(is_windows_batch_script(std::path::Path::new("pwsh.bat")));
|
||||
assert!(!is_windows_batch_script(std::path::Path::new("pwsh.exe")));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user