@@ -9,6 +9,7 @@ use std::sync::mpsc::SyncSender;
9
9
use build_helper:: git:: get_git_modified_files;
10
10
use ignore:: WalkBuilder ;
11
11
12
+ use crate :: core:: build_steps:: tool;
12
13
use crate :: core:: builder:: Builder ;
13
14
use crate :: utils:: build_stamp:: BuildStamp ;
14
15
use crate :: utils:: exec:: command;
@@ -237,11 +238,21 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
237
238
238
239
let override_ = override_builder. build ( ) . unwrap ( ) ; // `override` is a reserved keyword
239
240
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
243
+ // This is useful for `x test tidy`, since `x test` defaults to stage 1,
244
+ // but we don't actually want to use the in-tree stage 1 rustfmt for that command.
245
+ || !build. config . is_explicit_stage ( )
246
+ {
247
+ build. initial_rustfmt ( ) . unwrap_or_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
+ } else {
252
+ let compiler = build. compiler ( build. top_stage , build. config . build ) ;
253
+ build. ensure ( tool:: Rustfmt { compiler, target : build. config . build } ) . tool_path
254
+ } ;
255
+
245
256
let src = build. src . clone ( ) ;
246
257
let ( tx, rx) : ( SyncSender < PathBuf > , _ ) = std:: sync:: mpsc:: sync_channel ( 128 ) ;
247
258
let walker = WalkBuilder :: new ( src. clone ( ) ) . types ( matcher) . overrides ( override_) . build_parallel ( ) ;
0 commit comments