From d8ac940098eb16417742723c627d0de864597410 Mon Sep 17 00:00:00 2001 From: David Knaack Date: Wed, 20 Jul 2022 11:15:46 +0200 Subject: [PATCH] perf(rust): avoid calling `rustup` in more conditions (#4174) --- src/modules/rust.rs | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/modules/rust.rs b/src/modules/rust.rs index 1e46050a..35e38d9a 100644 --- a/src/modules/rust.rs +++ b/src/modules/rust.rs @@ -55,9 +55,10 @@ impl RustToolingEnvironmentInfo { // To display appropriate versions preventing `rustc` from downloading toolchains, we have to // check // 1. `$RUSTUP_TOOLCHAIN` - // 2. `rustup override list` + // 2. The override list from ~/.rustup/settings.toml (like `rustup override list`) // 3. `rust-toolchain` or `rust-toolchain.toml` in `.` or parent directories - // 4. `rustup default` + // 4. The `default_toolchain` from ~/.rustup/settings.toml (like `rustup default`) + // 5. `rustup default` (in addition to the above, this also looks at global fallback config files) // as `rustup` does. // https://github.com/rust-lang/rustup.rs/tree/eb694fcada7becc5d9d160bf7c623abe84f8971d#override-precedence // @@ -74,6 +75,11 @@ impl RustToolingEnvironmentInfo { .lookup_override(context.current_dir.as_path()) }) .or_else(|| find_rust_toolchain_file(context)) + .or_else(|| { + self.get_rustup_settings(context) + .default_toolchain() + .map(std::string::ToString::to_string) + }) .or_else(|| execute_rustup_default(context)); log::debug!("Environmental toolchain override is {:?}", out); @@ -87,12 +93,32 @@ impl RustToolingEnvironmentInfo { fn get_rustup_rustc_version(&self, context: &Context) -> &RustupRunRustcVersionOutcome { self.rustup_rustc_output.get_or_init(|| { let out = if let Some(toolchain) = self.get_env_toolchain_override(context) { - create_command("rustup") - .and_then(|mut cmd| { - cmd.args(&["run", toolchain, "rustc", "--version"]) - .current_dir(&context.current_dir) - .output() + // First try runnig ~/.rustup/toolchains//bin/rustc --version + rustup_home() + .map(|rustup_folder| { + rustup_folder + .join("toolchains") + .join(toolchain) + .join("bin") + .join("rustc") }) + .and_then(|rustc| { + log::trace!("Running rustc --version directly with {:?}", rustc); + create_command(rustc).map(|mut cmd| { + cmd.arg("--version"); + cmd + }) + }) + .or_else(|_| { + // If that fails, try running rustup rustup run rustc --version + // Depending on the source of the toolchain override, it might not have been a full toolchain name ("stable" or "nightly"). + log::trace!("Running rustup {toolchain} rustc --version"); + create_command("rustup").map(|mut cmd| { + cmd.args(&["run", toolchain, "rustc", "--version"]); + cmd + }) + }) + .and_then(|mut cmd| cmd.current_dir(&context.current_dir).output()) .map(extract_toolchain_from_rustup_run_rustc_version) .unwrap_or(RustupRunRustcVersionOutcome::RustupNotWorking) } else {