Skip to content

Commit 28b9c69

Browse files
committed
Align toolchain version fetching with other toolchain info querying
1 parent cfdcd20 commit 28b9c69

File tree

3 files changed

+54
-70
lines changed

3 files changed

+54
-70
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

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

+21-70
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
env::{cargo_config_env, inject_cargo_env, inject_cargo_package_env, inject_rustc_tool_env},
2727
project_json::{Crate, CrateArrayIdx},
2828
sysroot::{SysrootCrate, SysrootMode},
29-
toolchain_info::{rustc_cfg, target_data_layout, target_tuple, QueryConfig},
29+
toolchain_info::{rustc_cfg, target_data_layout, target_tuple, version, QueryConfig},
3030
utf8_stdout, CargoConfig, CargoWorkspace, CfgOverrides, InvocationStrategy, ManifestPath,
3131
Package, ProjectJson, ProjectManifest, Sysroot, TargetData, TargetKind, WorkspaceBuildScripts,
3232
};
@@ -150,27 +150,6 @@ impl fmt::Debug for ProjectWorkspace {
150150
}
151151
}
152152

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-
174153
impl ProjectWorkspace {
175154
pub fn load(
176155
manifest: ProjectManifest,
@@ -249,12 +228,10 @@ impl ProjectWorkspace {
249228
.ok_or_else(|| Some("Failed to discover rustc source for sysroot.".to_owned())),
250229
None => Err(None),
251230
};
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();
231+
let toolchain_config = QueryConfig::Cargo(&sysroot, cargo_toml);
232+
let targets =
233+
target_tuple::get(toolchain_config, config.target.as_deref(), &config.extra_env)
234+
.unwrap_or_default();
258235
let rustc = rustc_dir.and_then(|rustc_dir| {
259236
info!(workspace = %cargo_toml, rustc_dir = %rustc_dir, "Using rustc source");
260237
match CargoWorkspace::fetch_metadata(
@@ -291,21 +268,19 @@ impl ProjectWorkspace {
291268
}
292269
}
293270
});
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-
);
271+
let toolchain = version::get(toolchain_config, &config.extra_env)
272+
.inspect_err(|e| {
273+
tracing::error!(%e,
274+
"failed fetching toolchain version for {cargo_toml:?} workspace"
275+
)
276+
})
277+
.ok()
278+
.flatten();
279+
let rustc_cfg =
280+
rustc_cfg::get(toolchain_config, targets.first().map(Deref::deref), &config.extra_env);
306281
let cfg_overrides = config.cfg_overrides.clone();
307282
let data_layout = target_data_layout::get(
308-
QueryConfig::Cargo(&sysroot, cargo_toml),
283+
toolchain_config,
309284
targets.first().map(Deref::deref),
310285
&config.extra_env,
311286
);
@@ -355,19 +330,7 @@ impl ProjectWorkspace {
355330
&config.sysroot_query_metadata,
356331
);
357332
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-
};
333+
let toolchain = version::get(query_config, &config.extra_env).ok().flatten();
371334

372335
let target = config.target.as_deref();
373336
let rustc_cfg = rustc_cfg::get(query_config, target, &config.extra_env);
@@ -397,22 +360,10 @@ impl ProjectWorkspace {
397360
None => Sysroot::empty(),
398361
};
399362

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());
363+
let query_config = QueryConfig::Cargo(&sysroot, detached_file);
364+
let toolchain = version::get(query_config, &config.extra_env).ok().flatten();
365+
let targets = target_tuple::get(query_config, config.target.as_deref(), &config.extra_env)
366+
.unwrap_or_default();
416367
let rustc_cfg = rustc_cfg::get(query_config, None, &config.extra_env);
417368
let data_layout = target_data_layout::get(query_config, None, &config.extra_env);
418369

0 commit comments

Comments
 (0)