1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-24 21:57:41 +00:00

perf(rust): avoid calling rustup in more conditions (#4174)

This commit is contained in:
David Knaack 2022-07-20 11:15:46 +02:00 committed by GitHub
parent 61438484bd
commit d8ac940098
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,9 +55,10 @@ impl RustToolingEnvironmentInfo {
// To display appropriate versions preventing `rustc` from downloading toolchains, we have to // To display appropriate versions preventing `rustc` from downloading toolchains, we have to
// check // check
// 1. `$RUSTUP_TOOLCHAIN` // 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 // 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. // as `rustup` does.
// https://github.com/rust-lang/rustup.rs/tree/eb694fcada7becc5d9d160bf7c623abe84f8971d#override-precedence // https://github.com/rust-lang/rustup.rs/tree/eb694fcada7becc5d9d160bf7c623abe84f8971d#override-precedence
// //
@ -74,6 +75,11 @@ impl RustToolingEnvironmentInfo {
.lookup_override(context.current_dir.as_path()) .lookup_override(context.current_dir.as_path())
}) })
.or_else(|| find_rust_toolchain_file(context)) .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)); .or_else(|| execute_rustup_default(context));
log::debug!("Environmental toolchain override is {:?}", out); log::debug!("Environmental toolchain override is {:?}", out);
@ -87,12 +93,32 @@ impl RustToolingEnvironmentInfo {
fn get_rustup_rustc_version(&self, context: &Context) -> &RustupRunRustcVersionOutcome { fn get_rustup_rustc_version(&self, context: &Context) -> &RustupRunRustcVersionOutcome {
self.rustup_rustc_output.get_or_init(|| { self.rustup_rustc_output.get_or_init(|| {
let out = if let Some(toolchain) = self.get_env_toolchain_override(context) { let out = if let Some(toolchain) = self.get_env_toolchain_override(context) {
create_command("rustup") // First try runnig ~/.rustup/toolchains/<toolchain>/bin/rustc --version
.and_then(|mut cmd| { rustup_home()
cmd.args(&["run", toolchain, "rustc", "--version"]) .map(|rustup_folder| {
.current_dir(&context.current_dir) rustup_folder
.output() .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 <toolchain> 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) .map(extract_toolchain_from_rustup_run_rustc_version)
.unwrap_or(RustupRunRustcVersionOutcome::RustupNotWorking) .unwrap_or(RustupRunRustcVersionOutcome::RustupNotWorking)
} else { } else {