Skip to content

Don't normalize paths in backtraces to be relative to the cwd. #47057

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
Closed
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
48 changes: 34 additions & 14 deletions src/libstd/sys_common/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use io::prelude::*;
use io;
use str;
use sync::atomic::{self, Ordering};
use path::{self, Path};
use sys::mutex::Mutex;
use ptr;

Expand Down Expand Up @@ -196,6 +195,39 @@ fn output(w: &mut Write, idx: usize, frame: Frame,
w.write_all(b"\n")
}

#[cfg(not(target_os = "cloudabi"))]
fn output_fileline_relative(w: &mut Write,
file: &str,
line: u32,
format: PrintFormat) -> io::Result<bool> {
use path::{self, Path};

let file_path = Path::new(file);
let mut already_printed = false;
if format == PrintFormat::Short && file_path.is_absolute() {
if let Ok(cwd) = env::current_dir() {
if let Ok(stripped) = file_path.strip_prefix(&cwd) {
if let Some(s) = stripped.to_str() {
write!(w, " at .{}{}:{}", path::MAIN_SEPARATOR, s, line)?;
already_printed = true;
}
}
}
}
Ok(already_printed)
}

#[cfg(target_os = "cloudabi")]
fn output_fileline_relative(_: &mut Write,
_: &str,
_: u32,
_: PrintFormat) -> io::Result<bool> {
// CloudABI does not provide a global file system namespace, nor a
// process working directory. Don't attempt to make pathnames
// relative to the working directory.
Ok(false)
}

/// Print the filename and line number of the backtrace frame.
///
/// See also `output`.
Expand All @@ -215,19 +247,7 @@ fn output_fileline(w: &mut Write,
}

let file = str::from_utf8(file).unwrap_or("<unknown>");
let file_path = Path::new(file);
let mut already_printed = false;
if format == PrintFormat::Short && file_path.is_absolute() {
if let Ok(cwd) = env::current_dir() {
if let Ok(stripped) = file_path.strip_prefix(&cwd) {
if let Some(s) = stripped.to_str() {
write!(w, " at .{}{}:{}", path::MAIN_SEPARATOR, s, line)?;
already_printed = true;
}
}
}
}
if !already_printed {
if !output_fileline_relative(w, file, line, format)? {
write!(w, " at {}:{}", file, line)?;
}

Expand Down