Skip to content

Commit 6b0c39e

Browse files
committed
use env variables to avoid hardcode, and use paltform independent path
Signed-off-by: xizheyin <[email protected]>
1 parent fd7029d commit 6b0c39e

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

compiler/rustc_errors/src/emitter.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -3544,8 +3544,35 @@ pub(crate) fn should_show_source_code(
35443544
fn is_external_library(sm: &SourceMap, span: Span) -> bool {
35453545
let filename = sm.span_to_filename(span);
35463546
if let Some(path) = filename.into_local_path() {
3547-
path.to_string_lossy().contains(".cargo/registry/src/")
3548-
|| path.to_string_lossy().contains(".rustup/toolchains/")
3547+
// use env variable to get path, avoid hardcode
3548+
// use platform independent path
3549+
3550+
let cargo_home = match std::env::var("CARGO_HOME") {
3551+
Ok(dir) => std::path::PathBuf::from(dir),
3552+
Err(_) => {
3553+
if let Ok(home) = std::env::var("HOME") {
3554+
std::path::PathBuf::from(home).join(".cargo")
3555+
} else {
3556+
return false;
3557+
}
3558+
}
3559+
};
3560+
3561+
let rustup_home = match std::env::var("RUSTUP_HOME") {
3562+
Ok(dir) => std::path::PathBuf::from(dir),
3563+
Err(_) => {
3564+
if let Ok(home) = std::env::var("HOME") {
3565+
std::path::PathBuf::from(home).join(".rustup")
3566+
} else {
3567+
return false;
3568+
}
3569+
}
3570+
};
3571+
3572+
let registry_path = cargo_home.join("registry").join("src");
3573+
let toolchain_path = rustup_home.join("toolchains");
3574+
3575+
path.starts_with(&registry_path) || path.starts_with(&toolchain_path)
35493576
} else {
35503577
false
35513578
}

0 commit comments

Comments
 (0)