@@ -516,15 +516,63 @@ impl<'a> Cfg<'a> {
516
516
517
517
pub ( crate ) async fn find_active_toolchain (
518
518
& self ,
519
+ force_install_active : Option < bool > ,
519
520
) -> Result < Option < ( LocalToolchainName , ActiveReason ) > > {
520
- Ok (
521
- if let Some ( ( override_config, reason) ) = self . find_override_config ( ) ? {
522
- Some ( ( override_config. into_local_toolchain_name ( ) , reason) )
523
- } else {
524
- self . get_default ( ) ?
525
- . map ( |x| ( x. into ( ) , ActiveReason :: Default ) )
526
- } ,
527
- )
521
+ let ( components, targets, profile, toolchain, reason) = match self . find_override_config ( ) ? {
522
+ Some ( (
523
+ OverrideCfg :: Official {
524
+ components,
525
+ targets,
526
+ profile,
527
+ toolchain,
528
+ } ,
529
+ reason,
530
+ ) ) => ( components, targets, profile, toolchain, reason) ,
531
+ Some ( ( override_config, reason) ) => {
532
+ return Ok ( Some ( ( override_config. into_local_toolchain_name ( ) , reason) ) ) ;
533
+ }
534
+ None => {
535
+ return Ok ( self
536
+ . get_default ( ) ?
537
+ . map ( |x| ( x. into ( ) , ActiveReason :: Default ) ) ) ;
538
+ }
539
+ } ;
540
+
541
+ let should_install_active = force_install_active. unwrap_or_else ( || {
542
+ self . process
543
+ . var ( "RUSTUP_AUTO_INSTALL" )
544
+ . map_or ( true , |it| it != "0" )
545
+ } ) ;
546
+
547
+ if !should_install_active {
548
+ return Ok ( Some ( ( ( & toolchain) . into ( ) , reason) ) ) ;
549
+ }
550
+
551
+ let components = components. iter ( ) . map ( AsRef :: as_ref) . collect :: < Vec < _ > > ( ) ;
552
+ let targets = targets. iter ( ) . map ( AsRef :: as_ref) . collect :: < Vec < _ > > ( ) ;
553
+ match DistributableToolchain :: new ( self , toolchain. clone ( ) ) {
554
+ Err ( RustupError :: ToolchainNotInstalled { .. } ) => {
555
+ DistributableToolchain :: install (
556
+ self ,
557
+ & toolchain,
558
+ & components,
559
+ & targets,
560
+ profile. unwrap_or_default ( ) ,
561
+ false ,
562
+ )
563
+ . await ?;
564
+ }
565
+ Ok ( mut distributable) => {
566
+ if !distributable. components_exist ( & components, & targets) ? {
567
+ distributable
568
+ . update ( & components, & targets, profile. unwrap_or_default ( ) )
569
+ . await ?;
570
+ }
571
+ }
572
+ Err ( e) => return Err ( e. into ( ) ) ,
573
+ } ;
574
+
575
+ Ok ( Some ( ( ( & toolchain) . into ( ) , reason) ) )
528
576
}
529
577
530
578
fn find_override_config ( & self ) -> Result < Option < ( OverrideCfg , ActiveReason ) > > {
@@ -709,7 +757,7 @@ impl<'a> Cfg<'a> {
709
757
self . set_toolchain_override ( & ResolvableToolchainName :: try_from ( & t[ 1 ..] ) ?) ;
710
758
}
711
759
712
- let Some ( ( name, _) ) = self . find_active_toolchain ( ) . await ? else {
760
+ let Some ( ( name, _) ) = self . find_active_toolchain ( None ) . await ? else {
713
761
return Ok ( None ) ;
714
762
} ;
715
763
Ok ( Some ( Toolchain :: new ( self , name) ?. rustc_version ( ) ) )
@@ -739,7 +787,7 @@ impl<'a> Cfg<'a> {
739
787
let toolchain = match name {
740
788
Some ( tc) => tc,
741
789
None => {
742
- self . find_active_toolchain ( )
790
+ self . find_active_toolchain ( None )
743
791
. await ?
744
792
. ok_or_else ( || no_toolchain_error ( self . process ) ) ?
745
793
. 0
0 commit comments