diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index ff2f7ef247..2b5c9aaa78 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -206,6 +206,36 @@ async fn load_config_normalizes_relative_cwd_override() -> std::io::Result<()> { Ok(()) } +#[test] +fn normalize_current_dir_for_default_cwd_replaces_windowsapps_package_path() { + let codex_home = test_absolute_path(r"C:\Users\wang\.codex"); + let current_dir = PathBuf::from( + r"C:\Program Files\WindowsApps\OpenAI.Codex_26.609.41114_x64__2p2nqsd0c76g0\app", + ); + + let resolved = super::normalize_current_dir_for_default_cwd(current_dir, codex_home.as_path()); + + assert_eq!(resolved, codex_home.to_path_buf()); +} + +#[test] +fn normalize_current_dir_for_default_cwd_preserves_normal_workspace_path() { + let codex_home = test_absolute_path(r"C:\Users\wang\.codex"); + let workspace = PathBuf::from(r"C:\Users\wang\src\project"); + + let resolved = + super::normalize_current_dir_for_default_cwd(workspace.clone(), codex_home.as_path()); + + assert_eq!(resolved, workspace); +} + +#[test] +fn is_windowsapps_package_path_accepts_verbatim_windows_prefix() { + assert!(super::is_windowsapps_package_path(Path::new( + r"\\?\C:\Program Files\WindowsApps\OpenAI.Codex_26.609.41114_x64__2p2nqsd0c76g0\app" + ))); +} + #[tokio::test] async fn test_toml_parsing() { let history_with_persistence = r#" diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index 6cf7b9a18e..149f548f72 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -267,6 +267,30 @@ fn resolve_sqlite_home_env(resolved_cwd: &Path) -> Option { } } +fn normalize_current_dir_for_default_cwd(current_dir: PathBuf, codex_home: &Path) -> PathBuf { + if is_windowsapps_package_path(¤t_dir) { + default_cwd_fallback(codex_home) + } else { + current_dir + } +} + +fn default_cwd_fallback(codex_home: &Path) -> PathBuf { + std::env::var_os("USERPROFILE") + .map(PathBuf::from) + .filter(|path| path.is_absolute()) + .unwrap_or_else(|| codex_home.to_path_buf()) +} + +fn is_windowsapps_package_path(path: &Path) -> bool { + let mut key = path.as_os_str().to_string_lossy().replace('\\', "/"); + while let Some(stripped) = key.strip_prefix("//?/") { + key = stripped.to_string(); + } + let key = key.to_ascii_lowercase(); + key == "c:/program files/windowsapps" || key.starts_with("c:/program files/windowsapps/") +} + fn resolve_cli_auth_credentials_store_mode( configured: AuthCredentialsStoreMode, package_version: &str, @@ -1217,7 +1241,10 @@ impl ConfigBuilder { let cwd_override = harness_overrides.cwd.as_deref().or(fallback_cwd.as_deref()); let cwd = match cwd_override { Some(path) => AbsolutePathBuf::relative_to_current_dir(path)?, - None => AbsolutePathBuf::current_dir()?, + None => AbsolutePathBuf::try_from(normalize_current_dir_for_default_cwd( + std::env::current_dir()?, + codex_home.as_path(), + ))?, }; harness_overrides.cwd = Some(cwd.to_path_buf()); let config_layer_stack = load_config_layers_state( @@ -2762,7 +2789,7 @@ impl Config { match cwd { None => { tracing::info!("cwd not set, using current dir"); - env::current_dir()? + normalize_current_dir_for_default_cwd(env::current_dir()?, codex_home.as_path()) } Some(p) if p.is_absolute() => p, Some(p) => {