@@ -12,8 +12,8 @@ use std::sync::mpsc::SyncSender;
12
12
13
13
fn rustfmt ( src : & Path , rustfmt : & Path , paths : & [ PathBuf ] , check : bool ) -> impl FnMut ( bool ) -> bool {
14
14
let mut cmd = Command :: new ( rustfmt) ;
15
- // avoid the submodule config paths from coming into play,
16
- // we only allow a single global config for the workspace for now
15
+ // Avoid the submodule config paths from coming into play. We only allow a single global config
16
+ // for the workspace for now.
17
17
cmd. arg ( "--config-path" ) . arg ( & src. canonicalize ( ) . unwrap ( ) ) ;
18
18
cmd. arg ( "--edition" ) . arg ( "2021" ) ;
19
19
cmd. arg ( "--unstable-features" ) ;
@@ -24,7 +24,7 @@ fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl F
24
24
cmd. args ( paths) ;
25
25
let cmd_debug = format ! ( "{cmd:?}" ) ;
26
26
let mut cmd = cmd. spawn ( ) . expect ( "running rustfmt" ) ;
27
- // poor man's async: return a closure that'll wait for rustfmt's completion
27
+ // Poor man's async: return a closure that'll wait for rustfmt's completion.
28
28
move |block : bool | -> bool {
29
29
if !block {
30
30
match cmd. try_wait ( ) {
@@ -72,7 +72,7 @@ fn verify_rustfmt_version(build: &Builder<'_>) -> bool {
72
72
!program_out_of_date ( & stamp_file, & version)
73
73
}
74
74
75
- /// Updates the last rustfmt version used
75
+ /// Updates the last rustfmt version used.
76
76
fn update_rustfmt_version ( build : & Builder < ' _ > ) {
77
77
let Some ( ( version, stamp_file) ) = get_rustfmt_version ( build) else {
78
78
return ;
@@ -115,8 +115,15 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
115
115
let rustfmt_config: RustfmtConfig = t ! ( toml:: from_str( & rustfmt_config) ) ;
116
116
let mut fmt_override = ignore:: overrides:: OverrideBuilder :: new ( & build. src ) ;
117
117
for ignore in rustfmt_config. ignore {
118
- if let Some ( ignore) = ignore. strip_prefix ( '!' ) {
119
- fmt_override. add ( ignore) . expect ( ignore) ;
118
+ if ignore. starts_with ( '!' ) {
119
+ // A `!`-prefixed entry could be added as a whitelisted entry in `fmt_override`, i.e.
120
+ // strip the `!` prefix. But as soon as whitelisted entries are added, an
121
+ // `OverrideBuilder` will only traverse those whitelisted entries, and won't traverse
122
+ // any files that aren't explicitly mentioned. No bueno! Maybe there's a way to combine
123
+ // explicit whitelisted entries and traversal of unmentioned files, but for now just
124
+ // forbid such entries.
125
+ eprintln ! ( "`!`-prefixed entries are not supported in rustfmt.toml, sorry" ) ;
126
+ crate :: exit!( 1 ) ;
120
127
} else {
121
128
fmt_override. add ( & format ! ( "!{ignore}" ) ) . expect ( & ignore) ;
122
129
}
@@ -168,9 +175,10 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
168
175
untracked_count += 1 ;
169
176
fmt_override. add ( & format ! ( "!/{untracked_path}" ) ) . expect ( untracked_path) ;
170
177
}
171
- // Only check modified files locally to speed up runtime.
172
- // We still check all files in CI to avoid bugs in `get_modified_rs_files` letting regressions slip through;
173
- // we also care about CI time less since this is still very fast compared to building the compiler.
178
+ // Only check modified files locally to speed up runtime. We still check all files in
179
+ // CI to avoid bugs in `get_modified_rs_files` letting regressions slip through; we
180
+ // also care about CI time less since this is still very fast compared to building the
181
+ // compiler.
174
182
if !CiEnv :: is_ci ( ) && paths. is_empty ( ) {
175
183
match get_modified_rs_files ( build) {
176
184
Ok ( Some ( files) ) => {
@@ -275,21 +283,23 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
275
283
. overrides ( fmt_override)
276
284
. build_parallel ( ) ;
277
285
278
- // there is a lot of blocking involved in spawning a child process and reading files to format.
279
- // spawn more processes than available concurrency to keep the CPU busy
286
+ // There is a lot of blocking involved in spawning a child process and reading files to format.
287
+ // Spawn more processes than available concurrency to keep the CPU busy.
280
288
let max_processes = build. jobs ( ) as usize * 2 ;
281
289
282
- // spawn child processes on a separate thread so we can batch entries we have received from ignore
290
+ // Spawn child processes on a separate thread so we can batch entries we have received from
291
+ // ignore.
283
292
let thread = std:: thread:: spawn ( move || {
284
293
let mut children = VecDeque :: new ( ) ;
285
294
while let Ok ( path) = rx. recv ( ) {
286
- // try getting more paths from the channel to amortize the overhead of spawning processes
295
+ // Try getting more paths from the channel to amortize the overhead of spawning
296
+ // processes.
287
297
let paths: Vec < _ > = rx. try_iter ( ) . take ( 63 ) . chain ( std:: iter:: once ( path) ) . collect ( ) ;
288
298
289
299
let child = rustfmt ( & src, & rustfmt_path, paths. as_slice ( ) , check) ;
290
300
children. push_back ( child) ;
291
301
292
- // poll completion before waiting
302
+ // Poll completion before waiting.
293
303
for i in ( 0 ..children. len ( ) ) . rev ( ) {
294
304
if children[ i] ( false ) {
295
305
children. swap_remove_back ( i) ;
@@ -298,12 +308,12 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
298
308
}
299
309
300
310
if children. len ( ) >= max_processes {
301
- // await oldest child
311
+ // Await oldest child.
302
312
children. pop_front ( ) . unwrap ( ) ( true ) ;
303
313
}
304
314
}
305
315
306
- // await remaining children
316
+ // Await remaining children.
307
317
for mut child in children {
308
318
child ( true ) ;
309
319
}
0 commit comments