@@ -19,7 +19,6 @@ use itertools::Itertools;
19
19
use tracing:: { info, trace, warn} ;
20
20
use tracing_subscriber:: { EnvFilter , Registry , reload:: Handle } ;
21
21
22
- use crate :: dist:: AutoInstallMode ;
23
22
use crate :: {
24
23
cli:: {
25
24
common:: { self , PackageUpdate , update_console_filter} ,
@@ -47,6 +46,7 @@ use crate::{
47
46
} ,
48
47
utils:: { self , ExitCode } ,
49
48
} ;
49
+ use crate :: { component_for_bin, dist:: AutoInstallMode } ;
50
50
51
51
const TOOLCHAIN_OVERRIDE_ERROR : & str = "To override the toolchain using the 'rustup +toolchain' syntax, \
52
52
make sure to prefix the toolchain override with a '+'";
@@ -1034,12 +1034,30 @@ async fn which(
1034
1034
binary : & str ,
1035
1035
toolchain : Option < ResolvableToolchainName > ,
1036
1036
) -> 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
+ }
1038
1043
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
+ } ;
1040
1050
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}'.\n To install, run `rustup component add {selector}{component_name}`"
1060
+ ) ) ;
1043
1061
}
1044
1062
1045
1063
#[ tracing:: instrument( level = "trace" , skip_all) ]
0 commit comments