@@ -9,6 +9,7 @@ use std::sync::mpsc::SyncSender;
99use build_helper:: git:: get_git_modified_files;
1010use ignore:: WalkBuilder ;
1111
12+ use crate :: core:: build_steps:: tool;
1213use crate :: core:: builder:: Builder ;
1314use crate :: utils:: build_stamp:: BuildStamp ;
1415use crate :: utils:: exec:: command;
@@ -237,11 +238,17 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
237238
238239 let override_ = override_builder. build ( ) . unwrap ( ) ; // `override` is a reserved keyword
239240
240- let rustfmt_path = build. initial_rustfmt ( ) . unwrap_or_else ( || {
241- eprintln ! ( "fmt error: `x fmt` is not supported on this channel" ) ;
242- crate :: exit!( 1 ) ;
243- } ) ;
244- assert ! ( rustfmt_path. exists( ) , "{}" , rustfmt_path. display( ) ) ;
241+ // If stage is not explicitly set or is 0, try using the precompiled one first.
242+ let rustfmt_path = if build. top_stage == 0 || !build. config . is_explicit_stage ( ) {
243+ build. initial_rustfmt ( ) . unwrap_or_else ( || {
244+ let compiler = build. compiler ( build. top_stage , build. config . build ) ;
245+ build. ensure ( tool:: Rustfmt { compiler, target : build. config . build } ) . tool_path
246+ } )
247+ } else {
248+ let compiler = build. compiler ( build. top_stage , build. config . build ) ;
249+ build. ensure ( tool:: Rustfmt { compiler, target : build. config . build } ) . tool_path
250+ } ;
251+
245252 let src = build. src . clone ( ) ;
246253 let ( tx, rx) : ( SyncSender < PathBuf > , _ ) = std:: sync:: mpsc:: sync_channel ( 128 ) ;
247254 let walker = WalkBuilder :: new ( src. clone ( ) ) . types ( matcher) . overrides ( override_) . build_parallel ( ) ;
0 commit comments