11use 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,
34} ;
45use itertools:: Itertools ;
56use rustc_lexer:: { TokenKind , tokenize} ;
@@ -9,7 +10,6 @@ use std::io::{self, Read};
910use std:: ops:: ControlFlow ;
1011use std:: path:: PathBuf ;
1112use std:: process:: { self , Command , Stdio } ;
12- use walkdir:: WalkDir ;
1313
1414pub enum Error {
1515 Io ( io:: Error ) ,
@@ -260,50 +260,27 @@ fn fmt_syms(update_mode: UpdateMode) {
260260 ) ;
261261}
262262
263- fn run_rustfmt ( clippy : & ClippyInfo , update_mode : UpdateMode ) {
263+ fn run_rustfmt ( update_mode : UpdateMode ) {
264264 let mut rustfmt_path = String :: from_utf8 ( run_with_output (
265265 "rustup which rustfmt" ,
266266 Command :: new ( "rustup" ) . args ( [ "which" , "rustfmt" ] ) ,
267267 ) )
268268 . expect ( "invalid rustfmt path" ) ;
269269 rustfmt_path. truncate ( rustfmt_path. trim_end ( ) . len ( ) ) ;
270270
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 ( ) ;
305281
306- run_with_args_split (
282+ let mut children: Vec < _ > = split_args_for_threads (
283+ 32 ,
307284 || {
308285 let mut cmd = Command :: new ( & rustfmt_path) ;
309286 if update_mode. is_check ( ) {
@@ -312,66 +289,44 @@ fn run_rustfmt(clippy: &ClippyInfo, update_mode: UpdateMode) {
312289 cmd. stdout ( Stdio :: null ( ) )
313290 . stdin ( Stdio :: null ( ) )
314291 . stderr ( Stdio :: piped ( ) )
315- . args ( [ "--config " , "show_parse_errors=false " ] ) ;
292+ . args ( [ "--unstable-features " , "--skip-children " ] ) ;
316293 cmd
317294 } ,
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 ( ) ;
334299
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" ) ;
358322 } ,
359- Err ( ref e) => panic_action ( e, ErrAction :: Run , name. as_ref ( ) ) ,
360323 }
361324 }
362325}
363326
364327// 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) ;
375330 fmt_syms ( update_mode) ;
376331 if let Err ( e) = fmt_conf ( update_mode. is_check ( ) ) {
377332 e. display ( ) ;
0 commit comments