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 , panic_action, run_with_output , split_args_for_threads ,
3
3
} ;
4
4
use itertools:: Itertools ;
5
5
use rustc_lexer:: { TokenKind , tokenize} ;
@@ -260,50 +260,37 @@ 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 < _ > = WalkDir :: new ( "." )
272
+ . into_iter ( )
273
+ . filter_entry ( |e|
274
+ // Skip traversing some of the larger directories.
275
+ e. path ( )
276
+ . as_os_str ( )
277
+ . as_encoded_bytes ( )
278
+ . get ( 2 ..)
279
+ . is_none_or ( |x| x != "target" . as_bytes ( ) && x != ".git" . as_bytes ( ) )
280
+ && e. path ( ) . file_name ( ) . is_none_or ( |x| x != "skip_rustfmt" ) )
281
+ // e.path().file_name().is_none_or(|x| x != "skip_rustfmt"))
282
+ . filter_map ( |e| {
283
+ let e = e. expect ( "error reading `tests`" ) ;
284
+ e. path ( )
285
+ . as_os_str ( )
286
+ . as_encoded_bytes ( )
287
+ . ends_with ( b".rs" )
288
+ . then ( || e. into_path ( ) . into_os_string ( ) )
289
+ } )
290
+ . collect ( ) ;
305
291
306
- run_with_args_split (
292
+ let mut children: Vec < _ > = split_args_for_threads (
293
+ 32 ,
307
294
|| {
308
295
let mut cmd = Command :: new ( & rustfmt_path) ;
309
296
if update_mode. is_check ( ) {
@@ -312,27 +299,23 @@ fn run_rustfmt(clippy: &ClippyInfo, update_mode: UpdateMode) {
312
299
cmd. stdout ( Stdio :: null ( ) )
313
300
. stdin ( Stdio :: null ( ) )
314
301
. stderr ( Stdio :: piped ( ) )
315
- . args ( [ "--config" , "show_parse_errors=false" ] ) ;
302
+ . args ( [
303
+ "--config" ,
304
+ "show_parse_errors=false" ,
305
+ "--unstable-features" ,
306
+ "--skip-children" ,
307
+ ] ) ;
316
308
cmd
317
309
} ,
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
- ) ;
310
+ args. iter ( ) ,
311
+ )
312
+ . map ( |mut cmd| match cmd. spawn ( ) {
313
+ Ok ( x) => x,
314
+ Err ( ref e) => panic_action ( & e, ErrAction :: Run , "rustfmt" . as_ref ( ) ) ,
315
+ } )
316
+ . collect ( ) ;
334
317
335
- for ( name , child) in & mut children {
318
+ for child in & mut children {
336
319
match child. wait ( ) {
337
320
Ok ( status) => match ( update_mode, status. exit_ok ( ) ) {
338
321
( UpdateMode :: Check | UpdateMode :: Change , Ok ( ( ) ) ) => { } ,
@@ -353,25 +336,17 @@ fn run_rustfmt(clippy: &ClippyInfo, update_mode: UpdateMode) {
353
336
{
354
337
eprintln ! ( "{s}" ) ;
355
338
}
356
- panic_action ( & e, ErrAction :: Run , name . as_ref ( ) ) ;
339
+ panic_action ( & e, ErrAction :: Run , "rustfmt" . as_ref ( ) ) ;
357
340
} ,
358
341
} ,
359
- Err ( ref e) => panic_action ( e, ErrAction :: Run , name . as_ref ( ) ) ,
342
+ Err ( ref e) => panic_action ( e, ErrAction :: Run , "rustfmt" . as_ref ( ) ) ,
360
343
}
361
344
}
362
345
}
363
346
364
347
// 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) ;
348
+ pub fn run ( update_mode : UpdateMode ) {
349
+ run_rustfmt ( update_mode) ;
375
350
fmt_syms ( update_mode) ;
376
351
if let Err ( e) = fmt_conf ( update_mode. is_check ( ) ) {
377
352
e. display ( ) ;
0 commit comments