Skip to content

Commit fbfb71c

Browse files
committed
feat: improve error message for rustup which
1 parent 658d317 commit fbfb71c

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/cli/rustup_mode.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use itertools::Itertools;
1919
use tracing::{info, trace, warn};
2020
use tracing_subscriber::{EnvFilter, Registry, reload::Handle};
2121

22-
use crate::dist::AutoInstallMode;
2322
use crate::{
2423
cli::{
2524
common::{self, PackageUpdate, update_console_filter},
@@ -47,6 +46,7 @@ use crate::{
4746
},
4847
utils::{self, ExitCode},
4948
};
49+
use crate::{component_for_bin, dist::AutoInstallMode};
5050

5151
const TOOLCHAIN_OVERRIDE_ERROR: &str = "To override the toolchain using the 'rustup +toolchain' syntax, \
5252
make sure to prefix the toolchain override with a '+'";
@@ -1034,12 +1034,30 @@ async fn which(
10341034
binary: &str,
10351035
toolchain: Option<ResolvableToolchainName>,
10361036
) -> Result<utils::ExitCode> {
1037-
let binary_path = cfg.resolve_toolchain(toolchain).await?.binary_file(binary);
1037+
let toolchain = cfg.resolve_toolchain(toolchain).await?;
1038+
let binary_path = toolchain.binary_file(binary);
1039+
if utils::is_file(&binary_path) {
1040+
writeln!(cfg.process.stdout().lock(), "{}", binary_path.display())?;
1041+
return Ok(utils::ExitCode(0));
1042+
}
10381043

1039-
utils::assert_is_file(&binary_path)?;
1044+
let toolchain_name = toolchain.name();
1045+
let Some(component_name) = component_for_bin(binary) else {
1046+
return Err(anyhow!(
1047+
"Unknown binary '{binary}' in toolchain '{toolchain_name}'.",
1048+
));
1049+
};
10401050

1041-
writeln!(cfg.process.stdout().lock(), "{}", binary_path.display())?;
1042-
Ok(utils::ExitCode(0))
1051+
let active = matches!(cfg.active_toolchain(), Ok(Some((t, _))) if &t == toolchain_name);
1052+
let selector = if active {
1053+
String::new()
1054+
} else {
1055+
format!("--toolchain {} ", toolchain.name())
1056+
};
1057+
1058+
return Err(anyhow!(
1059+
"'{binary}' is not installed for the toolchain '{toolchain_name}'.\nTo install, run `rustup component add {selector}{component_name}`"
1060+
));
10431061
}
10441062

10451063
#[tracing::instrument(level = "trace", skip_all)]

0 commit comments

Comments
 (0)