Skip to content

Normalize .. and . in diagnostic file paths#155569

Open
arferreira wants to merge 1 commit intorust-lang:mainfrom
arferreira:normalize-diagnostic-paths
Open

Normalize .. and . in diagnostic file paths#155569
arferreira wants to merge 1 commit intorust-lang:mainfrom
arferreira:normalize-diagnostic-paths

Conversation

@arferreira
Copy link
Copy Markdown
Contributor

@arferreira arferreira commented Apr 20, 2026

Fixes #51349

Lexically normalize . and .. in file paths when rendering diagnostics, so errors show foo.rs instead of sub/../foo.rs. Normalization is scoped to the new FileNameDisplayPreference::Diagnostics variant used by SourceMap::filename_for_diagnostics; file!(), debuginfo, and remapped/local/short paths are unchanged.
Avoids Path::canonicalize to keep relative paths relative (see #83345 for context). Compiletest gains a $DIR/.. substitution so existing .stderr files that referenced auxiliary paths above $DIR keep matching.

Previous attempt was #68654.

r? @estebank

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 20, 2026

Some changes occurred in src/tools/compiletest

cc @jieyouxu

@rustbot rustbot added A-compiletest Area: The compiletest test runner A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 20, 2026
@rust-log-analyzer

This comment has been minimized.

@arferreira arferreira force-pushed the normalize-diagnostic-paths branch from 72beb68 to e02a2f9 Compare April 20, 2026 17:46
Comment on lines +504 to +526
/// Lexically normalizes a path by resolving `.` and `..` components without
/// touching the filesystem. Preserves whether the path is relative or absolute.
///
/// Used for diagnostic display only. `Path::canonicalize` is intentionally avoided
/// because it turns relative paths into absolute ones (see #51349, #83345).
pub(crate) fn normalize_path(path: &Path) -> PathBuf {
let mut out = PathBuf::new();
for component in path.components() {
match component {
Component::CurDir => {}
Component::ParentDir => match out.components().next_back() {
Some(Component::Normal(_)) => {
out.pop();
}
Some(Component::RootDir) => {}
_ => out.push(component),
},
_ => out.push(component),
}
}
if out.as_os_str().is_empty() { PathBuf::from(".") } else { out }
}

Copy link
Copy Markdown
Member

@Urgau Urgau Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method can be replaced by the unstable Path::normalize_lexically method.

View changes since the review

Comment thread compiler/rustc_span/src/lib.rs Outdated
Comment on lines +564 to +573
fn maybe_normalize<'s>(&self, s: Cow<'s, str>) -> Cow<'s, str> {
if !matches!(self.display_pref, FileNameDisplayPreference::Diagnostics(_)) {
return s;
}
let path = Path::new(s.as_ref());
if !path.components().any(|c| matches!(c, Component::ParentDir | Component::CurDir)) {
return s;
}
Cow::Owned(normalize_path(path).into_os_string().to_string_lossy().into_owned())
}
Copy link
Copy Markdown
Member

@Urgau Urgau Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic seems better fitted in RealFileName::to_string_lossy.

Even better it seems to me that it would be simpler to just embedded it in the FileNameDisplayPreference::Diagnostics(scope) match arm.

View changes since the review

@arferreira arferreira force-pushed the normalize-diagnostic-paths branch from e02a2f9 to 7e7055d Compare April 20, 2026 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-compiletest Area: The compiletest test runner A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

compiler error message shows the error path containing parent directory identifier ( /../ )

5 participants