Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

[codex] Handle PowerShell UTF-8 setup failures (#24949)

iceweasel-oaid9f53128b7ab
diff --git a/codex-rs/shell-command/src/powershell.rs b/codex-rs/shell-command/src/powershell.rs
index f7b294c0f8..e8c9a500c4 100644
--- a/codex-rs/shell-command/src/powershell.rs
+++ b/codex-rs/shell-command/src/powershell.rs
@@ -8,8 +8,9 @@ use crate::shell_detect::detect_shell_type;
const POWERSHELL_FLAGS: &[&str] = &["-nologo", "-noprofile", "-command", "-c"];
-/// Prefixed command for powershell shell calls to force UTF-8 console output.
-pub const UTF8_OUTPUT_PREFIX: &str = "[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;\n";
+/// Prefixed command for powershell shell calls to request UTF-8 console output.
+pub const UTF8_OUTPUT_PREFIX: &str =
+ "try { [Console]::OutputEncoding=[System.Text.Encoding]::UTF8 } catch {}\n";
pub fn prefix_powershell_script_with_utf8(command: &[String]) -> Vec<String> {
let Some((_, script)) = extract_powershell_command(command) else {
@@ -151,9 +152,11 @@ fn is_powershellish_executable_available(powershell_or_pwsh_exe: &std::path::Pat
#[cfg(test)]
mod tests {
+ use super::UTF8_OUTPUT_PREFIX;
use super::extract_powershell_command;
#[cfg(windows)]
use super::parse_powershell_command_into_plain_commands;
+ use super::prefix_powershell_script_with_utf8;
#[test]
fn extracts_basic_powershell_command() {
@@ -202,6 +205,37 @@ mod tests {
assert_eq!(script, "Get-ChildItem | Select-String foo");
}
+ #[test]
+ fn prefixes_powershell_command_with_best_effort_utf8() {
+ let cmd = vec![
+ "powershell".to_string(),
+ "-Command".to_string(),
+ "Write-Host hi".to_string(),
+ ];
+
+ let prefixed = prefix_powershell_script_with_utf8(&cmd);
+
+ assert_eq!(
+ prefixed,
+ vec![
+ "powershell".to_string(),
+ "-Command".to_string(),
+ format!("{UTF8_OUTPUT_PREFIX}Write-Host hi"),
+ ]
+ );
+ }
+
+ #[test]
+ fn does_not_duplicate_utf8_prefix() {
+ let cmd = vec![
+ "powershell".to_string(),
+ "-Command".to_string(),
+ format!("{UTF8_OUTPUT_PREFIX}Write-Host hi"),
+ ];
+
+ assert_eq!(prefix_powershell_script_with_utf8(&cmd), cmd);
+ }
+
#[cfg(windows)]
#[test]
fn parses_plain_powershell_commands() {