mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-11-03 05:13:35 +00:00
fix: Set RUSTUP_TOOLCHAIN and invoke the proxies instead of directly invoking sysroot binaries
This commit is contained in:
parent
000ce5d29c
commit
465ddef7cc
9 changed files with 82 additions and 79 deletions
|
|
@ -71,10 +71,8 @@ impl WorkspaceBuildScripts {
|
|||
cmd
|
||||
}
|
||||
_ => {
|
||||
let mut cmd = Command::new(
|
||||
Sysroot::discover_tool(sysroot, Tool::Cargo)
|
||||
.map_err(|e| io::Error::new(io::ErrorKind::NotFound, e))?,
|
||||
);
|
||||
let mut cmd = Command::new(Tool::Cargo.path());
|
||||
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
|
||||
|
||||
cmd.args(["check", "--quiet", "--workspace", "--message-format=json"]);
|
||||
cmd.args(&config.extra_args);
|
||||
|
|
@ -431,7 +429,8 @@ impl WorkspaceBuildScripts {
|
|||
}
|
||||
let res = (|| {
|
||||
let target_libdir = (|| {
|
||||
let mut cargo_config = Command::new(Sysroot::discover_tool(sysroot, Tool::Cargo)?);
|
||||
let mut cargo_config = Command::new(Tool::Cargo.path());
|
||||
Sysroot::set_rustup_toolchain_env(&mut cargo_config, sysroot);
|
||||
cargo_config.envs(extra_env);
|
||||
cargo_config
|
||||
.current_dir(current_dir)
|
||||
|
|
@ -440,7 +439,8 @@ impl WorkspaceBuildScripts {
|
|||
if let Ok(it) = utf8_stdout(cargo_config) {
|
||||
return Ok(it);
|
||||
}
|
||||
let mut cmd = Command::new(Sysroot::discover_tool(sysroot, Tool::Rustc)?);
|
||||
let mut cmd = Command::new(Tool::Rustc.path());
|
||||
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
|
||||
cmd.envs(extra_env);
|
||||
cmd.args(["--print", "target-libdir"]);
|
||||
utf8_stdout(cmd)
|
||||
|
|
|
|||
|
|
@ -243,7 +243,7 @@ impl CargoWorkspace {
|
|||
let targets = find_list_of_build_targets(config, cargo_toml, sysroot);
|
||||
|
||||
let mut meta = MetadataCommand::new();
|
||||
meta.cargo_path(Sysroot::discover_tool(sysroot, Tool::Cargo)?);
|
||||
meta.cargo_path(Tool::Cargo.path());
|
||||
meta.manifest_path(cargo_toml.to_path_buf());
|
||||
match &config.features {
|
||||
CargoFeatures::All => {
|
||||
|
|
@ -291,6 +291,7 @@ impl CargoWorkspace {
|
|||
|
||||
(|| -> Result<cargo_metadata::Metadata, cargo_metadata::Error> {
|
||||
let mut command = meta.cargo_command();
|
||||
Sysroot::set_rustup_toolchain_env(&mut command, sysroot);
|
||||
command.envs(&config.extra_env);
|
||||
let output = command.output()?;
|
||||
if !output.status.success() {
|
||||
|
|
@ -500,7 +501,8 @@ fn rustc_discover_host_triple(
|
|||
extra_env: &FxHashMap<String, String>,
|
||||
sysroot: Option<&Sysroot>,
|
||||
) -> Option<String> {
|
||||
let mut rustc = Command::new(Sysroot::discover_tool(sysroot, Tool::Rustc).ok()?);
|
||||
let mut rustc = Command::new(Tool::Rustc.path());
|
||||
Sysroot::set_rustup_toolchain_env(&mut rustc, sysroot);
|
||||
rustc.envs(extra_env);
|
||||
rustc.current_dir(cargo_toml.parent()).arg("-vV");
|
||||
tracing::debug!("Discovering host platform by {:?}", rustc);
|
||||
|
|
@ -528,8 +530,8 @@ fn cargo_config_build_target(
|
|||
extra_env: &FxHashMap<String, String>,
|
||||
sysroot: Option<&Sysroot>,
|
||||
) -> Vec<String> {
|
||||
let Ok(program) = Sysroot::discover_tool(sysroot, Tool::Cargo) else { return vec![] };
|
||||
let mut cargo_config = Command::new(program);
|
||||
let mut cargo_config = Command::new(Tool::Cargo.path());
|
||||
Sysroot::set_rustup_toolchain_env(&mut cargo_config, sysroot);
|
||||
cargo_config.envs(extra_env);
|
||||
cargo_config
|
||||
.current_dir(cargo_toml.parent())
|
||||
|
|
|
|||
|
|
@ -68,11 +68,11 @@ fn get_rust_cfgs(
|
|||
config: RustcCfgConfig<'_>,
|
||||
) -> anyhow::Result<String> {
|
||||
let sysroot = match config {
|
||||
RustcCfgConfig::Cargo(sysroot, cargo_oml) => {
|
||||
let cargo = Sysroot::discover_tool(sysroot, toolchain::Tool::Cargo)?;
|
||||
let mut cmd = Command::new(cargo);
|
||||
RustcCfgConfig::Cargo(sysroot, cargo_toml) => {
|
||||
let mut cmd = Command::new(toolchain::Tool::Cargo.path());
|
||||
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
|
||||
cmd.envs(extra_env);
|
||||
cmd.current_dir(cargo_oml.parent())
|
||||
cmd.current_dir(cargo_toml.parent())
|
||||
.args(["rustc", "-Z", "unstable-options", "--print", "cfg"])
|
||||
.env("RUSTC_BOOTSTRAP", "1");
|
||||
if let Some(target) = target {
|
||||
|
|
@ -90,9 +90,8 @@ fn get_rust_cfgs(
|
|||
RustcCfgConfig::Rustc(sysroot) => sysroot,
|
||||
};
|
||||
|
||||
let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
|
||||
tracing::debug!(?rustc, "using explicit rustc from sysroot");
|
||||
let mut cmd = Command::new(rustc);
|
||||
let mut cmd = Command::new(toolchain::Tool::Rustc.path());
|
||||
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
|
||||
cmd.envs(extra_env);
|
||||
cmd.args(["--print", "cfg", "-O"]);
|
||||
if let Some(target) = target {
|
||||
|
|
|
|||
|
|
@ -6,13 +6,13 @@
|
|||
|
||||
use std::{env, fs, iter, ops, path::PathBuf, process::Command, sync::Arc};
|
||||
|
||||
use anyhow::{format_err, Context, Result};
|
||||
use anyhow::{format_err, Result};
|
||||
use base_db::CrateName;
|
||||
use itertools::Itertools;
|
||||
use la_arena::{Arena, Idx};
|
||||
use paths::{AbsPath, AbsPathBuf};
|
||||
use rustc_hash::FxHashMap;
|
||||
use toolchain::{probe_for_binary, Tool};
|
||||
use toolchain::probe_for_binary;
|
||||
|
||||
use crate::{utf8_stdout, CargoConfig, CargoWorkspace, ManifestPath};
|
||||
|
||||
|
|
@ -193,23 +193,9 @@ impl Sysroot {
|
|||
Ok(Sysroot::load(sysroot_dir, Some(sysroot_src_dir), metadata))
|
||||
}
|
||||
|
||||
pub fn discover_binary(&self, binary: &str) -> anyhow::Result<AbsPathBuf> {
|
||||
toolchain::probe_for_binary(self.root.join("bin").join(binary).into())
|
||||
.ok_or_else(|| anyhow::anyhow!("no rustc binary found in {}", self.root.join("bin")))
|
||||
.and_then(|rustc| {
|
||||
fs::metadata(&rustc).map(|_| AbsPathBuf::assert(rustc)).with_context(|| {
|
||||
format!(
|
||||
"failed to discover rustc in sysroot: {:?}",
|
||||
AsRef::<std::path::Path>::as_ref(&self.root)
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub fn discover_tool(sysroot: Option<&Self>, tool: Tool) -> anyhow::Result<PathBuf> {
|
||||
match sysroot {
|
||||
Some(sysroot) => sysroot.discover_binary(tool.name()).map(Into::into),
|
||||
None => Ok(tool.path()),
|
||||
pub fn set_rustup_toolchain_env(cmd: &mut Command, sysroot: Option<&Self>) {
|
||||
if let Some(sysroot) = sysroot {
|
||||
cmd.env("RUSTUP_TOOLCHAIN", AsRef::<std::path::Path>::as_ref(&sysroot.root));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,8 +28,8 @@ pub fn get(
|
|||
};
|
||||
let sysroot = match config {
|
||||
RustcDataLayoutConfig::Cargo(sysroot, cargo_toml) => {
|
||||
let cargo = Sysroot::discover_tool(sysroot, toolchain::Tool::Cargo)?;
|
||||
let mut cmd = Command::new(cargo);
|
||||
let mut cmd = Command::new(toolchain::Tool::Cargo.path());
|
||||
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
|
||||
cmd.envs(extra_env);
|
||||
cmd.current_dir(cargo_toml.parent())
|
||||
.args(["rustc", "--", "-Z", "unstable-options", "--print", "target-spec-json"])
|
||||
|
|
@ -48,8 +48,8 @@ pub fn get(
|
|||
RustcDataLayoutConfig::Rustc(sysroot) => sysroot,
|
||||
};
|
||||
|
||||
let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
|
||||
let mut cmd = Command::new(rustc);
|
||||
let mut cmd = Command::new(toolchain::Tool::Rustc.path());
|
||||
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
|
||||
cmd.envs(extra_env)
|
||||
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
|
||||
.env("RUSTC_BOOTSTRAP", "1");
|
||||
|
|
|
|||
|
|
@ -2,9 +2,7 @@
|
|||
//! metadata` or `rust-project.json`) into representation stored in the salsa
|
||||
//! database -- `CrateGraph`.
|
||||
|
||||
use std::{
|
||||
collections::VecDeque, fmt, fs, iter, path::PathBuf, process::Command, str::FromStr, sync,
|
||||
};
|
||||
use std::{collections::VecDeque, fmt, fs, iter, process::Command, str::FromStr, sync};
|
||||
|
||||
use anyhow::{format_err, Context};
|
||||
use base_db::{
|
||||
|
|
@ -16,6 +14,7 @@ use paths::{AbsPath, AbsPathBuf};
|
|||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use semver::Version;
|
||||
use stdx::always;
|
||||
use toolchain::Tool;
|
||||
use triomphe::Arc;
|
||||
|
||||
use crate::{
|
||||
|
|
@ -163,12 +162,14 @@ impl fmt::Debug for ProjectWorkspace {
|
|||
|
||||
fn get_toolchain_version(
|
||||
current_dir: &AbsPath,
|
||||
cmd_path: Result<PathBuf, anyhow::Error>,
|
||||
sysroot: Option<&Sysroot>,
|
||||
tool: Tool,
|
||||
extra_env: &FxHashMap<String, String>,
|
||||
prefix: &str,
|
||||
) -> Result<Option<Version>, anyhow::Error> {
|
||||
let cargo_version = utf8_stdout({
|
||||
let mut cmd = Command::new(cmd_path?);
|
||||
let mut cmd = Command::new(tool.path());
|
||||
Sysroot::set_rustup_toolchain_env(&mut cmd, sysroot);
|
||||
cmd.envs(extra_env);
|
||||
cmd.arg("--version").current_dir(current_dir);
|
||||
cmd
|
||||
|
|
@ -289,7 +290,8 @@ impl ProjectWorkspace {
|
|||
|
||||
let toolchain = get_toolchain_version(
|
||||
cargo_toml.parent(),
|
||||
Sysroot::discover_tool(sysroot_ref, toolchain::Tool::Cargo),
|
||||
sysroot_ref,
|
||||
toolchain::Tool::Cargo,
|
||||
&config.extra_env,
|
||||
"cargo ",
|
||||
)?;
|
||||
|
|
@ -370,9 +372,13 @@ impl ProjectWorkspace {
|
|||
let sysroot_ref = sysroot.as_ref().ok();
|
||||
let cfg_config = RustcCfgConfig::Rustc(sysroot_ref);
|
||||
let data_layout_config = RustcDataLayoutConfig::Rustc(sysroot_ref);
|
||||
let rustc = Sysroot::discover_tool(sysroot_ref, toolchain::Tool::Rustc).map(Into::into);
|
||||
let toolchain = match get_toolchain_version(project_json.path(), rustc, extra_env, "rustc ")
|
||||
{
|
||||
let toolchain = match get_toolchain_version(
|
||||
project_json.path(),
|
||||
sysroot_ref,
|
||||
toolchain::Tool::Rustc,
|
||||
extra_env,
|
||||
"rustc ",
|
||||
) {
|
||||
Ok(it) => it,
|
||||
Err(e) => {
|
||||
tracing::error!("{e}");
|
||||
|
|
@ -1615,10 +1621,8 @@ fn cargo_config_env(
|
|||
extra_env: &FxHashMap<String, String>,
|
||||
sysroot: Option<&Sysroot>,
|
||||
) -> FxHashMap<String, String> {
|
||||
let Ok(program) = Sysroot::discover_tool(sysroot, toolchain::Tool::Cargo) else {
|
||||
return Default::default();
|
||||
};
|
||||
let mut cargo_config = Command::new(program);
|
||||
let mut cargo_config = Command::new(Tool::Cargo.path());
|
||||
Sysroot::set_rustup_toolchain_env(&mut cargo_config, sysroot);
|
||||
cargo_config.envs(extra_env);
|
||||
cargo_config
|
||||
.current_dir(cargo_toml.parent())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue