Skip to content

Commit cbb1df4

Browse files
committed
Add support for rustc sysroot
Signed-off-by: xizheyin <[email protected]>
1 parent 6b0c39e commit cbb1df4

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

compiler/rustc_errors/src/emitter.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use std::error::Report;
1313
use std::io::prelude::*;
1414
use std::io::{self, IsTerminal};
1515
use std::iter;
16-
use std::path::Path;
16+
use std::path::{Path, PathBuf};
17+
use std::process::Command;
1718
use std::sync::Arc;
1819

1920
use derive_setters::Setters;
@@ -3571,9 +3572,22 @@ fn is_external_library(sm: &SourceMap, span: Span) -> bool {
35713572

35723573
let registry_path = cargo_home.join("registry").join("src");
35733574
let toolchain_path = rustup_home.join("toolchains");
3575+
let sysroot = get_rustc_sysroot();
35743576

3575-
path.starts_with(&registry_path) || path.starts_with(&toolchain_path)
3577+
path.starts_with(&registry_path)
3578+
|| path.starts_with(&toolchain_path)
3579+
|| sysroot.as_ref().map_or(false, |sr| path.starts_with(sr))
35763580
} else {
35773581
false
35783582
}
35793583
}
3584+
3585+
fn get_rustc_sysroot() -> Option<PathBuf> {
3586+
let rustc_path = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string());
3587+
match Command::new(rustc_path).arg("--print").arg("sysroot").output() {
3588+
Ok(output) if output.status.success() => {
3589+
String::from_utf8(output.stdout).ok().map(|s| PathBuf::from(s.trim()))
3590+
}
3591+
_ => None,
3592+
}
3593+
}

0 commit comments

Comments
 (0)