Enforce a current directory being set for spawned commands

This commit is contained in:
Lukas Wirth 2024-12-29 12:51:13 +01:00
parent 0f95e60da3
commit 5ce14b0439
22 changed files with 99 additions and 75 deletions

View file

@ -172,16 +172,15 @@ impl WorkspaceBuildScripts {
}
let res = (|| {
let target_libdir = (|| {
let mut cargo_config = sysroot.tool(Tool::Cargo);
let mut cargo_config = sysroot.tool(Tool::Cargo, current_dir);
cargo_config.envs(extra_env);
cargo_config
.current_dir(current_dir)
.args(["rustc", "-Z", "unstable-options", "--print", "target-libdir"])
.env("RUSTC_BOOTSTRAP", "1");
if let Ok(it) = utf8_stdout(&mut cargo_config) {
return Ok(it);
}
let mut cmd = sysroot.tool(Tool::Rustc);
let mut cmd = sysroot.tool(Tool::Rustc, current_dir);
cmd.envs(extra_env);
cmd.args(["--print", "target-libdir"]);
utf8_stdout(&mut cmd)
@ -390,12 +389,12 @@ impl WorkspaceBuildScripts {
) -> io::Result<Command> {
let mut cmd = match config.run_build_script_command.as_deref() {
Some([program, args @ ..]) => {
let mut cmd = Command::new(program);
let mut cmd = toolchain::command(program, current_dir);
cmd.args(args);
cmd
}
_ => {
let mut cmd = sysroot.tool(Tool::Cargo);
let mut cmd = sysroot.tool(Tool::Cargo, current_dir);
cmd.args(["check", "--quiet", "--workspace", "--message-format=json"]);
cmd.args(&config.extra_args);
@ -448,7 +447,6 @@ impl WorkspaceBuildScripts {
}
};
cmd.current_dir(current_dir);
cmd.envs(&config.extra_env);
if config.wrap_rustc_in_build_scripts {
// Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use

View file

@ -294,7 +294,7 @@ impl CargoWorkspace {
no_deps: bool,
progress: &dyn Fn(String),
) -> anyhow::Result<(cargo_metadata::Metadata, Option<anyhow::Error>)> {
let cargo = sysroot.tool(Tool::Cargo);
let cargo = sysroot.tool(Tool::Cargo, current_dir);
let mut meta = MetadataCommand::new();
meta.cargo_path(cargo.get_program());
cargo.get_envs().for_each(|(var, val)| _ = meta.env(var, val.unwrap_or_default()));

View file

@ -74,10 +74,9 @@ pub(crate) fn cargo_config_env(
extra_env: &FxHashMap<String, String>,
sysroot: &Sysroot,
) -> FxHashMap<String, String> {
let mut cargo_config = sysroot.tool(Tool::Cargo);
let mut cargo_config = sysroot.tool(Tool::Cargo, manifest.parent());
cargo_config.envs(extra_env);
cargo_config
.current_dir(manifest.parent())
.args(["-Z", "unstable-options", "config", "get", "env"])
.env("RUSTC_BOOTSTRAP", "1");
if manifest.is_rust_manifest() {

View file

@ -4,7 +4,7 @@
//! but we can't process `.rlib` and need source code instead. The source code
//! is typically installed with `rustup component add rust-src` command.
use std::{env, fs, ops, process::Command};
use std::{env, fs, ops, path::Path, process::Command};
use anyhow::{format_err, Result};
use base_db::CrateName;
@ -170,7 +170,7 @@ impl Sysroot {
}
/// Returns a command to run a tool preferring the cargo proxies if the sysroot exists.
pub fn tool(&self, tool: Tool) -> Command {
pub fn tool(&self, tool: Tool, current_dir: impl AsRef<Path>) -> Command {
match self.root() {
Some(root) => {
// special case rustc, we can look that up directly in the sysroot's bin folder
@ -179,15 +179,15 @@ impl Sysroot {
if let Some(path) =
probe_for_binary(root.join("bin").join(Tool::Rustc.name()).into())
{
return Command::new(path);
return toolchain::command(path, current_dir);
}
}
let mut cmd = Command::new(tool.prefer_proxy());
let mut cmd = toolchain::command(tool.prefer_proxy(), current_dir);
cmd.env("RUSTUP_TOOLCHAIN", AsRef::<std::path::Path>::as_ref(root));
cmd
}
_ => Command::new(tool.path()),
_ => toolchain::command(tool.path(), current_dir),
}
}
@ -436,7 +436,7 @@ fn discover_sysroot_dir(
current_dir: &AbsPath,
extra_env: &FxHashMap<String, String>,
) -> Result<AbsPathBuf> {
let mut rustc = Command::new(Tool::Rustc.path());
let mut rustc = toolchain::command(Tool::Rustc.path(), current_dir);
rustc.envs(extra_env);
rustc.current_dir(current_dir).args(["--print", "sysroot"]);
tracing::debug!("Discovering sysroot by {:?}", rustc);
@ -468,9 +468,9 @@ fn discover_sysroot_src_dir_or_add_component(
) -> Result<AbsPathBuf> {
discover_sysroot_src_dir(sysroot_path)
.or_else(|| {
let mut rustup = Command::new(Tool::Rustup.prefer_proxy());
let mut rustup = toolchain::command(Tool::Rustup.prefer_proxy(), current_dir);
rustup.envs(extra_env);
rustup.current_dir(current_dir).args(["component", "add", "rust-src"]);
rustup.args(["component", "add", "rust-src"]);
tracing::info!("adding rust-src component by {:?}", rustup);
utf8_stdout(&mut rustup).ok()?;
get_rust_src(sysroot_path)

View file

@ -45,9 +45,9 @@ fn rustc_print_cfg(
const RUSTC_ARGS: [&str; 3] = ["--print", "cfg", "-O"];
let sysroot = match config {
QueryConfig::Cargo(sysroot, cargo_toml) => {
let mut cmd = sysroot.tool(Tool::Cargo);
let mut cmd = sysroot.tool(Tool::Cargo, cargo_toml.parent());
cmd.envs(extra_env);
cmd.current_dir(cargo_toml.parent()).env("RUSTC_BOOTSTRAP", "1");
cmd.env("RUSTC_BOOTSTRAP", "1");
cmd.args(["rustc", "-Z", "unstable-options"]).args(RUSTC_ARGS);
if let Some(target) = target {
cmd.args(["--target", target]);
@ -67,7 +67,7 @@ fn rustc_print_cfg(
QueryConfig::Rustc(sysroot) => sysroot,
};
let mut cmd = sysroot.tool(Tool::Rustc);
let mut cmd = sysroot.tool(Tool::Rustc, &std::env::current_dir()?);
cmd.envs(extra_env);
cmd.args(RUSTC_ARGS);
if let Some(target) = target {

View file

@ -21,9 +21,9 @@ pub fn get(
};
let sysroot = match config {
QueryConfig::Cargo(sysroot, cargo_toml) => {
let mut cmd = sysroot.tool(Tool::Cargo);
let mut cmd = sysroot.tool(Tool::Cargo, cargo_toml.parent());
cmd.envs(extra_env);
cmd.current_dir(cargo_toml.parent()).env("RUSTC_BOOTSTRAP", "1");
cmd.env("RUSTC_BOOTSTRAP", "1");
cmd.args(["rustc", "-Z", "unstable-options"]).args(RUSTC_ARGS).args([
"--",
"-Z",
@ -43,7 +43,7 @@ pub fn get(
QueryConfig::Rustc(sysroot) => sysroot,
};
let mut cmd = Sysroot::tool(sysroot, Tool::Rustc);
let mut cmd = Sysroot::tool(sysroot, Tool::Rustc, &std::env::current_dir()?);
cmd.envs(extra_env)
.env("RUSTC_BOOTSTRAP", "1")
.args(["-Z", "unstable-options"])

View file

@ -32,7 +32,7 @@ fn rustc_discover_host_triple(
extra_env: &FxHashMap<String, String>,
sysroot: &Sysroot,
) -> anyhow::Result<String> {
let mut cmd = sysroot.tool(Tool::Rustc);
let mut cmd = sysroot.tool(Tool::Rustc, &std::env::current_dir()?);
cmd.envs(extra_env);
cmd.arg("-vV");
let stdout = utf8_stdout(&mut cmd)
@ -52,7 +52,7 @@ fn cargo_config_build_target(
extra_env: &FxHashMap<String, String>,
sysroot: &Sysroot,
) -> Option<Vec<String>> {
let mut cmd = sysroot.tool(Tool::Cargo);
let mut cmd = sysroot.tool(Tool::Cargo, cargo_toml.parent());
cmd.envs(extra_env);
cmd.current_dir(cargo_toml.parent()).env("RUSTC_BOOTSTRAP", "1");
cmd.args(["-Z", "unstable-options", "config", "get", "build.target"]);

View file

@ -176,9 +176,9 @@ fn get_toolchain_version(
prefix: &str,
) -> Result<Option<Version>, anyhow::Error> {
let cargo_version = utf8_stdout(&mut {
let mut cmd = Sysroot::tool(sysroot, tool);
let mut cmd = Sysroot::tool(sysroot, tool, current_dir);
cmd.envs(extra_env);
cmd.arg("--version").current_dir(current_dir);
cmd.arg("--version");
cmd
})
.with_context(|| format!("Failed to query rust toolchain version at {current_dir}, is your toolchain setup correctly?"))?;