@@ -14,7 +14,19 @@ use crate::core::builder::Builder;
14
14
use crate :: utils:: exec:: command;
15
15
use crate :: utils:: helpers:: { self , program_out_of_date, t} ;
16
16
17
- fn rustfmt ( src : & Path , rustfmt : & Path , paths : & [ PathBuf ] , check : bool ) -> impl FnMut ( bool ) -> bool {
17
+ #[ must_use]
18
+ enum RustfmtStatus {
19
+ InProgress ,
20
+ Ok ,
21
+ Failed ,
22
+ }
23
+
24
+ fn rustfmt (
25
+ src : & Path ,
26
+ rustfmt : & Path ,
27
+ paths : & [ PathBuf ] ,
28
+ check : bool ,
29
+ ) -> impl FnMut ( bool ) -> RustfmtStatus {
18
30
let mut cmd = Command :: new ( rustfmt) ;
19
31
// Avoid the submodule config paths from coming into play. We only allow a single global config
20
32
// for the workspace for now.
@@ -26,30 +38,20 @@ fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl F
26
38
cmd. arg ( "--check" ) ;
27
39
}
28
40
cmd. args ( paths) ;
29
- let cmd_debug = format ! ( "{cmd:?}" ) ;
30
41
let mut cmd = cmd. spawn ( ) . expect ( "running rustfmt" ) ;
31
42
// Poor man's async: return a closure that might wait for rustfmt's completion (depending on
32
43
// the value of the `block` argument).
33
- move |block : bool | -> bool {
44
+ move |block : bool | -> RustfmtStatus {
34
45
let status = if !block {
35
46
match cmd. try_wait ( ) {
36
47
Ok ( Some ( status) ) => Ok ( status) ,
37
- Ok ( None ) => return false ,
48
+ Ok ( None ) => return RustfmtStatus :: InProgress ,
38
49
Err ( err) => Err ( err) ,
39
50
}
40
51
} else {
41
52
cmd. wait ( )
42
53
} ;
43
- if !status. unwrap ( ) . success ( ) {
44
- eprintln ! (
45
- "fmt error: Running `{}` failed.\n If you're running `tidy`, \
46
- try again with `--bless`. Or, if you just want to format \
47
- code, run `./x.py fmt` instead.",
48
- cmd_debug,
49
- ) ;
50
- crate :: exit!( 1 ) ;
51
- }
52
- true
54
+ if status. unwrap ( ) . success ( ) { RustfmtStatus :: Ok } else { RustfmtStatus :: Failed }
53
55
}
54
56
}
55
57
@@ -240,6 +242,8 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
240
242
// Spawn child processes on a separate thread so we can batch entries we have received from
241
243
// ignore.
242
244
let thread = std:: thread:: spawn ( move || {
245
+ let mut result = Ok ( ( ) ) ;
246
+
243
247
let mut children = VecDeque :: new ( ) ;
244
248
while let Ok ( path) = rx. recv ( ) {
245
249
// Try getting more paths from the channel to amortize the overhead of spawning
@@ -251,22 +255,38 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
251
255
252
256
// Poll completion before waiting.
253
257
for i in ( 0 ..children. len ( ) ) . rev ( ) {
254
- if children[ i] ( false ) {
255
- children. swap_remove_back ( i) ;
256
- break ;
258
+ match children[ i] ( false ) {
259
+ RustfmtStatus :: InProgress => { }
260
+ RustfmtStatus :: Failed => {
261
+ result = Err ( ( ) ) ;
262
+ children. swap_remove_back ( i) ;
263
+ break ;
264
+ }
265
+ RustfmtStatus :: Ok => {
266
+ children. swap_remove_back ( i) ;
267
+ break ;
268
+ }
257
269
}
258
270
}
259
271
260
272
if children. len ( ) >= max_processes {
261
273
// Await oldest child.
262
- children. pop_front ( ) . unwrap ( ) ( true ) ;
274
+ match children. pop_front ( ) . unwrap ( ) ( true ) {
275
+ RustfmtStatus :: InProgress | RustfmtStatus :: Ok => { }
276
+ RustfmtStatus :: Failed => result = Err ( ( ) ) ,
277
+ }
263
278
}
264
279
}
265
280
266
281
// Await remaining children.
267
282
for mut child in children {
268
- child ( true ) ;
283
+ match child ( true ) {
284
+ RustfmtStatus :: InProgress | RustfmtStatus :: Ok => { }
285
+ RustfmtStatus :: Failed => result = Err ( ( ) ) ,
286
+ }
269
287
}
288
+
289
+ result
270
290
} ) ;
271
291
272
292
let formatted_paths = Mutex :: new ( Vec :: new ( ) ) ;
@@ -299,7 +319,12 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
299
319
300
320
drop ( tx) ;
301
321
302
- thread. join ( ) . unwrap ( ) ;
322
+ let result = thread. join ( ) . unwrap ( ) ;
323
+
324
+ if result. is_err ( ) {
325
+ crate :: exit!( 1 ) ;
326
+ }
327
+
303
328
if !check {
304
329
update_rustfmt_version ( build) ;
305
330
}
0 commit comments