Skip to content

Commit 5ce14b0

Browse files
committed
Enforce a current directory being set for spawned commands
1 parent 0f95e60 commit 5ce14b0

File tree

22 files changed

+99
-75
lines changed

22 files changed

+99
-75
lines changed

clippy.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ disallowed-types = [
33
{ path = "std::collections::HashSet", reason = "use FxHashSet" },
44
{ path = "std::collections::hash_map::RandomState", reason = "use BuildHasherDefault<FxHasher>"}
55
]
6+
7+
disallowed-methods = [
8+
{ path = "std::process::Command::new", reason = "use `toolchain::command` instead as it forces the choice of a working directory" },
9+
]

crates/ide/src/expand_macro.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ fn _format(
253253
let &crate_id = db.relevant_crates(file_id).iter().next()?;
254254
let edition = db.crate_graph()[crate_id].edition;
255255

256+
#[allow(clippy::disallowed_methods)]
256257
let mut cmd = std::process::Command::new(toolchain::Tool::Rustfmt.path());
257258
cmd.arg("--edition");
258259
cmd.arg(edition.to_string());

crates/proc-macro-api/src/process.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ fn mk_child(
202202
env: impl IntoIterator<Item = (impl AsRef<std::ffi::OsStr>, impl AsRef<std::ffi::OsStr>)>,
203203
null_stderr: bool,
204204
) -> io::Result<Child> {
205+
#[allow(clippy::disallowed_methods)]
205206
let mut cmd = Command::new(path);
206207
cmd.envs(env)
207208
.env("RUST_ANALYZER_INTERNALS_DO_NOT_USE", "this is unstable")

crates/proc-macro-srv/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ fn main() {
77
println!("cargo::rustc-check-cfg=cfg(rust_analyzer)");
88

99
let rustc = env::var("RUSTC").expect("proc-macro-srv's build script expects RUSTC to be set");
10+
#[allow(clippy::disallowed_methods)]
1011
let output = Command::new(rustc).arg("--version").output().expect("rustc --version must run");
1112
let version_string = std::str::from_utf8(&output.stdout[..])
1213
.expect("rustc --version output must be UTF-8")

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//! a specific rustup toolchain: this allows testing against older ABIs (e.g.
88
//! 1.58) and future ABIs (stage1, nightly)
99
10+
#![allow(clippy::disallowed_methods)]
11+
1012
use std::{
1113
env,
1214
path::{Path, PathBuf},

crates/project-model/src/build_dependencies.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,15 @@ impl WorkspaceBuildScripts {
172172
}
173173
let res = (|| {
174174
let target_libdir = (|| {
175-
let mut cargo_config = sysroot.tool(Tool::Cargo);
175+
let mut cargo_config = sysroot.tool(Tool::Cargo, current_dir);
176176
cargo_config.envs(extra_env);
177177
cargo_config
178-
.current_dir(current_dir)
179178
.args(["rustc", "-Z", "unstable-options", "--print", "target-libdir"])
180179
.env("RUSTC_BOOTSTRAP", "1");
181180
if let Ok(it) = utf8_stdout(&mut cargo_config) {
182181
return Ok(it);
183182
}
184-
let mut cmd = sysroot.tool(Tool::Rustc);
183+
let mut cmd = sysroot.tool(Tool::Rustc, current_dir);
185184
cmd.envs(extra_env);
186185
cmd.args(["--print", "target-libdir"]);
187186
utf8_stdout(&mut cmd)
@@ -390,12 +389,12 @@ impl WorkspaceBuildScripts {
390389
) -> io::Result<Command> {
391390
let mut cmd = match config.run_build_script_command.as_deref() {
392391
Some([program, args @ ..]) => {
393-
let mut cmd = Command::new(program);
392+
let mut cmd = toolchain::command(program, current_dir);
394393
cmd.args(args);
395394
cmd
396395
}
397396
_ => {
398-
let mut cmd = sysroot.tool(Tool::Cargo);
397+
let mut cmd = sysroot.tool(Tool::Cargo, current_dir);
399398

400399
cmd.args(["check", "--quiet", "--workspace", "--message-format=json"]);
401400
cmd.args(&config.extra_args);
@@ -448,7 +447,6 @@ impl WorkspaceBuildScripts {
448447
}
449448
};
450449

451-
cmd.current_dir(current_dir);
452450
cmd.envs(&config.extra_env);
453451
if config.wrap_rustc_in_build_scripts {
454452
// Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use

crates/project-model/src/cargo_workspace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ impl CargoWorkspace {
294294
no_deps: bool,
295295
progress: &dyn Fn(String),
296296
) -> anyhow::Result<(cargo_metadata::Metadata, Option<anyhow::Error>)> {
297-
let cargo = sysroot.tool(Tool::Cargo);
297+
let cargo = sysroot.tool(Tool::Cargo, current_dir);
298298
let mut meta = MetadataCommand::new();
299299
meta.cargo_path(cargo.get_program());
300300
cargo.get_envs().for_each(|(var, val)| _ = meta.env(var, val.unwrap_or_default()));

crates/project-model/src/env.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ pub(crate) fn cargo_config_env(
7474
extra_env: &FxHashMap<String, String>,
7575
sysroot: &Sysroot,
7676
) -> FxHashMap<String, String> {
77-
let mut cargo_config = sysroot.tool(Tool::Cargo);
77+
let mut cargo_config = sysroot.tool(Tool::Cargo, manifest.parent());
7878
cargo_config.envs(extra_env);
7979
cargo_config
80-
.current_dir(manifest.parent())
8180
.args(["-Z", "unstable-options", "config", "get", "env"])
8281
.env("RUSTC_BOOTSTRAP", "1");
8382
if manifest.is_rust_manifest() {

crates/project-model/src/sysroot.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! but we can't process `.rlib` and need source code instead. The source code
55
//! is typically installed with `rustup component add rust-src` command.
66
7-
use std::{env, fs, ops, process::Command};
7+
use std::{env, fs, ops, path::Path, process::Command};
88

99
use anyhow::{format_err, Result};
1010
use base_db::CrateName;
@@ -170,7 +170,7 @@ impl Sysroot {
170170
}
171171

172172
/// Returns a command to run a tool preferring the cargo proxies if the sysroot exists.
173-
pub fn tool(&self, tool: Tool) -> Command {
173+
pub fn tool(&self, tool: Tool, current_dir: impl AsRef<Path>) -> Command {
174174
match self.root() {
175175
Some(root) => {
176176
// special case rustc, we can look that up directly in the sysroot's bin folder
@@ -179,15 +179,15 @@ impl Sysroot {
179179
if let Some(path) =
180180
probe_for_binary(root.join("bin").join(Tool::Rustc.name()).into())
181181
{
182-
return Command::new(path);
182+
return toolchain::command(path, current_dir);
183183
}
184184
}
185185

186-
let mut cmd = Command::new(tool.prefer_proxy());
186+
let mut cmd = toolchain::command(tool.prefer_proxy(), current_dir);
187187
cmd.env("RUSTUP_TOOLCHAIN", AsRef::<std::path::Path>::as_ref(root));
188188
cmd
189189
}
190-
_ => Command::new(tool.path()),
190+
_ => toolchain::command(tool.path(), current_dir),
191191
}
192192
}
193193

@@ -436,7 +436,7 @@ fn discover_sysroot_dir(
436436
current_dir: &AbsPath,
437437
extra_env: &FxHashMap<String, String>,
438438
) -> Result<AbsPathBuf> {
439-
let mut rustc = Command::new(Tool::Rustc.path());
439+
let mut rustc = toolchain::command(Tool::Rustc.path(), current_dir);
440440
rustc.envs(extra_env);
441441
rustc.current_dir(current_dir).args(["--print", "sysroot"]);
442442
tracing::debug!("Discovering sysroot by {:?}", rustc);
@@ -468,9 +468,9 @@ fn discover_sysroot_src_dir_or_add_component(
468468
) -> Result<AbsPathBuf> {
469469
discover_sysroot_src_dir(sysroot_path)
470470
.or_else(|| {
471-
let mut rustup = Command::new(Tool::Rustup.prefer_proxy());
471+
let mut rustup = toolchain::command(Tool::Rustup.prefer_proxy(), current_dir);
472472
rustup.envs(extra_env);
473-
rustup.current_dir(current_dir).args(["component", "add", "rust-src"]);
473+
rustup.args(["component", "add", "rust-src"]);
474474
tracing::info!("adding rust-src component by {:?}", rustup);
475475
utf8_stdout(&mut rustup).ok()?;
476476
get_rust_src(sysroot_path)

crates/project-model/src/toolchain_info/rustc_cfg.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ fn rustc_print_cfg(
4545
const RUSTC_ARGS: [&str; 3] = ["--print", "cfg", "-O"];
4646
let sysroot = match config {
4747
QueryConfig::Cargo(sysroot, cargo_toml) => {
48-
let mut cmd = sysroot.tool(Tool::Cargo);
48+
let mut cmd = sysroot.tool(Tool::Cargo, cargo_toml.parent());
4949
cmd.envs(extra_env);
50-
cmd.current_dir(cargo_toml.parent()).env("RUSTC_BOOTSTRAP", "1");
50+
cmd.env("RUSTC_BOOTSTRAP", "1");
5151
cmd.args(["rustc", "-Z", "unstable-options"]).args(RUSTC_ARGS);
5252
if let Some(target) = target {
5353
cmd.args(["--target", target]);
@@ -67,7 +67,7 @@ fn rustc_print_cfg(
6767
QueryConfig::Rustc(sysroot) => sysroot,
6868
};
6969

70-
let mut cmd = sysroot.tool(Tool::Rustc);
70+
let mut cmd = sysroot.tool(Tool::Rustc, &std::env::current_dir()?);
7171
cmd.envs(extra_env);
7272
cmd.args(RUSTC_ARGS);
7373
if let Some(target) = target {

0 commit comments

Comments
 (0)