Skip to content

Commit 64c17a9

Browse files
committed
fix: rust-project.json projects not preferring sysroot rustc
1 parent d4d9d0c commit 64c17a9

File tree

6 files changed

+25
-18
lines changed

6 files changed

+25
-18
lines changed

crates/project-model/src/build_scripts.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,7 @@ impl WorkspaceBuildScripts {
440440
if let Ok(it) = utf8_stdout(cargo_config) {
441441
return Ok(it);
442442
}
443-
let mut cmd = Command::new(Tool::Rustc.path());
444-
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
443+
let mut cmd = Sysroot::rustc(sysroot);
445444
cmd.envs(extra_env);
446445
cmd.args(["--print", "target-libdir"]);
447446
utf8_stdout(cmd)

crates/project-model/src/cargo_workspace.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,7 @@ fn rustc_discover_host_triple(
501501
extra_env: &FxHashMap<String, String>,
502502
sysroot: Option<&Sysroot>,
503503
) -> Option<String> {
504-
let mut rustc = Command::new(Tool::Rustc.path());
505-
Sysroot::set_rustup_toolchain_env(&mut rustc, sysroot);
504+
let mut rustc = Sysroot::rustc(sysroot);
506505
rustc.envs(extra_env);
507506
rustc.current_dir(cargo_toml.parent()).arg("-vV");
508507
tracing::debug!("Discovering host platform by {:?}", rustc);

crates/project-model/src/rustc_cfg.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ fn get_rust_cfgs(
9090
RustcCfgConfig::Rustc(sysroot) => sysroot,
9191
};
9292

93-
let mut cmd = Command::new(toolchain::Tool::Rustc.path());
94-
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
93+
let mut cmd = Sysroot::rustc(sysroot);
9594
cmd.envs(extra_env);
9695
cmd.args(["--print", "cfg", "-O"]);
9796
if let Some(target) = target {

crates/project-model/src/sysroot.rs

+13
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ impl Sysroot {
199199
}
200200
}
201201

202+
/// Returns a `Command` that is configured to run `rustc` from the sysroot if it exists,
203+
/// otherwise returns what [toolchain::Tool::Rustc] returns.
204+
pub fn rustc(sysroot: Option<&Self>) -> Command {
205+
let mut cmd = Command::new(match sysroot {
206+
Some(sysroot) => {
207+
toolchain::Tool::Rustc.path_in_or_discover(sysroot.root.join("bin").as_ref())
208+
}
209+
None => toolchain::Tool::Rustc.path(),
210+
});
211+
Self::set_rustup_toolchain_env(&mut cmd, sysroot);
212+
cmd
213+
}
214+
202215
pub fn discover_proc_macro_srv(&self) -> anyhow::Result<AbsPathBuf> {
203216
["libexec", "lib"]
204217
.into_iter()

crates/project-model/src/target_data_layout.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ pub fn get(
5757
RustcDataLayoutConfig::Rustc(sysroot) => sysroot,
5858
};
5959

60-
let mut cmd = Command::new(toolchain::Tool::Rustc.path());
61-
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
60+
let mut cmd = Sysroot::rustc(sysroot);
6261
cmd.envs(extra_env)
6362
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
6463
.env("RUSTC_BOOTSTRAP", "1");

crates/project-model/src/workspace.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,11 @@ impl fmt::Debug for ProjectWorkspace {
172172

173173
fn get_toolchain_version(
174174
current_dir: &AbsPath,
175-
sysroot: Option<&Sysroot>,
176-
tool: Tool,
175+
mut cmd: Command,
177176
extra_env: &FxHashMap<String, String>,
178177
prefix: &str,
179178
) -> Result<Option<Version>, anyhow::Error> {
180179
let cargo_version = utf8_stdout({
181-
let mut cmd = Command::new(tool.path());
182-
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
183180
cmd.envs(extra_env);
184181
cmd.arg("--version").current_dir(current_dir);
185182
cmd
@@ -300,8 +297,11 @@ impl ProjectWorkspace {
300297

301298
let toolchain = get_toolchain_version(
302299
cargo_toml.parent(),
303-
sysroot_ref,
304-
toolchain::Tool::Cargo,
300+
{
301+
let mut cmd = Command::new(toolchain::Tool::Cargo.path());
302+
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot_ref);
303+
cmd
304+
},
305305
&config.extra_env,
306306
"cargo ",
307307
)?;
@@ -386,8 +386,7 @@ impl ProjectWorkspace {
386386
let data_layout_config = RustcDataLayoutConfig::Rustc(sysroot_ref);
387387
let toolchain = match get_toolchain_version(
388388
project_json.path(),
389-
sysroot_ref,
390-
toolchain::Tool::Rustc,
389+
Sysroot::rustc(sysroot_ref),
391390
extra_env,
392391
"rustc ",
393392
) {
@@ -436,8 +435,7 @@ impl ProjectWorkspace {
436435
let sysroot_ref = sysroot.as_ref().ok();
437436
let toolchain = match get_toolchain_version(
438437
dir,
439-
sysroot_ref,
440-
toolchain::Tool::Rustc,
438+
Sysroot::rustc(sysroot_ref),
441439
&config.extra_env,
442440
"rustc ",
443441
) {

0 commit comments

Comments
 (0)