Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions codex-rs/core/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Shell {
ps.exe.clone(),
"-NoProfile".to_string(),
"-Command".to_string(),
script,
format!("[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; {}", script),
]),
};
}
Expand All @@ -79,13 +79,23 @@ impl Shell {
ps.exe.clone(),
"-NoProfile".to_string(),
"-Command".to_string(),
arg,
format!("[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; {}", arg),
]
});
}

// Model generated a PowerShell command. Run it.
Some(command)
// If it's already a PowerShell command, we need to ensure UTF-8 encoding is set.
if command.len() >= 3 && command[1] == "-Command" {
let mut modified_command = command.clone();
if let Some(cmd_arg) = modified_command.get_mut(2) {
// Prepend UTF-8 encoding setup to the command
*cmd_arg = format!("[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; {}", cmd_arg);
}
Some(modified_command)
} else {
Some(command)
}
}
Shell::Unknown => None,
}
Expand Down Expand Up @@ -490,15 +500,15 @@ mod tests_windows {
bash_exe_fallback: None,
}),
vec!["bash", "-lc", "echo hello"],
vec!["pwsh.exe", "-NoProfile", "-Command", "echo hello"],
vec!["pwsh.exe", "-NoProfile", "-Command", "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; echo hello"],
),
(
Shell::PowerShell(PowerShellConfig {
exe: "powershell.exe".to_string(),
bash_exe_fallback: None,
}),
vec!["bash", "-lc", "echo hello"],
vec!["powershell.exe", "-NoProfile", "-Command", "echo hello"],
vec!["powershell.exe", "-NoProfile", "-Command", "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; echo hello"],
),
(
Shell::PowerShell(PowerShellConfig {
Expand Down Expand Up @@ -530,15 +540,15 @@ mod tests_windows {
bash_exe_fallback: Some(PathBuf::from("bash.exe")),
}),
vec!["echo", "hello"],
vec!["pwsh.exe", "-NoProfile", "-Command", "echo hello"],
vec!["pwsh.exe", "-NoProfile", "-Command", "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; echo hello"],
),
(
Shell::PowerShell(PowerShellConfig {
exe: "pwsh.exe".to_string(),
bash_exe_fallback: Some(PathBuf::from("bash.exe")),
}),
vec!["pwsh.exe", "-NoProfile", "-Command", "echo hello"],
vec!["pwsh.exe", "-NoProfile", "-Command", "echo hello"],
vec!["pwsh.exe", "-NoProfile", "-Command", "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; echo hello"],
),
(
// TODO (CODEX_2900): Handle escaping newlines for powershell invocation.
Expand Down Expand Up @@ -568,4 +578,25 @@ mod tests_windows {
);
}
}

#[test]
fn test_powershell_utf8_encoding_setup() {
let shell = Shell::PowerShell(PowerShellConfig {
exe: "pwsh.exe".to_string(),
bash_exe_fallback: None,
});

// Test that commands get UTF-8 encoding setup prepended
let input = vec!["echo".to_string(), "Turkish: çğıİöşü".to_string()];
let actual = shell.format_default_shell_invocation(input);

assert!(actual.is_some());
let cmd = actual.unwrap();
assert_eq!(cmd.len(), 4);
assert_eq!(cmd[0], "pwsh.exe");
assert_eq!(cmd[1], "-NoProfile");
assert_eq!(cmd[2], "-Command");
assert!(cmd[3].starts_with("[Console]::OutputEncoding = [System.Text.Encoding]::UTF8;"));
assert!(cmd[3].contains("Turkish: çğıİöşü"));
}
}