@@ -95,17 +95,20 @@ impl<T: Into<String>> From<T> for OverrideFile {
95
95
}
96
96
}
97
97
98
+ // Represents the reason why the active toolchain is active.
98
99
#[ derive( Debug ) ]
99
- pub ( crate ) enum OverrideReason {
100
+ pub ( crate ) enum ActiveReason {
101
+ Default ,
100
102
Environment ,
101
103
CommandLine ,
102
104
OverrideDB ( PathBuf ) ,
103
105
ToolchainFile ( PathBuf ) ,
104
106
}
105
107
106
- impl Display for OverrideReason {
108
+ impl Display for ActiveReason {
107
109
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> std:: result:: Result < ( ) , fmt:: Error > {
108
110
match self {
111
+ Self :: Default => write ! ( f, "default" ) ,
109
112
Self :: Environment => write ! ( f, "environment override by RUSTUP_TOOLCHAIN" ) ,
110
113
Self :: CommandLine => write ! ( f, "overridden by +toolchain on the command line" ) ,
111
114
Self :: OverrideDB ( path) => write ! ( f, "directory override for '{}'" , path. display( ) ) ,
@@ -115,11 +118,11 @@ impl Display for OverrideReason {
115
118
}
116
119
117
120
/// Calls Toolchain::new(), but augments the error message with more context
118
- /// from the OverrideReason if the toolchain isn't installed.
121
+ /// from the ActiveReason if the toolchain isn't installed.
119
122
pub ( crate ) fn new_toolchain_with_reason < ' a > (
120
123
cfg : & ' a Cfg ,
121
124
name : LocalToolchainName ,
122
- reason : & OverrideReason ,
125
+ reason : & ActiveReason ,
123
126
) -> Result < Toolchain < ' a > > {
124
127
match Toolchain :: new ( cfg, name. clone ( ) ) {
125
128
Err ( RustupError :: ToolchainNotInstalled ( _) ) => ( ) ,
@@ -129,21 +132,24 @@ pub(crate) fn new_toolchain_with_reason<'a>(
129
132
}
130
133
131
134
let reason_err = match reason {
132
- OverrideReason :: Environment => {
135
+ ActiveReason :: Environment => {
133
136
"the RUSTUP_TOOLCHAIN environment variable specifies an uninstalled toolchain"
134
137
. to_string ( )
135
138
}
136
- OverrideReason :: CommandLine => {
139
+ ActiveReason :: CommandLine => {
137
140
"the +toolchain on the command line specifies an uninstalled toolchain" . to_string ( )
138
141
}
139
- OverrideReason :: OverrideDB ( ref path) => format ! (
142
+ ActiveReason :: OverrideDB ( ref path) => format ! (
140
143
"the directory override for '{}' specifies an uninstalled toolchain" ,
141
144
utils:: canonicalize_path( path, cfg. notify_handler. as_ref( ) ) . display( ) ,
142
145
) ,
143
- OverrideReason :: ToolchainFile ( ref path) => format ! (
146
+ ActiveReason :: ToolchainFile ( ref path) => format ! (
144
147
"the toolchain file at '{}' specifies an uninstalled toolchain" ,
145
148
utils:: canonicalize_path( path, cfg. notify_handler. as_ref( ) ) . display( ) ,
146
149
) ,
150
+ ActiveReason :: Default => {
151
+ "the default toolchain does not describe an installed toolchain" . to_string ( )
152
+ }
147
153
} ;
148
154
149
155
Err ( anyhow ! ( reason_err) . context ( format ! ( "override toolchain '{name}' is not installed" ) ) )
@@ -478,21 +484,26 @@ impl Cfg {
478
484
. transpose ( ) ?)
479
485
}
480
486
481
- pub ( crate ) fn find_override (
487
+ pub ( crate ) fn find_active_toolchain (
482
488
& self ,
483
489
path : & Path ,
484
- ) -> Result < Option < ( LocalToolchainName , OverrideReason ) > > {
485
- Ok ( self
486
- . find_override_config ( path) ?
487
- . and_then ( |( override_cfg, reason) | override_cfg. toolchain . map ( |t| ( t, reason) ) ) )
490
+ ) -> Result < Option < ( LocalToolchainName , ActiveReason ) > > {
491
+ Ok (
492
+ if let Some ( ( override_config, reason) ) = self . find_override_config ( path) ? {
493
+ override_config. toolchain . map ( |t| ( t, reason) )
494
+ } else {
495
+ self . get_default ( ) ?
496
+ . map ( |x| ( x. into ( ) , ActiveReason :: Default ) )
497
+ } ,
498
+ )
488
499
}
489
500
490
- fn find_override_config ( & self , path : & Path ) -> Result < Option < ( OverrideCfg , OverrideReason ) > > {
501
+ fn find_override_config ( & self , path : & Path ) -> Result < Option < ( OverrideCfg , ActiveReason ) > > {
491
502
let mut override_ = None ;
492
503
493
504
// First check toolchain override from command
494
505
if let Some ( ref name) = self . toolchain_override {
495
- override_ = Some ( ( name. to_string ( ) . into ( ) , OverrideReason :: CommandLine ) ) ;
506
+ override_ = Some ( ( name. to_string ( ) . into ( ) , ActiveReason :: CommandLine ) ) ;
496
507
}
497
508
498
509
// Check RUSTUP_TOOLCHAIN
@@ -501,7 +512,7 @@ impl Cfg {
501
512
// custom, distributable, and absolute path toolchains otherwise
502
513
// rustup's export of a RUSTUP_TOOLCHAIN when running a process will
503
514
// error when a nested rustup invocation occurs
504
- override_ = Some ( ( name. to_string ( ) . into ( ) , OverrideReason :: Environment ) ) ;
515
+ override_ = Some ( ( name. to_string ( ) . into ( ) , ActiveReason :: Environment ) ) ;
505
516
}
506
517
507
518
// Then walk up the directory tree from 'path' looking for either the
@@ -539,14 +550,14 @@ impl Cfg {
539
550
& self ,
540
551
dir : & Path ,
541
552
settings : & Settings ,
542
- ) -> Result < Option < ( OverrideFile , OverrideReason ) > > {
553
+ ) -> Result < Option < ( OverrideFile , ActiveReason ) > > {
543
554
let notify = self . notify_handler . as_ref ( ) ;
544
555
let mut dir = Some ( dir) ;
545
556
546
557
while let Some ( d) = dir {
547
558
// First check the override database
548
559
if let Some ( name) = settings. dir_override ( d, notify) {
549
- let reason = OverrideReason :: OverrideDB ( d. to_owned ( ) ) ;
560
+ let reason = ActiveReason :: OverrideDB ( d. to_owned ( ) ) ;
550
561
return Ok ( Some ( ( name. into ( ) , reason) ) ) ;
551
562
}
552
563
@@ -619,7 +630,7 @@ impl Cfg {
619
630
}
620
631
}
621
632
622
- let reason = OverrideReason :: ToolchainFile ( toolchain_file) ;
633
+ let reason = ActiveReason :: ToolchainFile ( toolchain_file) ;
623
634
return Ok ( Some ( ( override_file, reason) ) ) ;
624
635
}
625
636
@@ -663,7 +674,7 @@ impl Cfg {
663
674
pub ( crate ) fn find_or_install_override_toolchain_or_default (
664
675
& self ,
665
676
path : & Path ,
666
- ) -> Result < ( Toolchain < ' _ > , Option < OverrideReason > ) > {
677
+ ) -> Result < ( Toolchain < ' _ > , Option < ActiveReason > ) > {
667
678
let ( toolchain, components, targets, reason, profile) =
668
679
match self . find_override_config ( path) ? {
669
680
Some ( (
0 commit comments