Skip to content

Commit 92ef331

Browse files
committed
Align toolchain version fetching with other toolchain info querying
Fix --target flag argument order in rustc_cfg fetching
1 parent cfdcd20 commit 92ef331

File tree

4 files changed

+58
-76
lines changed

4 files changed

+58
-76
lines changed

crates/project-model/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod toolchain_info {
2020
pub mod rustc_cfg;
2121
pub mod target_data_layout;
2222
pub mod target_tuple;
23+
pub mod version;
2324

2425
use std::path::Path;
2526

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,11 @@ fn rustc_print_cfg(
6666
QueryConfig::Cargo(sysroot, cargo_toml) => {
6767
let mut cmd = sysroot.tool(Tool::Cargo, cargo_toml.parent());
6868
cmd.envs(extra_env);
69-
cmd.env("RUSTC_BOOTSTRAP", "1");
70-
cmd.args(["rustc", "-Z", "unstable-options"]).args(RUSTC_ARGS);
71-
cmd.args(["--", "-O"]);
69+
cmd.args(["rustc"]).args(RUSTC_ARGS);
7270
if let Some(target) = target {
7371
cmd.args(["--target", target]);
7472
}
73+
cmd.args(["--", "-O"]);
7574

7675
match utf8_stdout(&mut cmd) {
7776
Ok(it) => return Ok(it),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//! Get the version string of the toolchain.
2+
3+
use anyhow::Context;
4+
use rustc_hash::FxHashMap;
5+
use semver::Version;
6+
use toolchain::Tool;
7+
8+
use crate::{toolchain_info::QueryConfig, utf8_stdout};
9+
10+
pub(crate) fn get(
11+
config: QueryConfig<'_>,
12+
extra_env: &FxHashMap<String, String>,
13+
) -> Result<Option<Version>, anyhow::Error> {
14+
let (mut cmd, prefix) = match config {
15+
QueryConfig::Cargo(sysroot, cargo_toml) => {
16+
(sysroot.tool(Tool::Cargo, cargo_toml.parent()), "cargo ")
17+
}
18+
QueryConfig::Rustc(sysroot, current_dir) => {
19+
(sysroot.tool(Tool::Rustc, current_dir), "rustc ")
20+
}
21+
};
22+
cmd.envs(extra_env);
23+
cmd.arg("--version");
24+
let out = utf8_stdout(&mut cmd).with_context(|| format!("Failed to query rust toolchain version via `{cmd:?}`, is your toolchain setup correctly?"))?;
25+
26+
let version =
27+
out.strip_prefix(prefix).and_then(|it| Version::parse(it.split_whitespace().next()?).ok());
28+
if version.is_none() {
29+
tracing::warn!("Failed to parse `{cmd:?}` output `{out}` as a semver version");
30+
}
31+
anyhow::Ok(version)
32+
}

crates/project-model/src/workspace.rs

+23-73
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use paths::{AbsPath, AbsPathBuf};
1616
use rustc_hash::FxHashMap;
1717
use semver::Version;
1818
use span::{Edition, FileId};
19-
use toolchain::Tool;
2019
use tracing::instrument;
2120
use triomphe::Arc;
2221

@@ -26,9 +25,9 @@ use crate::{
2625
env::{cargo_config_env, inject_cargo_env, inject_cargo_package_env, inject_rustc_tool_env},
2726
project_json::{Crate, CrateArrayIdx},
2827
sysroot::{SysrootCrate, SysrootMode},
29-
toolchain_info::{rustc_cfg, target_data_layout, target_tuple, QueryConfig},
30-
utf8_stdout, CargoConfig, CargoWorkspace, CfgOverrides, InvocationStrategy, ManifestPath,
31-
Package, ProjectJson, ProjectManifest, Sysroot, TargetData, TargetKind, WorkspaceBuildScripts,
28+
toolchain_info::{rustc_cfg, target_data_layout, target_tuple, version, QueryConfig},
29+
CargoConfig, CargoWorkspace, CfgOverrides, InvocationStrategy, ManifestPath, Package,
30+
ProjectJson, ProjectManifest, Sysroot, TargetData, TargetKind, WorkspaceBuildScripts,
3231
};
3332
use tracing::{debug, error, info};
3433

@@ -150,27 +149,6 @@ impl fmt::Debug for ProjectWorkspace {
150149
}
151150
}
152151

153-
fn get_toolchain_version(
154-
current_dir: &AbsPath,
155-
sysroot: &Sysroot,
156-
tool: Tool,
157-
extra_env: &FxHashMap<String, String>,
158-
prefix: &str,
159-
) -> Result<Option<Version>, anyhow::Error> {
160-
let cargo_version = utf8_stdout(&mut {
161-
let mut cmd = Sysroot::tool(sysroot, tool, current_dir);
162-
cmd.envs(extra_env);
163-
cmd.arg("--version");
164-
cmd
165-
})
166-
.with_context(|| format!("Failed to query rust toolchain version at {current_dir}, is your toolchain setup correctly?"))?;
167-
anyhow::Ok(
168-
cargo_version
169-
.get(prefix.len()..)
170-
.and_then(|it| Version::parse(it.split_whitespace().next()?).ok()),
171-
)
172-
}
173-
174152
impl ProjectWorkspace {
175153
pub fn load(
176154
manifest: ProjectManifest,
@@ -249,12 +227,10 @@ impl ProjectWorkspace {
249227
.ok_or_else(|| Some("Failed to discover rustc source for sysroot.".to_owned())),
250228
None => Err(None),
251229
};
252-
let targets = target_tuple::get(
253-
QueryConfig::Cargo(&sysroot, cargo_toml),
254-
config.target.as_deref(),
255-
&config.extra_env,
256-
)
257-
.unwrap_or_default();
230+
let toolchain_config = QueryConfig::Cargo(&sysroot, cargo_toml);
231+
let targets =
232+
target_tuple::get(toolchain_config, config.target.as_deref(), &config.extra_env)
233+
.unwrap_or_default();
258234
let rustc = rustc_dir.and_then(|rustc_dir| {
259235
info!(workspace = %cargo_toml, rustc_dir = %rustc_dir, "Using rustc source");
260236
match CargoWorkspace::fetch_metadata(
@@ -291,21 +267,19 @@ impl ProjectWorkspace {
291267
}
292268
}
293269
});
294-
let toolchain = get_toolchain_version(
295-
cargo_toml.parent(),
296-
&sysroot,
297-
Tool::Cargo,
298-
&config.extra_env,
299-
"cargo ",
300-
)?;
301-
let rustc_cfg = rustc_cfg::get(
302-
QueryConfig::Cargo(&sysroot, cargo_toml),
303-
targets.first().map(Deref::deref),
304-
&config.extra_env,
305-
);
270+
let toolchain = version::get(toolchain_config, &config.extra_env)
271+
.inspect_err(|e| {
272+
tracing::error!(%e,
273+
"failed fetching toolchain version for {cargo_toml:?} workspace"
274+
)
275+
})
276+
.ok()
277+
.flatten();
278+
let rustc_cfg =
279+
rustc_cfg::get(toolchain_config, targets.first().map(Deref::deref), &config.extra_env);
306280
let cfg_overrides = config.cfg_overrides.clone();
307281
let data_layout = target_data_layout::get(
308-
QueryConfig::Cargo(&sysroot, cargo_toml),
282+
toolchain_config,
309283
targets.first().map(Deref::deref),
310284
&config.extra_env,
311285
);
@@ -355,19 +329,7 @@ impl ProjectWorkspace {
355329
&config.sysroot_query_metadata,
356330
);
357331
let query_config = QueryConfig::Rustc(&sysroot, project_json.path().as_ref());
358-
let toolchain = match get_toolchain_version(
359-
project_json.path(),
360-
&sysroot,
361-
Tool::Rustc,
362-
&config.extra_env,
363-
"rustc ",
364-
) {
365-
Ok(it) => it,
366-
Err(e) => {
367-
tracing::error!("{e}");
368-
None
369-
}
370-
};
332+
let toolchain = version::get(query_config, &config.extra_env).ok().flatten();
371333

372334
let target = config.target.as_deref();
373335
let rustc_cfg = rustc_cfg::get(query_config, target, &config.extra_env);
@@ -397,22 +359,10 @@ impl ProjectWorkspace {
397359
None => Sysroot::empty(),
398360
};
399361

400-
let toolchain =
401-
match get_toolchain_version(dir, &sysroot, Tool::Rustc, &config.extra_env, "rustc ") {
402-
Ok(it) => it,
403-
Err(e) => {
404-
tracing::error!("{e}");
405-
None
406-
}
407-
};
408-
409-
let targets = target_tuple::get(
410-
QueryConfig::Cargo(&sysroot, detached_file),
411-
config.target.as_deref(),
412-
&config.extra_env,
413-
)
414-
.unwrap_or_default();
415-
let query_config = QueryConfig::Rustc(&sysroot, dir.as_ref());
362+
let query_config = QueryConfig::Cargo(&sysroot, detached_file);
363+
let toolchain = version::get(query_config, &config.extra_env).ok().flatten();
364+
let targets = target_tuple::get(query_config, config.target.as_deref(), &config.extra_env)
365+
.unwrap_or_default();
416366
let rustc_cfg = rustc_cfg::get(query_config, None, &config.extra_env);
417367
let data_layout = target_data_layout::get(query_config, None, &config.extra_env);
418368

0 commit comments

Comments
 (0)