Skip to content

Canonicalize path when displaying a FileName::Real #83345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ impl std::fmt::Display for FileName {
impl From<PathBuf> for FileName {
fn from(p: PathBuf) -> Self {
assert!(!p.to_string_lossy().ends_with('>'));
FileName::Real(RealFileName::Named(p))
if cfg!(not(windows)) {
FileName::Real(RealFileName::Named(p.canonicalize().unwrap_or(p)))
} else {
FileName::Real(RealFileName::Named(p))
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/test/run-make-fulldeps/libtest-json/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
OUTPUT_FILE_DEFAULT := $(TMPDIR)/libtest-json-output-default.json
OUTPUT_FILE_STDOUT_SUCCESS := $(TMPDIR)/libtest-json-output-stdout-success.json

CANONIZED_FILE_DEFAULT := $(TMPDIR)/libtest-json-output-default-canonized.json
CANONIZED_FILE_STDOUT_SUCCESS := $(TMPDIR)/libtest-json-output-stdout-success-canonized.json

all:
$(RUSTC) --test f.rs
RUST_BACKTRACE=0 $(call RUN,f) -Z unstable-options --test-threads=1 --format=json > $(OUTPUT_FILE_DEFAULT) || true
Expand All @@ -13,6 +16,9 @@ all:
cat $(OUTPUT_FILE_DEFAULT) | "$(PYTHON)" validate_json.py
cat $(OUTPUT_FILE_STDOUT_SUCCESS) | "$(PYTHON)" validate_json.py

cat output-default.json | "$(PYTHON)" canonicalize_path.py > $(CANONIZED_FILE_DEFAULT) || true
cat output-stdout-success.json | "$(PYTHON)" canonicalize_path.py > $(CANONIZED_FILE_STDOUT_SUCCESS) || true

# Normalize the actual output and compare to expected output file
cat $(OUTPUT_FILE_DEFAULT) | sed 's/"exec_time": [0-9.]*/"exec_time": $$TIME/' | diff output-default.json -
cat $(OUTPUT_FILE_STDOUT_SUCCESS) | sed 's/"exec_time": [0-9.]*/"exec_time": $$TIME/' | diff output-stdout-success.json -
cat $(OUTPUT_FILE_DEFAULT) | sed 's/"exec_time": [0-9.]*/"exec_time": $/"IGNORED"/' | diff $(CANONIZED_FILE_DEFAULT) -
cat $(OUTPUT_FILE_STDOUT_SUCCESS) | sed 's/"exec_time": [0-9.]*/"exec_time": $/"IGNORED"/' | diff $(CANONIZED_FILE_STDOUT_SUCCESS) -
14 changes: 14 additions & 0 deletions src/test/run-make-fulldeps/libtest-json/canonicalize_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python

import sys
import json
import os.path

for line in sys.stdin:
json_data = json.loads(line)
if "stdout" in json_data and "f.rs" in json_data["stdout"]:
normalized_path = os.path.join(os.getcwd(), "f.rs")
json_data["stdout"] = json_data["stdout"].replace("f.rs", normalized_path)
result = json.dumps(json_data).replace("{", "{ ")
result = result.replace("}", " }")
print(result)
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
{ "type": "test", "name": "c", "event": "ok" }
{ "type": "test", "event": "started", "name": "d" }
{ "type": "test", "name": "d", "event": "ignored" }
{ "type": "suite", "event": "failed", "passed": 2, "failed": 1, "allowed_fail": 0, "ignored": 1, "measured": 0, "filtered_out": 0, "exec_time": $TIME }
{ "type": "suite", "event": "failed", "passed": 2, "failed": 1, "allowed_fail": 0, "ignored": 1, "measured": 0, "filtered_out": 0, "exec_time": "IGNORED" }
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
{ "type": "test", "name": "c", "event": "ok", "stdout": "thread 'main' panicked at 'assertion failed: false', f.rs:15:5\n" }
{ "type": "test", "event": "started", "name": "d" }
{ "type": "test", "name": "d", "event": "ignored" }
{ "type": "suite", "event": "failed", "passed": 2, "failed": 1, "allowed_fail": 0, "ignored": 1, "measured": 0, "filtered_out": 0, "exec_time": $TIME }
{ "type": "suite", "event": "failed", "passed": 2, "failed": 1, "allowed_fail": 0, "ignored": 1, "measured": 0, "filtered_out": 0, "exec_time": "IGNORED" }