Skip to content

Commit d8ac940

Browse files
authored
perf(rust): avoid calling rustup in more conditions (starship#4174)
1 parent 6143848 commit d8ac940

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/modules/rust.rs

+33-7
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ impl RustToolingEnvironmentInfo {
5555
// To display appropriate versions preventing `rustc` from downloading toolchains, we have to
5656
// check
5757
// 1. `$RUSTUP_TOOLCHAIN`
58-
// 2. `rustup override list`
58+
// 2. The override list from ~/.rustup/settings.toml (like `rustup override list`)
5959
// 3. `rust-toolchain` or `rust-toolchain.toml` in `.` or parent directories
60-
// 4. `rustup default`
60+
// 4. The `default_toolchain` from ~/.rustup/settings.toml (like `rustup default`)
61+
// 5. `rustup default` (in addition to the above, this also looks at global fallback config files)
6162
// as `rustup` does.
6263
// https://github.com/rust-lang/rustup.rs/tree/eb694fcada7becc5d9d160bf7c623abe84f8971d#override-precedence
6364
//
@@ -74,6 +75,11 @@ impl RustToolingEnvironmentInfo {
7475
.lookup_override(context.current_dir.as_path())
7576
})
7677
.or_else(|| find_rust_toolchain_file(context))
78+
.or_else(|| {
79+
self.get_rustup_settings(context)
80+
.default_toolchain()
81+
.map(std::string::ToString::to_string)
82+
})
7783
.or_else(|| execute_rustup_default(context));
7884

7985
log::debug!("Environmental toolchain override is {:?}", out);
@@ -87,12 +93,32 @@ impl RustToolingEnvironmentInfo {
8793
fn get_rustup_rustc_version(&self, context: &Context) -> &RustupRunRustcVersionOutcome {
8894
self.rustup_rustc_output.get_or_init(|| {
8995
let out = if let Some(toolchain) = self.get_env_toolchain_override(context) {
90-
create_command("rustup")
91-
.and_then(|mut cmd| {
92-
cmd.args(&["run", toolchain, "rustc", "--version"])
93-
.current_dir(&context.current_dir)
94-
.output()
96+
// First try runnig ~/.rustup/toolchains/<toolchain>/bin/rustc --version
97+
rustup_home()
98+
.map(|rustup_folder| {
99+
rustup_folder
100+
.join("toolchains")
101+
.join(toolchain)
102+
.join("bin")
103+
.join("rustc")
104+
})
105+
.and_then(|rustc| {
106+
log::trace!("Running rustc --version directly with {:?}", rustc);
107+
create_command(rustc).map(|mut cmd| {
108+
cmd.arg("--version");
109+
cmd
110+
})
111+
})
112+
.or_else(|_| {
113+
// If that fails, try running rustup rustup run <toolchain> rustc --version
114+
// Depending on the source of the toolchain override, it might not have been a full toolchain name ("stable" or "nightly").
115+
log::trace!("Running rustup {toolchain} rustc --version");
116+
create_command("rustup").map(|mut cmd| {
117+
cmd.args(&["run", toolchain, "rustc", "--version"]);
118+
cmd
119+
})
95120
})
121+
.and_then(|mut cmd| cmd.current_dir(&context.current_dir).output())
96122
.map(extract_toolchain_from_rustup_run_rustc_version)
97123
.unwrap_or(RustupRunRustcVersionOutcome::RustupNotWorking)
98124
} else {

0 commit comments

Comments
 (0)