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`
This commit is contained in:
Denis Cornehl 2021-12-28 21:10:49 +01:00 committed by GitHub
parent eaa3cc1875
commit c63e9a71bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -65,6 +65,7 @@ fn get_module_version(context: &Context, config: &RustConfig) -> Option<String>
// 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<String>
// - `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<String> {
Some(val.trim().to_owned())
}
fn execute_rustup_override_list(cwd: &Path) -> Option<String> {
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<String> {
extract_toolchain_from_rustup_override_list(
&context.exec_cmd("rustup", &["override", "list"])?.stdout,
&context.current_dir,
)
}
fn execute_rustup_default(context: &Context) -> Option<String> {
// `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<String> {