diff --git a/src/modules/rust.rs b/src/modules/rust.rs index 48352017..58a75ad3 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 {