From 1b06a7002eb9610ddbe3c1a489f1b19f126bf580 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Mon, 30 Nov 2026 17:52:45 +0000 Subject: [PATCH] Preserve Git root casing in the folder trust prompt (#64777) ## Why On Windows, a case-folded trust key can make a Git root appear as a separate path in the folder trust prompt even when it is the current directory. ## What changed Display the Git root with its original casing when it matches the current directory. Keep the selected trust key for saving the decision and continue to show distinct trust targets. ## Testing Cover mixed-case Git roots in trust resolution and verify that the prompt displays a single path for a matching root while keeping distinct targets visible. GitOrigin-RevId: 95b459dbcb8acf0611da8b04a1a413e56663f1e4 --- codex-rs/tui/src/config_update.rs | 36 +++++++++++++++++-- codex-rs/tui/src/config_update_tests.rs | 18 ++++++++-- .../tui/src/onboarding/directory_trust.rs | 4 +-- .../tui/src/onboarding/onboarding_screen.rs | 2 +- ..._tests__renders_snapshot_for_git_repo.snap | 6 ++-- .../tui/src/onboarding/trust_directory.rs | 25 ++++++++++++- 6 files changed, 78 insertions(+), 13 deletions(-) diff --git a/codex-rs/tui/src/config_update.rs b/codex-rs/tui/src/config_update.rs index d2916dc1ce..35386eef06 100644 --- a/codex-rs/tui/src/config_update.rs +++ b/codex-rs/tui/src/config_update.rs @@ -51,6 +51,7 @@ pub(crate) struct RemoteProjectTrust { pub trust_level: Option, pub cwd: PathBuf, pub trust_target: PathBuf, + pub trust_key: String, } pub(crate) fn replace_config_value(key_path: impl Into, value: JsonValue) -> ConfigEdit { @@ -290,6 +291,7 @@ pub(crate) async fn read_remote_project_trust( }) }; let mut projectless = false; + let mut git_trust_root = None; let local_roots = if host == ProjectTrustHost::Local && project_layers.is_empty() { let cwd = AbsolutePathBuf::from_absolute_path(&cwd)?; let markers = serde_json::from_value::>>( @@ -297,14 +299,16 @@ pub(crate) async fn read_remote_project_trust( )? .unwrap_or_else(default_project_root_markers); let project_root = discover_project_root(LOCAL_FS.as_ref(), &cwd, &markers).await?; - let git_root = resolve_root_git_project_for_trust(LOCAL_FS.as_ref(), &cwd).await; + git_trust_root = resolve_root_git_project_for_trust(LOCAL_FS.as_ref(), &cwd).await; projectless = project_root.is_none() && discover_project_root(LOCAL_FS.as_ref(), &cwd, &default_project_root_markers()) .await? .is_none(); Some(( normalized_project_trust_keys(project_root.as_ref().unwrap_or(&cwd).as_path()), - git_root.map(|root| normalized_project_trust_keys(root.as_path())), + git_trust_root + .as_ref() + .map(|root| normalized_project_trust_keys(root.as_path())), )) } else { None @@ -393,6 +397,31 @@ pub(crate) async fn read_remote_project_trust( )); } + // A folded trust key may name the worktree instead of the main checkout. + // Keep the original Git root for display and the selected key for persistence. + let mut display_target = PathBuf::from(trust_target); + if host == ProjectTrustHost::Local + && LegacyAppPathString::from_string(trust_target) + .to_inferred_path_uri() + .as_ref() + == Some(&cwd_uri) + { + if local_roots.is_none() { + git_trust_root = resolve_root_git_project_for_trust( + LOCAL_FS.as_ref(), + &AbsolutePathBuf::from_absolute_path(&cwd)?, + ) + .await; + } + if let Some(root) = git_trust_root + && PathUri::from_abs_path(&root) == cwd_uri + && (root.as_path() != Path::new(&cwd) + || codex_utils_path::paths_match_after_normalization(&cwd, trust_target)) + { + display_target = root.into(); + } + } + Ok(Some(RemoteProjectTrust { trust_level: if explicitly_untrusted { Some(TrustLevel::Untrusted) @@ -400,7 +429,8 @@ pub(crate) async fn read_remote_project_trust( trust_level }, cwd: PathBuf::from(&cwd), - trust_target: PathBuf::from(trust_target), + trust_target: display_target, + trust_key: trust_target.to_owned(), })) } diff --git a/codex-rs/tui/src/config_update_tests.rs b/codex-rs/tui/src/config_update_tests.rs index 7f0f7c9efd..88f0a102f9 100644 --- a/codex-rs/tui/src/config_update_tests.rs +++ b/codex-rs/tui/src/config_update_tests.rs @@ -34,9 +34,8 @@ async fn remote_project_trust_guards_thread_start_and_preserves_repository_decis { let temp_dir = tempfile::tempdir()?; let codex_home = temp_dir.path().join("codex-home"); - let project_root = temp_dir - .path() - .join("project as a trusted project in sibling"); + let project_root = + dunce::canonicalize(temp_dir.path())?.join("Project as a trusted project in sibling"); let project_cwd = project_root.join("nested"); std::fs::create_dir_all(&codex_home)?; std::fs::create_dir_all(&project_cwd)?; @@ -80,6 +79,15 @@ async fn remote_project_trust_guards_thread_start_and_preserves_repository_decis .contains("directory path is not valid UTF-8") ); } + assert_eq!( + read_trust(&project_root, ProjectTrustHost::Local).await?, + Some(RemoteProjectTrust { + trust_level: None, + cwd: project_root.clone(), + trust_target: project_root.clone(), + trust_key: project_trust_key(&project_root), + }) + ); let relative_cwd = pathdiff::diff_paths(&project_cwd, std::env::current_dir()?) .ok_or_else(|| color_eyre::eyre::eyre!("failed to calculate relative project path"))?; @@ -89,6 +97,7 @@ async fn remote_project_trust_guards_thread_start_and_preserves_repository_decis trust_level: None, cwd: project_cwd.clone(), trust_target: PathBuf::from(project_trust_key(&project_root)), + trust_key: project_trust_key(&project_root), }) ); assert_eq!( @@ -128,6 +137,7 @@ async fn remote_project_trust_guards_thread_start_and_preserves_repository_decis trust_level: Some(TrustLevel::Untrusted), cwd: project_cwd.clone(), trust_target: project_cwd.clone(), + trust_key: project_cwd.to_string_lossy().into_owned(), }) ); @@ -214,6 +224,7 @@ async fn remote_project_trust_guards_thread_start_and_preserves_repository_decis trust_level: Some(TrustLevel::Untrusted), cwd: canonical_project_cwd.clone(), trust_target: PathBuf::from(project_trust_key(&project_root)), + trust_key: project_trust_key(&project_root), }) ); write_trusted_project(app_server.request_handle(), &project_root).await?; @@ -279,6 +290,7 @@ async fn remote_project_trust_guards_thread_start_and_preserves_repository_decis trust_level: Some(TrustLevel::Untrusted), cwd: canonical_project_cwd.clone(), trust_target: PathBuf::from(project_trust_key(&project_root)), + trust_key: project_trust_key(&project_root), }) ); } diff --git a/codex-rs/tui/src/onboarding/directory_trust.rs b/codex-rs/tui/src/onboarding/directory_trust.rs index 990d6d1f44..3b308a9a11 100644 --- a/codex-rs/tui/src/onboarding/directory_trust.rs +++ b/codex-rs/tui/src/onboarding/directory_trust.rs @@ -92,8 +92,8 @@ pub(crate) async fn check_directory_trust( "Folder trust changed. Reopen the destination with its restricted settings." ); } - let remote_trust_key = - connected.then(|| project.trust_target.to_string_lossy().into_owned()); + let remote_trust_key = (connected || project.trust_target != Path::new(&project.trust_key)) + .then_some(project.trust_key); if let Some(draft) = startup_draft.as_deref_mut() { draft.flush_pending_events(tui).await?; } diff --git a/codex-rs/tui/src/onboarding/onboarding_screen.rs b/codex-rs/tui/src/onboarding/onboarding_screen.rs index 2a278b0420..0c2bbf8769 100644 --- a/codex-rs/tui/src/onboarding/onboarding_screen.rs +++ b/codex-rs/tui/src/onboarding/onboarding_screen.rs @@ -152,7 +152,7 @@ impl OnboardingScreen { let cwd = config.cwd.to_path_buf(); let remote_trust_key = remote_project_trust .as_ref() - .map(|project| project.trust_target.to_string_lossy().into_owned()); + .map(|project| project.trust_key.clone()); let auth_config = config.auth_config(); let mut steps: Vec = Vec::new(); steps.push(Step::Welcome(WelcomeWidget::new( diff --git a/codex-rs/tui/src/onboarding/snapshots/codex_tui__onboarding__trust_directory__tests__renders_snapshot_for_git_repo.snap b/codex-rs/tui/src/onboarding/snapshots/codex_tui__onboarding__trust_directory__tests__renders_snapshot_for_git_repo.snap index 1c94e842ed..3608da8f46 100644 --- a/codex-rs/tui/src/onboarding/snapshots/codex_tui__onboarding__trust_directory__tests__renders_snapshot_for_git_repo.snap +++ b/codex-rs/tui/src/onboarding/snapshots/codex_tui__onboarding__trust_directory__tests__renders_snapshot_for_git_repo.snap @@ -1,17 +1,17 @@ --- source: tui/src/onboarding/trust_directory.rs -expression: terminal.backend() +expression: "screen.lines().map(str::trim_end).join(\"\\n\")" --- Folder access - /workspace/project + C:\Example\Project Trust this folder? Codex can read, edit, and run files here, subject to your permission settings. Folder settings can run code automatically, even without a model request. Continue only if you trust these files. Your trust decision will be saved. -› 1. Trust and continue +› 1. Trust and continue 2. Quit enter continue and create sandbox · esc quit diff --git a/codex-rs/tui/src/onboarding/trust_directory.rs b/codex-rs/tui/src/onboarding/trust_directory.rs index 266e74f5e6..4ed508cac7 100644 --- a/codex-rs/tui/src/onboarding/trust_directory.rs +++ b/codex-rs/tui/src/onboarding/trust_directory.rs @@ -284,6 +284,7 @@ mod tests { use crossterm::event::KeyEvent; use crossterm::event::KeyEventKind; use crossterm::event::KeyModifiers; + use itertools::Itertools; use pretty_assertions::assert_eq; use ratatui::Terminal; use std::path::PathBuf; @@ -356,6 +357,8 @@ mod tests { #[test] fn renders_snapshot_for_git_repo() { let mut widget = widget(/*error*/ None); + widget.cwd = PathBuf::from(r"C:\Example\Project"); + widget.trust_target = widget.cwd.clone(); widget.show_windows_create_sandbox_hint = true; let mut terminal = @@ -364,7 +367,27 @@ mod tests { .draw(|f| (&widget).render_ref(f.area(), f.buffer_mut())) .expect("draw"); - insta::assert_snapshot!(terminal.backend()); + let screen = terminal.backend().to_string(); + insta::assert_snapshot!(screen.lines().map(str::trim_end).join("\n")); + } + + #[test] + fn distinct_trust_targets_remain_visible() { + for (cwd, target) in [ + (r"C:\Example\Project\nested", r"c:\example\project"), + (r"C:\Example\Project", r"c:\example\project"), + ("/workspace/Project", "/workspace/project"), + ] { + let mut widget = widget(/*error*/ None); + widget.cwd = cwd.into(); + widget.trust_target = target.into(); + let mut terminal = + Terminal::new(VT100Backend::new(/*width*/ 70, /*height*/ 18)).expect("terminal"); + terminal + .draw(|f| (&widget).render_ref(f.area(), f.buffer_mut())) + .expect("draw"); + assert!(terminal.backend().to_string().contains(target)); + } } #[test]