From 2bd0700b76bd37e15b35bd4a311b28a534004402 Mon Sep 17 00:00:00 2001 From: viyatb-oai Date: Wed, 15 Apr 2026 14:09:45 -0700 Subject: [PATCH] refactor: share shell wrapper detection Expose the known-shell detector from codex-shell-command and map it into codex-core's runtime shell type. Co-authored-by: Codex --- codex-rs/core/src/lib.rs | 1 - codex-rs/core/src/shell.rs | 13 ++++++++- codex-rs/core/src/shell_detect.rs | 34 ---------------------- codex-rs/shell-command/src/lib.rs | 2 +- codex-rs/shell-command/src/shell_detect.rs | 5 ++-- 5 files changed, 16 insertions(+), 39 deletions(-) delete mode 100644 codex-rs/core/src/shell_detect.rs diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index 8d463f3b51..6f2c68f157 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -86,7 +86,6 @@ mod sandbox_tags; pub mod sandboxing; mod session_prefix; mod session_startup_prewarm; -mod shell_detect; pub mod skills; pub(crate) use skills::SkillError; pub(crate) use skills::SkillInjections; diff --git a/codex-rs/core/src/shell.rs b/codex-rs/core/src/shell.rs index 501d693e1a..5754871147 100644 --- a/codex-rs/core/src/shell.rs +++ b/codex-rs/core/src/shell.rs @@ -1,5 +1,6 @@ -use crate::shell_detect::detect_shell_type; use crate::shell_snapshot::ShellSnapshot; +use codex_shell_command::shell_detect::ShellType as DetectedShellType; +use codex_shell_command::shell_detect::detect_shell_type as detect_known_shell_type; use serde::Deserialize; use serde::Serialize; use std::path::PathBuf; @@ -88,6 +89,16 @@ impl PartialEq for Shell { impl Eq for Shell {} +fn detect_shell_type(shell_path: &PathBuf) -> Option { + match detect_known_shell_type(shell_path)? { + DetectedShellType::Zsh => Some(ShellType::Zsh), + DetectedShellType::Bash => Some(ShellType::Bash), + DetectedShellType::PowerShell => Some(ShellType::PowerShell), + DetectedShellType::Sh => Some(ShellType::Sh), + DetectedShellType::Cmd => Some(ShellType::Cmd), + } +} + #[cfg(unix)] fn get_user_shell_path() -> Option { let uid = unsafe { libc::getuid() }; diff --git a/codex-rs/core/src/shell_detect.rs b/codex-rs/core/src/shell_detect.rs deleted file mode 100644 index fda8d8cf21..0000000000 --- a/codex-rs/core/src/shell_detect.rs +++ /dev/null @@ -1,34 +0,0 @@ -use crate::shell::ShellType; -use std::path::PathBuf; - -pub(crate) fn detect_shell_type(shell_path: &PathBuf) -> Option { - let shell_text = shell_path.as_os_str().to_str()?; - // Keep this exact: repo-local files named like shells must not inherit - // shell-wrapper trust in approval or display decisions. - match shell_text { - "zsh" | "/bin/zsh" | "/usr/bin/zsh" | "/usr/local/bin/zsh" | "/opt/homebrew/bin/zsh" => { - Some(ShellType::Zsh) - } - "sh" | "/bin/sh" | "/usr/bin/sh" => Some(ShellType::Sh), - "bash" - | "/bin/bash" - | "/usr/bin/bash" - | "/usr/local/bin/bash" - | "/opt/homebrew/bin/bash" => Some(ShellType::Bash), - "pwsh" - | "powershell" - | "pwsh.exe" - | "powershell.exe" - | "/usr/local/bin/pwsh" - | "/usr/bin/pwsh" - | "/bin/pwsh" - | "/opt/homebrew/bin/pwsh" => Some(ShellType::PowerShell), - "cmd" | "cmd.exe" => Some(ShellType::Cmd), - _ => match shell_text.replace('\\', "/").to_ascii_lowercase().as_str() { - "c:/windows/system32/cmd.exe" => Some(ShellType::Cmd), - "c:/windows/system32/windowspowershell/v1.0/powershell.exe" - | "c:/program files/powershell/7/pwsh.exe" => Some(ShellType::PowerShell), - _ => None, - }, - } -} diff --git a/codex-rs/shell-command/src/lib.rs b/codex-rs/shell-command/src/lib.rs index 1d9e302a4e..947de5e645 100644 --- a/codex-rs/shell-command/src/lib.rs +++ b/codex-rs/shell-command/src/lib.rs @@ -1,6 +1,6 @@ //! Command parsing and safety utilities shared across Codex crates. -mod shell_detect; +pub mod shell_detect; pub mod bash; pub(crate) mod command_safety; diff --git a/codex-rs/shell-command/src/shell_detect.rs b/codex-rs/shell-command/src/shell_detect.rs index 5dc8e21762..902cabce00 100644 --- a/codex-rs/shell-command/src/shell_detect.rs +++ b/codex-rs/shell-command/src/shell_detect.rs @@ -1,7 +1,8 @@ use std::path::PathBuf; +/// Shell executable families that Codex treats as known command wrappers. #[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub(crate) enum ShellType { +pub enum ShellType { Zsh, Bash, PowerShell, @@ -9,7 +10,7 @@ pub(crate) enum ShellType { Cmd, } -pub(crate) fn detect_shell_type(shell_path: &PathBuf) -> Option { +pub fn detect_shell_type(shell_path: &PathBuf) -> Option { let shell_text = shell_path.as_os_str().to_str()?; // Keep this exact: repo-local files named like shells must not inherit // shell-wrapper trust in approval or display decisions.