Skip to content

Commit b570206

Browse files
authored
Merge pull request rust-lang#18785 from Veykril/push-uvsqposqyvmo
Cleanup toolchain info fetching
2 parents bde1322 + f36a93e commit b570206

File tree

27 files changed

+438
-436
lines changed

27 files changed

+438
-436
lines changed

src/tools/rust-analyzer/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+
]

src/tools/rust-analyzer/crates/hir-ty/src/layout/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use chalk_ir::{AdtId, TyKind};
22
use either::Either;
33
use hir_def::db::DefDatabase;
4-
use project_model::{target_data_layout::RustcDataLayoutConfig, Sysroot};
4+
use project_model::{toolchain_info::QueryConfig, Sysroot};
55
use rustc_hash::FxHashMap;
66
use syntax::ToSmolStr;
77
use test_fixture::WithFixture;
@@ -17,8 +17,8 @@ use crate::{
1717
mod closure;
1818

1919
fn current_machine_data_layout() -> String {
20-
project_model::target_data_layout::get(
21-
RustcDataLayoutConfig::Rustc(&Sysroot::empty()),
20+
project_model::toolchain_info::target_data_layout::get(
21+
QueryConfig::Rustc(&Sysroot::empty(), &std::env::current_dir().unwrap()),
2222
None,
2323
&FxHashMap::default(),
2424
)

src/tools/rust-analyzer/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());

src/tools/rust-analyzer/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")

src/tools/rust-analyzer/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")

src/tools/rust-analyzer/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},

src/tools/rust-analyzer/crates/project-model/src/build_dependencies.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,18 @@ 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");
181-
if let Ok(it) = utf8_stdout(cargo_config) {
180+
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"]);
187-
utf8_stdout(cmd)
186+
utf8_stdout(&mut cmd)
188187
})()?;
189188

190189
let target_libdir = AbsPathBuf::try_from(Utf8PathBuf::from(target_libdir))
@@ -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

src/tools/rust-analyzer/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()));

src/tools/rust-analyzer/crates/project-model/src/env.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,17 @@ 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() {
8483
cargo_config.arg("-Zscript");
8584
}
8685
// if successful we receive `env.key.value = "value" per entry
8786
tracing::debug!("Discovering cargo config env by {:?}", cargo_config);
88-
utf8_stdout(cargo_config)
87+
utf8_stdout(&mut cargo_config)
8988
.map(parse_output_cargo_config_env)
9089
.inspect(|env| {
9190
tracing::debug!("Discovered cargo config env: {:?}", env);

src/tools/rust-analyzer/crates/project-model/src/lib.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,31 @@
1515
//! procedural macros).
1616
//! * Lowering of concrete model to a [`base_db::CrateGraph`]
1717
18+
pub mod project_json;
19+
pub mod toolchain_info {
20+
pub mod rustc_cfg;
21+
pub mod target_data_layout;
22+
pub mod target_triple;
23+
24+
use std::path::Path;
25+
26+
use crate::{ManifestPath, Sysroot};
27+
28+
#[derive(Copy, Clone)]
29+
pub enum QueryConfig<'a> {
30+
/// Directly invoke `rustc` to query the desired information.
31+
Rustc(&'a Sysroot, &'a Path),
32+
/// Attempt to use cargo to query the desired information, honoring cargo configurations.
33+
/// If this fails, falls back to invoking `rustc` directly.
34+
Cargo(&'a Sysroot, &'a ManifestPath),
35+
}
36+
}
37+
1838
mod build_dependencies;
1939
mod cargo_workspace;
2040
mod env;
2141
mod manifest_path;
22-
pub mod project_json;
23-
mod rustc_cfg;
2442
mod sysroot;
25-
pub mod target_data_layout;
26-
mod target_triple;
2743
mod workspace;
2844

2945
#[cfg(test)]
@@ -182,7 +198,7 @@ impl fmt::Display for ProjectManifest {
182198
}
183199
}
184200

185-
fn utf8_stdout(mut cmd: Command) -> anyhow::Result<String> {
201+
fn utf8_stdout(cmd: &mut Command) -> anyhow::Result<String> {
186202
let output = cmd.output().with_context(|| format!("{cmd:?} failed"))?;
187203
if !output.status.success() {
188204
match String::from_utf8(output.stderr) {

src/tools/rust-analyzer/crates/project-model/src/rustc_cfg.rs

Lines changed: 0 additions & 99 deletions
This file was deleted.

src/tools/rust-analyzer/crates/project-model/src/sysroot.rs

Lines changed: 10 additions & 10 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,11 +436,11 @@ 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);
443-
let stdout = utf8_stdout(rustc)?;
443+
let stdout = utf8_stdout(&mut rustc)?;
444444
Ok(AbsPathBuf::assert(Utf8PathBuf::from(stdout)))
445445
}
446446

@@ -468,11 +468,11 @@ 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);
475-
utf8_stdout(rustup).ok()?;
475+
utf8_stdout(&mut rustup).ok()?;
476476
get_rust_src(sysroot_path)
477477
})
478478
.ok_or_else(|| {

0 commit comments

Comments
 (0)