@@ -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
+ mut 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,14 @@ 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
+ test:: test_main_with_exit_callback ( & test_args, standalone_tests, None , || {
398
+ // We ensure temp dir destructor is called.
399
+ std:: mem:: drop ( temp_dir. take ( ) ) ;
400
+ } ) ;
386
401
}
387
402
if nb_errors != 0 {
403
+ // We ensure temp dir destructor is called.
404
+ std:: mem:: drop ( temp_dir) ;
388
405
// libtest::ERROR_EXIT_CODE is not public but it's the same value.
389
406
std:: process:: exit ( 101 ) ;
390
407
}
@@ -450,7 +467,7 @@ enum TestFailure {
450
467
}
451
468
452
469
enum DirState {
453
- Temp ( tempfile :: TempDir ) ,
470
+ Temp ( TempDir ) ,
454
471
Perm ( PathBuf ) ,
455
472
}
456
473
0 commit comments