diff --git a/codex-rs/core/src/exec_env.rs b/codex-rs/core/src/exec_env.rs index 83ac8ad379..2c84792b70 100644 --- a/codex-rs/core/src/exec_env.rs +++ b/codex-rs/core/src/exec_env.rs @@ -38,6 +38,32 @@ where ShellEnvironmentPolicyInherit::All => vars.into_iter().collect(), ShellEnvironmentPolicyInherit::None => HashMap::new(), ShellEnvironmentPolicyInherit::Core => { + #[cfg(target_os = "windows")] + const CORE_VARS: &[&str] = &[ + "APPDATA", + "COMSPEC", + "HOMEDRIVE", + "HOMEPATH", + "HOME", + "LOCALAPPDATA", + "LOGNAME", + "PATH", + "PATHEXT", + "PROGRAMDATA", + "PROGRAMFILES", + "PROGRAMFILES(X86)", + "PROGRAMW6432", + "SHELL", + "SYSTEMDRIVE", + "SYSTEMROOT", + "TEMP", + "TMP", + "TMPDIR", + "USER", + "USERNAME", + "USERPROFILE", + ]; + #[cfg(not(target_os = "windows"))] const CORE_VARS: &[&str] = &[ "HOME", "LOGNAME", "PATH", "SHELL", "USER", "USERNAME", "TMPDIR", "TEMP", "TMP", ]; diff --git a/codex-rs/core/src/exec_env_tests.rs b/codex-rs/core/src/exec_env_tests.rs index 6f001b5828..80f463c312 100644 --- a/codex-rs/core/src/exec_env_tests.rs +++ b/codex-rs/core/src/exec_env_tests.rs @@ -192,6 +192,48 @@ fn test_core_inherit_respects_case_insensitive_names_on_windows() { assert_eq!(result, expected); } +#[test] +#[cfg(target_os = "windows")] +fn test_core_inherit_keeps_required_windows_runtime_vars() { + let vars = make_vars(&[ + ("APPDATA", "C:\\Users\\alice\\AppData\\Roaming"), + ("ComSpec", "C:\\Windows\\System32\\cmd.exe"), + ("LOCALAPPDATA", "C:\\Users\\alice\\AppData\\Local"), + ("Path", "C:\\Windows\\System32"), + ("ProgramFiles", "C:\\Program Files"), + ("ProgramFiles(x86)", "C:\\Program Files (x86)"), + ("ProgramW6432", "C:\\Program Files"), + ("SystemDrive", "C:"), + ("SystemRoot", "C:\\Windows"), + ("USERPROFILE", "C:\\Users\\alice"), + ("FOO", "bar"), + ]); + + let policy = ShellEnvironmentPolicy { + inherit: ShellEnvironmentPolicyInherit::Core, + ignore_default_excludes: true, + ..Default::default() + }; + + let thread_id = ThreadId::new(); + let result = populate_env(vars, &policy, Some(thread_id)); + let mut expected: HashMap = hashmap! { + "APPDATA".to_string() => "C:\\Users\\alice\\AppData\\Roaming".to_string(), + "ComSpec".to_string() => "C:\\Windows\\System32\\cmd.exe".to_string(), + "LOCALAPPDATA".to_string() => "C:\\Users\\alice\\AppData\\Local".to_string(), + "Path".to_string() => "C:\\Windows\\System32".to_string(), + "ProgramFiles".to_string() => "C:\\Program Files".to_string(), + "ProgramFiles(x86)".to_string() => "C:\\Program Files (x86)".to_string(), + "ProgramW6432".to_string() => "C:\\Program Files".to_string(), + "SystemDrive".to_string() => "C:".to_string(), + "SystemRoot".to_string() => "C:\\Windows".to_string(), + "USERPROFILE".to_string() => "C:\\Users\\alice".to_string(), + }; + expected.insert(CODEX_THREAD_ID_ENV_VAR.to_string(), thread_id.to_string()); + + assert_eq!(result, expected); +} + #[test] fn test_inherit_none() { let vars = make_vars(&[("PATH", "/usr/bin"), ("HOME", "/home")]);