Skip to content

Commit b85d38f

Browse files
committed
Auto merge of #16755 - Veykril:rustup-bins, r=Veykril
For toolchain binaries use the full path found in $PATH Fixes #16754
2 parents 1a55ab3 + 6b48133 commit b85d38f

File tree

12 files changed

+117
-131
lines changed

12 files changed

+117
-131
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ide/src/expand_macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ fn _format(
189189
let &crate_id = db.relevant_crates(file_id).iter().next()?;
190190
let edition = db.crate_graph()[crate_id].edition;
191191

192-
let mut cmd = std::process::Command::new(toolchain::rustfmt());
192+
let mut cmd = std::process::Command::new(toolchain::Tool::Rustfmt.path());
193193
cmd.arg("--edition");
194194
cmd.arg(edition.to_string());
195195

crates/proc-macro-srv/proc-macro-test/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,3 @@ doctest = false
1111

1212
[build-dependencies]
1313
cargo_metadata = "0.18.1"
14-
15-
# local deps
16-
toolchain.workspace = true

crates/proc-macro-srv/proc-macro-test/build.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ use cargo_metadata::Message;
1818
fn main() {
1919
println!("cargo:rerun-if-changed=imp");
2020

21+
let cargo = env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
22+
2123
let has_features = env::var_os("RUSTC_BOOTSTRAP").is_some()
22-
|| String::from_utf8(
23-
Command::new(toolchain::cargo()).arg("--version").output().unwrap().stdout,
24-
)
25-
.unwrap()
26-
.contains("nightly");
24+
|| String::from_utf8(Command::new(&cargo).arg("--version").output().unwrap().stdout)
25+
.unwrap()
26+
.contains("nightly");
2727

2828
let out_dir = env::var_os("OUT_DIR").unwrap();
2929
let out_dir = Path::new(&out_dir);
@@ -66,7 +66,7 @@ fn main() {
6666

6767
let target_dir = out_dir.join("target");
6868

69-
let mut cmd = Command::new(toolchain::cargo());
69+
let mut cmd = Command::new(&cargo);
7070
cmd.current_dir(&staging_dir)
7171
.args(["build", "-p", "proc-macro-test-impl", "--message-format", "json"])
7272
// Explicit override the target directory to avoid using the same one which the parent
@@ -96,7 +96,7 @@ fn main() {
9696
let repr = format!("{name} {version}");
9797
// New Package Id Spec since rust-lang/cargo#13311
9898
let pkgid = String::from_utf8(
99-
Command::new(toolchain::cargo())
99+
Command::new(cargo)
100100
.current_dir(&staging_dir)
101101
.args(["pkgid", name])
102102
.output()

crates/project-model/src/build_scripts.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ impl WorkspaceBuildScripts {
7171
cmd
7272
}
7373
_ => {
74-
let mut cmd = Command::new(Tool::Cargo.path());
75-
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
74+
let mut cmd = Sysroot::tool(sysroot, Tool::Cargo);
7675

7776
cmd.args(["check", "--quiet", "--workspace", "--message-format=json"]);
7877
cmd.args(&config.extra_args);
@@ -430,8 +429,7 @@ impl WorkspaceBuildScripts {
430429
}
431430
let res = (|| {
432431
let target_libdir = (|| {
433-
let mut cargo_config = Command::new(Tool::Cargo.path());
434-
Sysroot::set_rustup_toolchain_env(&mut cargo_config, sysroot);
432+
let mut cargo_config = Sysroot::tool(sysroot, Tool::Cargo);
435433
cargo_config.envs(extra_env);
436434
cargo_config
437435
.current_dir(current_dir)
@@ -440,7 +438,7 @@ impl WorkspaceBuildScripts {
440438
if let Ok(it) = utf8_stdout(cargo_config) {
441439
return Ok(it);
442440
}
443-
let mut cmd = Sysroot::rustc(sysroot);
441+
let mut cmd = Sysroot::tool(sysroot, Tool::Rustc);
444442
cmd.envs(extra_env);
445443
cmd.args(["--print", "target-libdir"]);
446444
utf8_stdout(cmd)

crates/project-model/src/cargo_workspace.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! See [`CargoWorkspace`].
22
3+
use std::ops;
34
use std::path::PathBuf;
45
use std::str::from_utf8;
5-
use std::{ops, process::Command};
66

77
use anyhow::Context;
88
use base_db::Edition;
@@ -243,8 +243,11 @@ impl CargoWorkspace {
243243
) -> anyhow::Result<cargo_metadata::Metadata> {
244244
let targets = find_list_of_build_targets(config, cargo_toml, sysroot);
245245

246+
let cargo = Sysroot::tool(sysroot, Tool::Cargo);
246247
let mut meta = MetadataCommand::new();
247-
meta.cargo_path(Tool::Cargo.path());
248+
meta.cargo_path(cargo.get_program());
249+
cargo.get_envs().for_each(|(var, val)| _ = meta.env(var, val.unwrap_or_default()));
250+
config.extra_env.iter().for_each(|(var, val)| _ = meta.env(var, val));
248251
meta.manifest_path(cargo_toml.to_path_buf());
249252
match &config.features {
250253
CargoFeatures::All => {
@@ -291,10 +294,7 @@ impl CargoWorkspace {
291294
progress("metadata".to_owned());
292295

293296
(|| -> Result<cargo_metadata::Metadata, cargo_metadata::Error> {
294-
let mut command = meta.cargo_command();
295-
Sysroot::set_rustup_toolchain_env(&mut command, sysroot);
296-
command.envs(&config.extra_env);
297-
let output = command.output()?;
297+
let output = meta.cargo_command().output()?;
298298
if !output.status.success() {
299299
return Err(cargo_metadata::Error::CargoMetadata {
300300
stderr: String::from_utf8(output.stderr)?,
@@ -501,7 +501,7 @@ fn rustc_discover_host_triple(
501501
extra_env: &FxHashMap<String, String>,
502502
sysroot: Option<&Sysroot>,
503503
) -> Option<String> {
504-
let mut rustc = Sysroot::rustc(sysroot);
504+
let mut rustc = Sysroot::tool(sysroot, Tool::Rustc);
505505
rustc.envs(extra_env);
506506
rustc.current_dir(cargo_toml.parent()).arg("-vV");
507507
tracing::debug!("Discovering host platform by {:?}", rustc);
@@ -529,8 +529,7 @@ fn cargo_config_build_target(
529529
extra_env: &FxHashMap<String, String>,
530530
sysroot: Option<&Sysroot>,
531531
) -> Vec<String> {
532-
let mut cargo_config = Command::new(Tool::Cargo.path());
533-
Sysroot::set_rustup_toolchain_env(&mut cargo_config, sysroot);
532+
let mut cargo_config = Sysroot::tool(sysroot, Tool::Cargo);
534533
cargo_config.envs(extra_env);
535534
cargo_config
536535
.current_dir(cargo_toml.parent())

crates/project-model/src/rustc_cfg.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! Runs `rustc --print cfg` to get built-in cfg flags.
22
3-
use std::process::Command;
4-
53
use anyhow::Context;
64
use rustc_hash::FxHashMap;
5+
use toolchain::Tool;
76

87
use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath, Sysroot};
98

@@ -69,8 +68,8 @@ fn get_rust_cfgs(
6968
) -> anyhow::Result<String> {
7069
let sysroot = match config {
7170
RustcCfgConfig::Cargo(sysroot, cargo_toml) => {
72-
let mut cmd = Command::new(toolchain::Tool::Cargo.path());
73-
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
71+
let mut cmd = Sysroot::tool(sysroot, Tool::Cargo);
72+
7473
cmd.envs(extra_env);
7574
cmd.current_dir(cargo_toml.parent())
7675
.args(["rustc", "-Z", "unstable-options", "--print", "cfg"])
@@ -90,7 +89,7 @@ fn get_rust_cfgs(
9089
RustcCfgConfig::Rustc(sysroot) => sysroot,
9190
};
9291

93-
let mut cmd = Sysroot::rustc(sysroot);
92+
let mut cmd = Sysroot::tool(sysroot, Tool::Rustc);
9493
cmd.envs(extra_env);
9594
cmd.args(["--print", "cfg", "-O"]);
9695
if let Some(target) = target {

crates/project-model/src/sysroot.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use itertools::Itertools;
1212
use la_arena::{Arena, Idx};
1313
use paths::{AbsPath, AbsPathBuf};
1414
use rustc_hash::FxHashMap;
15-
use toolchain::probe_for_binary;
15+
use toolchain::{probe_for_binary, Tool};
1616

1717
use crate::{utf8_stdout, CargoConfig, CargoWorkspace, ManifestPath};
1818

@@ -193,23 +193,26 @@ impl Sysroot {
193193
Ok(Sysroot::load(sysroot_dir, Some(sysroot_src_dir), metadata))
194194
}
195195

196-
pub fn set_rustup_toolchain_env(cmd: &mut Command, sysroot: Option<&Self>) {
197-
if let Some(sysroot) = sysroot {
198-
cmd.env("RUSTUP_TOOLCHAIN", AsRef::<std::path::Path>::as_ref(&sysroot.root));
199-
}
200-
}
201-
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 {
196+
/// Returns a command to run a tool preferring the cargo proxies if the sysroot exists.
197+
pub fn tool(sysroot: Option<&Self>, tool: Tool) -> Command {
198+
match sysroot {
206199
Some(sysroot) => {
207-
toolchain::Tool::Rustc.path_in_or_discover(sysroot.root.join("bin").as_ref())
200+
// special case rustc, we can look that up directly in the sysroot's bin folder
201+
// as it should never invoke another cargo binary
202+
if let Tool::Rustc = tool {
203+
if let Some(path) =
204+
probe_for_binary(sysroot.root.join("bin").join(Tool::Rustc.name()).into())
205+
{
206+
return Command::new(path);
207+
}
208+
}
209+
210+
let mut cmd = Command::new(tool.prefer_proxy());
211+
cmd.env("RUSTUP_TOOLCHAIN", AsRef::<std::path::Path>::as_ref(&sysroot.root));
212+
cmd
208213
}
209-
None => toolchain::Tool::Rustc.path(),
210-
});
211-
Self::set_rustup_toolchain_env(&mut cmd, sysroot);
212-
cmd
214+
_ => Command::new(tool.path()),
215+
}
213216
}
214217

215218
pub fn discover_proc_macro_srv(&self) -> anyhow::Result<AbsPathBuf> {
@@ -411,7 +414,7 @@ fn discover_sysroot_dir(
411414
current_dir: &AbsPath,
412415
extra_env: &FxHashMap<String, String>,
413416
) -> Result<AbsPathBuf> {
414-
let mut rustc = Command::new(toolchain::rustc());
417+
let mut rustc = Command::new(Tool::Rustc.path());
415418
rustc.envs(extra_env);
416419
rustc.current_dir(current_dir).args(["--print", "sysroot"]);
417420
tracing::debug!("Discovering sysroot by {:?}", rustc);
@@ -443,7 +446,7 @@ fn discover_sysroot_src_dir_or_add_component(
443446
) -> Result<AbsPathBuf> {
444447
discover_sysroot_src_dir(sysroot_path)
445448
.or_else(|| {
446-
let mut rustup = Command::new(toolchain::rustup());
449+
let mut rustup = Command::new(Tool::Rustup.prefer_proxy());
447450
rustup.envs(extra_env);
448451
rustup.current_dir(current_dir).args(["component", "add", "rust-src"]);
449452
tracing::info!("adding rust-src component by {:?}", rustup);

crates/project-model/src/target_data_layout.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Runs `rustc --print target-spec-json` to get the target_data_layout.
2-
use std::process::Command;
32
43
use rustc_hash::FxHashMap;
4+
use toolchain::Tool;
55

66
use crate::{utf8_stdout, ManifestPath, Sysroot};
77

@@ -28,8 +28,7 @@ pub fn get(
2828
};
2929
let sysroot = match config {
3030
RustcDataLayoutConfig::Cargo(sysroot, cargo_toml) => {
31-
let mut cmd = Command::new(toolchain::Tool::Cargo.path());
32-
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
31+
let mut cmd = Sysroot::tool(sysroot, Tool::Cargo);
3332
cmd.envs(extra_env);
3433
cmd.current_dir(cargo_toml.parent())
3534
.args([
@@ -57,7 +56,7 @@ pub fn get(
5756
RustcDataLayoutConfig::Rustc(sysroot) => sysroot,
5857
};
5958

60-
let mut cmd = Sysroot::rustc(sysroot);
59+
let mut cmd = Sysroot::tool(sysroot, Tool::Rustc);
6160
cmd.envs(extra_env)
6261
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
6362
.env("RUSTC_BOOTSTRAP", "1");

crates/project-model/src/workspace.rs

+18-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! metadata` or `rust-project.json`) into representation stored in the salsa
33
//! database -- `CrateGraph`.
44
5-
use std::{collections::VecDeque, fmt, fs, iter, process::Command, str::FromStr, sync};
5+
use std::{collections::VecDeque, fmt, fs, iter, str::FromStr, sync};
66

77
use anyhow::{format_err, Context};
88
use base_db::{
@@ -172,11 +172,13 @@ impl fmt::Debug for ProjectWorkspace {
172172

173173
fn get_toolchain_version(
174174
current_dir: &AbsPath,
175-
mut cmd: Command,
175+
sysroot: Option<&Sysroot>,
176+
tool: Tool,
176177
extra_env: &FxHashMap<String, String>,
177178
prefix: &str,
178179
) -> Result<Option<Version>, anyhow::Error> {
179180
let cargo_version = utf8_stdout({
181+
let mut cmd = Sysroot::tool(sysroot, tool);
180182
cmd.envs(extra_env);
181183
cmd.arg("--version").current_dir(current_dir);
182184
cmd
@@ -297,11 +299,8 @@ impl ProjectWorkspace {
297299

298300
let toolchain = get_toolchain_version(
299301
cargo_toml.parent(),
300-
{
301-
let mut cmd = Command::new(toolchain::Tool::Cargo.path());
302-
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot_ref);
303-
cmd
304-
},
302+
sysroot_ref,
303+
Tool::Cargo,
305304
&config.extra_env,
306305
"cargo ",
307306
)?;
@@ -386,7 +385,8 @@ impl ProjectWorkspace {
386385
let data_layout_config = RustcDataLayoutConfig::Rustc(sysroot_ref);
387386
let toolchain = match get_toolchain_version(
388387
project_json.path(),
389-
Sysroot::rustc(sysroot_ref),
388+
sysroot_ref,
389+
Tool::Rustc,
390390
extra_env,
391391
"rustc ",
392392
) {
@@ -433,18 +433,15 @@ impl ProjectWorkspace {
433433
};
434434

435435
let sysroot_ref = sysroot.as_ref().ok();
436-
let toolchain = match get_toolchain_version(
437-
dir,
438-
Sysroot::rustc(sysroot_ref),
439-
&config.extra_env,
440-
"rustc ",
441-
) {
442-
Ok(it) => it,
443-
Err(e) => {
444-
tracing::error!("{e}");
445-
None
446-
}
447-
};
436+
let toolchain =
437+
match get_toolchain_version(dir, sysroot_ref, Tool::Rustc, &config.extra_env, "rustc ")
438+
{
439+
Ok(it) => it,
440+
Err(e) => {
441+
tracing::error!("{e}");
442+
None
443+
}
444+
};
448445

449446
let rustc_cfg = rustc_cfg::get(None, &config.extra_env, RustcCfgConfig::Rustc(sysroot_ref));
450447
let data_layout = target_data_layout::get(
@@ -1573,8 +1570,7 @@ fn cargo_config_env(
15731570
extra_env: &FxHashMap<String, String>,
15741571
sysroot: Option<&Sysroot>,
15751572
) -> FxHashMap<String, String> {
1576-
let mut cargo_config = Command::new(Tool::Cargo.path());
1577-
Sysroot::set_rustup_toolchain_env(&mut cargo_config, sysroot);
1573+
let mut cargo_config = Sysroot::tool(sysroot, Tool::Cargo);
15781574
cargo_config.envs(extra_env);
15791575
cargo_config
15801576
.current_dir(cargo_toml.parent())

crates/rust-analyzer/src/handlers/request.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,7 @@ fn run_rustfmt(
20022002
let mut command = match snap.config.rustfmt() {
20032003
RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
20042004
// FIXME: Set RUSTUP_TOOLCHAIN
2005-
let mut cmd = process::Command::new(toolchain::rustfmt());
2005+
let mut cmd = process::Command::new(toolchain::Tool::Rustfmt.path());
20062006
cmd.envs(snap.config.extra_env());
20072007
cmd.args(extra_args);
20082008

0 commit comments

Comments
 (0)