Skip to content

Commit 839409a

Browse files
committed
project-model: when using rust-project.json, prefer the sysroot-defined rustc over an env-based one
1 parent caeea45 commit 839409a

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

crates/project-model/src/rustc_cfg.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use std::process::Command;
44

55
use rustc_hash::FxHashMap;
66

7-
use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath};
7+
use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath, Sysroot};
88

99
pub(crate) fn get(
1010
cargo_toml: Option<&ManifestPath>,
11+
sysroot: Option<&Sysroot>,
1112
target: Option<&str>,
1213
extra_env: &FxHashMap<String, String>,
1314
) -> Vec<CfgFlag> {
@@ -25,7 +26,7 @@ pub(crate) fn get(
2526
// Add miri cfg, which is useful for mir eval in stdlib
2627
res.push(CfgFlag::Atom("miri".into()));
2728

28-
match get_rust_cfgs(cargo_toml, target, extra_env) {
29+
match get_rust_cfgs(cargo_toml, sysroot, target, extra_env) {
2930
Ok(rustc_cfgs) => {
3031
tracing::debug!(
3132
"rustc cfgs found: {:?}",
@@ -44,6 +45,7 @@ pub(crate) fn get(
4445

4546
fn get_rust_cfgs(
4647
cargo_toml: Option<&ManifestPath>,
48+
sysroot: Option<&Sysroot>,
4749
target: Option<&str>,
4850
extra_env: &FxHashMap<String, String>,
4951
) -> anyhow::Result<String> {
@@ -62,8 +64,22 @@ fn get_rust_cfgs(
6264
Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
6365
}
6466
}
67+
68+
let rustc = match sysroot {
69+
Some(sysroot) => {
70+
let rustc = sysroot.discover_rustc()?.into();
71+
tracing::debug!(?rustc, "using rustc from sysroot");
72+
rustc
73+
}
74+
None => {
75+
let rustc = toolchain::rustc();
76+
tracing::debug!(?rustc, "using rustc from env");
77+
rustc
78+
}
79+
};
80+
6581
// using unstable cargo features failed, fall back to using plain rustc
66-
let mut cmd = Command::new(toolchain::rustc());
82+
let mut cmd = Command::new(rustc);
6783
cmd.envs(extra_env);
6884
cmd.args(["--print", "cfg", "-O"]);
6985
if let Some(target) = target {

crates/project-model/src/sysroot.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,19 @@ impl Sysroot {
115115
Ok(Sysroot::load(sysroot_dir, src))
116116
}
117117

118-
pub fn discover_rustc(&self) -> Option<ManifestPath> {
118+
pub fn discover_rustc_src(&self) -> Option<ManifestPath> {
119119
get_rustc_src(&self.root)
120120
}
121121

122+
pub fn discover_rustc(&self) -> Result<AbsPathBuf, std::io::Error> {
123+
let rustc = self.root.join("bin/rustc");
124+
tracing::debug!(?rustc, "checking for rustc binary at location");
125+
match fs::metadata(&rustc) {
126+
Ok(_) => Ok(rustc),
127+
Err(e) => Err(e),
128+
}
129+
}
130+
122131
pub fn with_sysroot_dir(sysroot_dir: AbsPathBuf) -> Result<Sysroot> {
123132
let sysroot_src_dir = discover_sysroot_src_dir(&sysroot_dir).ok_or_else(|| {
124133
format_err!("can't load standard library from sysroot path {sysroot_dir}")

crates/project-model/src/workspace.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ impl ProjectWorkspace {
240240
Some(RustLibSource::Path(path)) => ManifestPath::try_from(path.clone())
241241
.map_err(|p| Some(format!("rustc source path is not absolute: {p}"))),
242242
Some(RustLibSource::Discover) => {
243-
sysroot.as_ref().ok().and_then(Sysroot::discover_rustc).ok_or_else(|| {
244-
Some(format!("Failed to discover rustc source for sysroot."))
245-
})
243+
sysroot.as_ref().ok().and_then(Sysroot::discover_rustc_src).ok_or_else(
244+
|| Some(format!("Failed to discover rustc source for sysroot.")),
245+
)
246246
}
247247
None => Err(None),
248248
};
@@ -279,8 +279,12 @@ impl ProjectWorkspace {
279279
}
280280
});
281281

282-
let rustc_cfg =
283-
rustc_cfg::get(Some(&cargo_toml), config.target.as_deref(), &config.extra_env);
282+
let rustc_cfg = rustc_cfg::get(
283+
Some(&cargo_toml),
284+
None,
285+
config.target.as_deref(),
286+
&config.extra_env,
287+
);
284288

285289
let cfg_overrides = config.cfg_overrides.clone();
286290
let data_layout = target_data_layout::get(
@@ -335,7 +339,7 @@ impl ProjectWorkspace {
335339
tracing::info!(src_root = %sysroot.src_root(), root = %sysroot.root(), "Using sysroot");
336340
}
337341

338-
let rustc_cfg = rustc_cfg::get(None, target, extra_env);
342+
let rustc_cfg = rustc_cfg::get(None, sysroot.as_ref().ok(), target, extra_env);
339343
ProjectWorkspace::Json { project: project_json, sysroot, rustc_cfg, toolchain }
340344
}
341345

@@ -360,7 +364,7 @@ impl ProjectWorkspace {
360364
if let Ok(sysroot) = &sysroot {
361365
tracing::info!(src_root = %sysroot.src_root(), root = %sysroot.root(), "Using sysroot");
362366
}
363-
let rustc_cfg = rustc_cfg::get(None, None, &Default::default());
367+
let rustc_cfg = rustc_cfg::get(None, sysroot.as_ref().ok(), None, &Default::default());
364368
Ok(ProjectWorkspace::DetachedFiles { files: detached_files, sysroot, rustc_cfg })
365369
}
366370

@@ -756,7 +760,7 @@ fn project_json_to_crate_graph(
756760
let target_cfgs = match target.as_deref() {
757761
Some(target) => cfg_cache
758762
.entry(target)
759-
.or_insert_with(|| rustc_cfg::get(None, Some(target), extra_env)),
763+
.or_insert_with(|| rustc_cfg::get(None, sysroot, Some(target), extra_env)),
760764
None => &rustc_cfg,
761765
};
762766

0 commit comments

Comments
 (0)