From c63e9a71bd958c576100fbbeaf5723bb22450fd9 Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Tue, 28 Dec 2021 21:10:49 +0100 Subject: [PATCH] perf(rust): additionally check `rustup default` for faster result. (#3354) * perf(rust): additionally check `rustup default` for faster result. After checking directory overrides we were directly falling back to the relatively slow call to `rustc --version`. Inserting a call to `rustup default` leads to a quicker response. * use `context.exec_cmd` instead of `create_command` --- src/modules/rust.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/modules/rust.rs b/src/modules/rust.rs index 48352017b..58a75ad38 100644 --- a/src/modules/rust.rs +++ b/src/modules/rust.rs @@ -65,6 +65,7 @@ fn get_module_version(context: &Context, config: &RustConfig) -> Option // 1. `$RUSTUP_TOOLCHAIN` // 2. `rustup override list` // 3. `rust-toolchain` or `rust-toolchain.toml` in `.` or parent directories + // 4. `rustup default` // as `rustup` does. // https://github.com/rust-lang/rustup.rs/tree/eb694fcada7becc5d9d160bf7c623abe84f8971d#override-precedence // @@ -74,8 +75,9 @@ fn get_module_version(context: &Context, config: &RustConfig) -> Option // - `rustup show active-toolchain` // - `rustup which` if let Some(toolchain) = env_rustup_toolchain(context) - .or_else(|| execute_rustup_override_list(&context.current_dir)) + .or_else(|| execute_rustup_override_list(context)) .or_else(|| find_rust_toolchain_file(context)) + .or_else(|| execute_rustup_default(context)) { match execute_rustup_run_rustc_version(&toolchain) { RustupRunRustcVersionOutcome::RustcVersion(rustc_version) => { @@ -99,14 +101,22 @@ fn env_rustup_toolchain(context: &Context) -> Option { Some(val.trim().to_owned()) } -fn execute_rustup_override_list(cwd: &Path) -> Option { - let Output { stdout, .. } = create_command("rustup") - .ok()? - .args(&["override", "list"]) - .output() - .ok()?; - let stdout = String::from_utf8(stdout).ok()?; - extract_toolchain_from_rustup_override_list(&stdout, cwd) +fn execute_rustup_override_list(context: &Context) -> Option { + extract_toolchain_from_rustup_override_list( + &context.exec_cmd("rustup", &["override", "list"])?.stdout, + &context.current_dir, + ) +} + +fn execute_rustup_default(context: &Context) -> Option { + // `rustup default` output is: + // stable-x86_64-apple-darwin (default) + context + .exec_cmd("rustup", &["default"])? + .stdout + .split_whitespace() + .next() + .map(str::to_owned) } fn extract_toolchain_from_rustup_override_list(stdout: &str, cwd: &Path) -> Option {