@@ -262,11 +262,21 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, input: Input, options: RustdocOptions
262
262
Ok ( None ) => return ,
263
263
Err ( error) => {
264
264
eprintln ! ( "{error}" ) ;
265
+ // Since some files in the temporary folder are still owned and alive, we need
266
+ // to manually remove the folder.
267
+ let _ = std:: fs:: remove_dir_all ( temp_dir. path ( ) ) ;
265
268
std:: process:: exit ( 1 ) ;
266
269
}
267
270
} ;
268
271
269
- run_tests ( opts, & rustdoc_options, & unused_extern_reports, standalone_tests, mergeable_tests) ;
272
+ run_tests (
273
+ opts,
274
+ & rustdoc_options,
275
+ & unused_extern_reports,
276
+ standalone_tests,
277
+ mergeable_tests,
278
+ Some ( temp_dir) ,
279
+ ) ;
270
280
271
281
let compiling_test_count = compiling_test_count. load ( Ordering :: SeqCst ) ;
272
282
@@ -316,6 +326,8 @@ pub(crate) fn run_tests(
316
326
unused_extern_reports : & Arc < Mutex < Vec < UnusedExterns > > > ,
317
327
mut standalone_tests : Vec < test:: TestDescAndFn > ,
318
328
mergeable_tests : FxIndexMap < Edition , Vec < ( DocTestBuilder , ScrapedDocTest ) > > ,
329
+ // We pass this argument so we can drop it manually before using `exit`.
330
+ temp_dir : Option < TempDir > ,
319
331
) {
320
332
let mut test_args = Vec :: with_capacity ( rustdoc_options. test_args . len ( ) + 1 ) ;
321
333
test_args. insert ( 0 , "rustdoctest" . to_string ( ) ) ;
@@ -382,9 +394,16 @@ pub(crate) fn run_tests(
382
394
// `running 0 tests...`.
383
395
if ran_edition_tests == 0 || !standalone_tests. is_empty ( ) {
384
396
standalone_tests. sort_by ( |a, b| a. desc . name . as_slice ( ) . cmp ( b. desc . name . as_slice ( ) ) ) ;
385
- test:: test_main ( & test_args, standalone_tests, None ) ;
397
+ let temp_dir_path = temp_dir. as_ref ( ) . map ( |t| t. path ( ) ) ;
398
+ test:: test_main_with_exit_callback ( & test_args, standalone_tests, None , || {
399
+ // We cannot move `temp_dir` in this closure so instead we do its job manually.
400
+ if let Some ( temp_dir_path) = temp_dir_path {
401
+ let _ = std:: fs:: remove_dir_all ( temp_dir_path) ;
402
+ }
403
+ } ) ;
386
404
}
387
405
if nb_errors != 0 {
406
+ std:: mem:: drop ( temp_dir) ;
388
407
// libtest::ERROR_EXIT_CODE is not public but it's the same value.
389
408
std:: process:: exit ( 101 ) ;
390
409
}
@@ -450,7 +469,7 @@ enum TestFailure {
450
469
}
451
470
452
471
enum DirState {
453
- Temp ( tempfile :: TempDir ) ,
472
+ Temp ( TempDir ) ,
454
473
Perm ( PathBuf ) ,
455
474
}
456
475
0 commit comments