Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/project-model/src/toolchain_info/version.rs')
-rw-r--r--crates/project-model/src/toolchain_info/version.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/crates/project-model/src/toolchain_info/version.rs b/crates/project-model/src/toolchain_info/version.rs
new file mode 100644
index 0000000000..a873944dd8
--- /dev/null
+++ b/crates/project-model/src/toolchain_info/version.rs
@@ -0,0 +1,32 @@
+//! Get the version string of the toolchain.
+
+use anyhow::Context;
+use rustc_hash::FxHashMap;
+use semver::Version;
+use toolchain::Tool;
+
+use crate::{toolchain_info::QueryConfig, utf8_stdout};
+
+pub(crate) fn get(
+ config: QueryConfig<'_>,
+ extra_env: &FxHashMap<String, String>,
+) -> Result<Option<Version>, anyhow::Error> {
+ let (mut cmd, prefix) = match config {
+ QueryConfig::Cargo(sysroot, cargo_toml) => {
+ (sysroot.tool(Tool::Cargo, cargo_toml.parent()), "cargo ")
+ }
+ QueryConfig::Rustc(sysroot, current_dir) => {
+ (sysroot.tool(Tool::Rustc, current_dir), "rustc ")
+ }
+ };
+ cmd.envs(extra_env);
+ cmd.arg("--version");
+ let out = utf8_stdout(&mut cmd).with_context(|| format!("Failed to query rust toolchain version via `{cmd:?}`, is your toolchain setup correctly?"))?;
+
+ let version =
+ out.strip_prefix(prefix).and_then(|it| Version::parse(it.split_whitespace().next()?).ok());
+ if version.is_none() {
+ tracing::warn!("Failed to parse `{cmd:?}` output `{out}` as a semver version");
+ }
+ anyhow::Ok(version)
+}