@@ -12,7 +12,7 @@ use itertools::Itertools;
1212use la_arena:: { Arena , Idx } ;
1313use paths:: { AbsPath , AbsPathBuf } ;
1414use rustc_hash:: FxHashMap ;
15- use toolchain:: probe_for_binary;
15+ use toolchain:: { probe_for_binary, Tool } ;
1616
1717use crate :: { utf8_stdout, CargoConfig , CargoWorkspace , ManifestPath } ;
1818
@@ -193,23 +193,26 @@ impl Sysroot {
193193 Ok ( Sysroot :: load ( sysroot_dir, Some ( sysroot_src_dir) , metadata) )
194194 }
195195
196- pub fn set_rustup_toolchain_env ( cmd : & mut Command , sysroot : Option < & Self > ) {
197- if let Some ( sysroot) = sysroot {
198- cmd. env ( "RUSTUP_TOOLCHAIN" , AsRef :: < std:: path:: Path > :: as_ref ( & sysroot. root ) ) ;
199- }
200- }
201-
202- /// Returns a `Command` that is configured to run `rustc` from the sysroot if it exists,
203- /// otherwise returns what [toolchain::Tool::Rustc] returns.
204- pub fn rustc ( sysroot : Option < & Self > ) -> Command {
205- let mut cmd = Command :: new ( match sysroot {
196+ /// Returns a command to run a tool preferring the cargo proxies if the sysroot exists.
197+ pub fn tool ( sysroot : Option < & Self > , tool : Tool ) -> Command {
198+ match sysroot {
206199 Some ( sysroot) => {
207- toolchain:: Tool :: Rustc . path_in_or_discover ( sysroot. root . join ( "bin" ) . as_ref ( ) )
200+ // special case rustc, we can look that up directly in the sysroot's bin folder
201+ // as it should never invoke another cargo binary
202+ if let Tool :: Rustc = tool {
203+ if let Some ( path) =
204+ probe_for_binary ( sysroot. root . join ( "bin" ) . join ( Tool :: Rustc . name ( ) ) . into ( ) )
205+ {
206+ return Command :: new ( path) ;
207+ }
208+ }
209+
210+ let mut cmd = Command :: new ( tool. prefer_proxy ( ) ) ;
211+ cmd. env ( "RUSTUP_TOOLCHAIN" , AsRef :: < std:: path:: Path > :: as_ref ( & sysroot. root ) ) ;
212+ cmd
208213 }
209- None => toolchain:: Tool :: Rustc . path ( ) ,
210- } ) ;
211- Self :: set_rustup_toolchain_env ( & mut cmd, sysroot) ;
212- cmd
214+ _ => Command :: new ( tool. path ( ) ) ,
215+ }
213216 }
214217
215218 pub fn discover_proc_macro_srv ( & self ) -> anyhow:: Result < AbsPathBuf > {
@@ -411,7 +414,7 @@ fn discover_sysroot_dir(
411414 current_dir : & AbsPath ,
412415 extra_env : & FxHashMap < String , String > ,
413416) -> Result < AbsPathBuf > {
414- let mut rustc = Command :: new ( toolchain :: rustc ( ) ) ;
417+ let mut rustc = Command :: new ( Tool :: Rustc . path ( ) ) ;
415418 rustc. envs ( extra_env) ;
416419 rustc. current_dir ( current_dir) . args ( [ "--print" , "sysroot" ] ) ;
417420 tracing:: debug!( "Discovering sysroot by {:?}" , rustc) ;
@@ -443,7 +446,7 @@ fn discover_sysroot_src_dir_or_add_component(
443446) -> Result < AbsPathBuf > {
444447 discover_sysroot_src_dir ( sysroot_path)
445448 . or_else ( || {
446- let mut rustup = Command :: new ( toolchain :: rustup ( ) ) ;
449+ let mut rustup = Command :: new ( Tool :: Rustup . prefer_proxy ( ) ) ;
447450 rustup. envs ( extra_env) ;
448451 rustup. current_dir ( current_dir) . args ( [ "component" , "add" , "rust-src" ] ) ;
449452 tracing:: info!( "adding rust-src component by {:?}" , rustup) ;
0 commit comments