1
1
use crate :: utils:: {
2
- ClippyInfo , ErrAction , FileUpdater , UpdateMode , UpdateStatus , panic_action, run_with_args_split, run_with_output,
2
+ ErrAction , FileUpdater , UpdateMode , UpdateStatus , expect_action, run_with_output, split_args_for_threads,
3
+ walk_dir_no_dot_or_target,
3
4
} ;
4
5
use itertools:: Itertools ;
5
6
use rustc_lexer:: { TokenKind , tokenize} ;
@@ -9,7 +10,6 @@ use std::io::{self, Read};
9
10
use std:: ops:: ControlFlow ;
10
11
use std:: path:: PathBuf ;
11
12
use std:: process:: { self , Command , Stdio } ;
12
- use walkdir:: WalkDir ;
13
13
14
14
pub enum Error {
15
15
Io ( io:: Error ) ,
@@ -260,50 +260,27 @@ fn fmt_syms(update_mode: UpdateMode) {
260
260
) ;
261
261
}
262
262
263
- fn run_rustfmt ( clippy : & ClippyInfo , update_mode : UpdateMode ) {
263
+ fn run_rustfmt ( update_mode : UpdateMode ) {
264
264
let mut rustfmt_path = String :: from_utf8 ( run_with_output (
265
265
"rustup which rustfmt" ,
266
266
Command :: new ( "rustup" ) . args ( [ "which" , "rustfmt" ] ) ,
267
267
) )
268
268
. expect ( "invalid rustfmt path" ) ;
269
269
rustfmt_path. truncate ( rustfmt_path. trim_end ( ) . len ( ) ) ;
270
270
271
- let mut cargo_path = String :: from_utf8 ( run_with_output (
272
- "rustup which cargo" ,
273
- Command :: new ( "rustup" ) . args ( [ "which" , "cargo" ] ) ,
274
- ) )
275
- . expect ( "invalid cargo path" ) ;
276
- cargo_path. truncate ( cargo_path. trim_end ( ) . len ( ) ) ;
277
-
278
- // Start all format jobs first before waiting on the results.
279
- let mut children = Vec :: with_capacity ( 16 ) ;
280
- for & path in & [
281
- "." ,
282
- "clippy_config" ,
283
- "clippy_dev" ,
284
- "clippy_lints" ,
285
- "clippy_lints_internal" ,
286
- "clippy_utils" ,
287
- "rustc_tools_util" ,
288
- "lintcheck" ,
289
- ] {
290
- let mut cmd = Command :: new ( & cargo_path) ;
291
- cmd. current_dir ( clippy. path . join ( path) )
292
- . args ( [ "fmt" ] )
293
- . env ( "RUSTFMT" , & rustfmt_path)
294
- . stdout ( Stdio :: null ( ) )
295
- . stdin ( Stdio :: null ( ) )
296
- . stderr ( Stdio :: piped ( ) ) ;
297
- if update_mode. is_check ( ) {
298
- cmd. arg ( "--check" ) ;
299
- }
300
- match cmd. spawn ( ) {
301
- Ok ( x) => children. push ( ( "cargo fmt" , x) ) ,
302
- Err ( ref e) => panic_action ( & e, ErrAction :: Run , "cargo fmt" . as_ref ( ) ) ,
303
- }
304
- }
271
+ let args: Vec < _ > = walk_dir_no_dot_or_target ( )
272
+ . filter_map ( |e| {
273
+ let e = expect_action ( e, ErrAction :: Read , "." ) ;
274
+ e. path ( )
275
+ . as_os_str ( )
276
+ . as_encoded_bytes ( )
277
+ . ends_with ( b".rs" )
278
+ . then ( || e. into_path ( ) . into_os_string ( ) )
279
+ } )
280
+ . collect ( ) ;
305
281
306
- run_with_args_split (
282
+ let mut children: Vec < _ > = split_args_for_threads (
283
+ 32 ,
307
284
|| {
308
285
let mut cmd = Command :: new ( & rustfmt_path) ;
309
286
if update_mode. is_check ( ) {
@@ -312,66 +289,44 @@ fn run_rustfmt(clippy: &ClippyInfo, update_mode: UpdateMode) {
312
289
cmd. stdout ( Stdio :: null ( ) )
313
290
. stdin ( Stdio :: null ( ) )
314
291
. stderr ( Stdio :: piped ( ) )
315
- . args ( [ "--config " , "show_parse_errors=false " ] ) ;
292
+ . args ( [ "--unstable-features " , "--skip-children " ] ) ;
316
293
cmd
317
294
} ,
318
- |cmd| match cmd. spawn ( ) {
319
- Ok ( x) => children. push ( ( "rustfmt" , x) ) ,
320
- Err ( ref e) => panic_action ( & e, ErrAction :: Run , "rustfmt" . as_ref ( ) ) ,
321
- } ,
322
- WalkDir :: new ( "tests" )
323
- . into_iter ( )
324
- . filter_entry ( |p| p. path ( ) . file_name ( ) . is_none_or ( |x| x != "skip_rustfmt" ) )
325
- . filter_map ( |e| {
326
- let e = e. expect ( "error reading `tests`" ) ;
327
- e. path ( )
328
- . as_os_str ( )
329
- . as_encoded_bytes ( )
330
- . ends_with ( b".rs" )
331
- . then ( || e. into_path ( ) . into_os_string ( ) )
332
- } ) ,
333
- ) ;
295
+ args. iter ( ) ,
296
+ )
297
+ . map ( |mut cmd| expect_action ( cmd. spawn ( ) , ErrAction :: Run , "rustfmt" ) )
298
+ . collect ( ) ;
334
299
335
- for ( name, child) in & mut children {
336
- match child. wait ( ) {
337
- Ok ( status) => match ( update_mode, status. exit_ok ( ) ) {
338
- ( UpdateMode :: Check | UpdateMode :: Change , Ok ( ( ) ) ) => { } ,
339
- ( UpdateMode :: Check , Err ( _) ) => {
340
- let mut s = String :: new ( ) ;
341
- if let Some ( mut stderr) = child. stderr . take ( )
342
- && stderr. read_to_string ( & mut s) . is_ok ( )
343
- {
344
- eprintln ! ( "{s}" ) ;
345
- }
346
- eprintln ! ( "Formatting check failed!\n Run `cargo dev fmt` to update." ) ;
347
- process:: exit ( 1 ) ;
348
- } ,
349
- ( UpdateMode :: Change , Err ( e) ) => {
350
- let mut s = String :: new ( ) ;
351
- if let Some ( mut stderr) = child. stderr . take ( )
352
- && stderr. read_to_string ( & mut s) . is_ok ( )
353
- {
354
- eprintln ! ( "{s}" ) ;
355
- }
356
- panic_action ( & e, ErrAction :: Run , name. as_ref ( ) ) ;
357
- } ,
300
+ for child in & mut children {
301
+ let status = expect_action ( child. wait ( ) , ErrAction :: Run , "rustfmt" ) ;
302
+ match ( update_mode, status. exit_ok ( ) ) {
303
+ ( UpdateMode :: Check | UpdateMode :: Change , Ok ( ( ) ) ) => { } ,
304
+ ( UpdateMode :: Check , Err ( _) ) => {
305
+ let mut s = String :: new ( ) ;
306
+ if let Some ( mut stderr) = child. stderr . take ( )
307
+ && stderr. read_to_string ( & mut s) . is_ok ( )
308
+ {
309
+ eprintln ! ( "{s}" ) ;
310
+ }
311
+ eprintln ! ( "Formatting check failed!\n Run `cargo dev fmt` to update." ) ;
312
+ process:: exit ( 1 ) ;
313
+ } ,
314
+ ( UpdateMode :: Change , e) => {
315
+ let mut s = String :: new ( ) ;
316
+ if let Some ( mut stderr) = child. stderr . take ( )
317
+ && stderr. read_to_string ( & mut s) . is_ok ( )
318
+ {
319
+ eprintln ! ( "{s}" ) ;
320
+ }
321
+ expect_action ( e, ErrAction :: Run , "rustfmt" ) ;
358
322
} ,
359
- Err ( ref e) => panic_action ( e, ErrAction :: Run , name. as_ref ( ) ) ,
360
323
}
361
324
}
362
325
}
363
326
364
327
// the "main" function of cargo dev fmt
365
- pub fn run ( clippy : & ClippyInfo , update_mode : UpdateMode ) {
366
- if clippy. has_intellij_hook {
367
- eprintln ! (
368
- "error: a local rustc repo is enabled as path dependency via `cargo dev setup intellij`.\n \
369
- Not formatting because that would format the local repo as well!\n \
370
- Please revert the changes to `Cargo.toml`s with `cargo dev remove intellij`."
371
- ) ;
372
- return ;
373
- }
374
- run_rustfmt ( clippy, update_mode) ;
328
+ pub fn run ( update_mode : UpdateMode ) {
329
+ run_rustfmt ( update_mode) ;
375
330
fmt_syms ( update_mode) ;
376
331
if let Err ( e) = fmt_conf ( update_mode. is_check ( ) ) {
377
332
e. display ( ) ;
0 commit comments