Skip to content

Commit 4c3d2a5

Browse files
authored
fix: render cwd-relative paths in tui (#8771)
Display paths relative to the cwd before checking git roots so view image tool calls keep project-local names in jj/no-.git workspaces.
1 parent c92dbea commit 4c3d2a5

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

codex-rs/tui/src/diff_render.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,18 @@ fn render_change(change: &FileChange, out: &mut Vec<RtLine<'static>>, width: usi
299299
}
300300
}
301301

302+
/// Format a path for display relative to the current working directory when
303+
/// possible, keeping output stable in jj/no-`.git` workspaces (e.g. image
304+
/// tool calls should show `example.png` instead of an absolute path).
302305
pub(crate) fn display_path_for(path: &Path, cwd: &Path) -> String {
306+
if path.is_relative() {
307+
return path.display().to_string();
308+
}
309+
310+
if let Ok(stripped) = path.strip_prefix(cwd) {
311+
return stripped.display().to_string();
312+
}
313+
303314
let path_in_same_repo = match (get_git_repo_root(cwd), get_git_repo_root(path)) {
304315
(Some(cwd_repo), Some(path_repo)) => cwd_repo == path_repo,
305316
_ => false,
@@ -420,6 +431,7 @@ fn style_del() -> Style {
420431
mod tests {
421432
use super::*;
422433
use insta::assert_snapshot;
434+
use pretty_assertions::assert_eq;
423435
use ratatui::Terminal;
424436
use ratatui::backend::TestBackend;
425437
use ratatui::text::Text;
@@ -459,6 +471,26 @@ mod tests {
459471
assert_snapshot!(name, text);
460472
}
461473

474+
#[test]
475+
fn display_path_prefers_cwd_without_git_repo() {
476+
let cwd = if cfg!(windows) {
477+
PathBuf::from(r"C:\workspace\codex")
478+
} else {
479+
PathBuf::from("/workspace/codex")
480+
};
481+
let path = cwd.join("tui").join("example.png");
482+
483+
let rendered = display_path_for(&path, &cwd);
484+
485+
assert_eq!(
486+
rendered,
487+
PathBuf::from("tui")
488+
.join("example.png")
489+
.display()
490+
.to_string()
491+
);
492+
}
493+
462494
#[test]
463495
fn ui_snapshot_wrap_behavior_insert() {
464496
// Narrow width to force wrapping within our diff line rendering

codex-rs/tui2/src/diff_render.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,18 @@ fn render_change(change: &FileChange, out: &mut Vec<RtLine<'static>>, width: usi
299299
}
300300
}
301301

302+
/// Format a path for display relative to the current working directory when
303+
/// possible, keeping output stable in jj/no-`.git` workspaces (e.g. image
304+
/// tool calls should show `example.png` instead of an absolute path).
302305
pub(crate) fn display_path_for(path: &Path, cwd: &Path) -> String {
306+
if path.is_relative() {
307+
return path.display().to_string();
308+
}
309+
310+
if let Ok(stripped) = path.strip_prefix(cwd) {
311+
return stripped.display().to_string();
312+
}
313+
303314
// Prefer a stable, user-local relative path when the file is under the current working
304315
// directory. This keeps output deterministic in jj-only repos (no `.git`) and matches user
305316
// expectations for "files in this project".
@@ -431,6 +442,7 @@ fn style_del() -> Style {
431442
mod tests {
432443
use super::*;
433444
use insta::assert_snapshot;
445+
use pretty_assertions::assert_eq;
434446
use ratatui::Terminal;
435447
use ratatui::backend::TestBackend;
436448
use ratatui::text::Text;
@@ -470,6 +482,26 @@ mod tests {
470482
assert_snapshot!(name, text);
471483
}
472484

485+
#[test]
486+
fn display_path_prefers_cwd_without_git_repo() {
487+
let cwd = if cfg!(windows) {
488+
PathBuf::from(r"C:\workspace\codex")
489+
} else {
490+
PathBuf::from("/workspace/codex")
491+
};
492+
let path = cwd.join("tui").join("example.png");
493+
494+
let rendered = display_path_for(&path, &cwd);
495+
496+
assert_eq!(
497+
rendered,
498+
PathBuf::from("tui")
499+
.join("example.png")
500+
.display()
501+
.to_string()
502+
);
503+
}
504+
473505
#[test]
474506
fn ui_snapshot_wrap_behavior_insert() {
475507
// Narrow width to force wrapping within our diff line rendering

0 commit comments

Comments
 (0)