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
This commit is contained in:
Eric Traut
2026-11-30 18:05:37 +00:00
committed by copyberry
parent 81425480f1
commit 1b06a7002e
6 changed files with 78 additions and 13 deletions
+33 -3
View File
@@ -51,6 +51,7 @@ pub(crate) struct RemoteProjectTrust {
pub trust_level: Option<TrustLevel>,
pub cwd: PathBuf,
pub trust_target: PathBuf,
pub trust_key: String,
}
pub(crate) fn replace_config_value(key_path: impl Into<String>, 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::<Option<Vec<String>>>(
@@ -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(),
}))
}
+15 -3
View File
@@ -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),
})
);
}
@@ -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?;
}
@@ -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<Step> = Vec::new();
steps.push(Step::Welcome(WelcomeWidget::new(
@@ -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
+24 -1
View File
@@ -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]