Skip to content

Commit 9959875

Browse files
Ensure that temporary doctest folder is correctly removed even if doctests failed
1 parent 14782ad commit 9959875

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/librustdoc/doctest.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,21 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, input: Input, options: RustdocOptions
262262
Ok(None) => return,
263263
Err(error) => {
264264
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());
265268
std::process::exit(1);
266269
}
267270
};
268271

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+
);
270280

271281
let compiling_test_count = compiling_test_count.load(Ordering::SeqCst);
272282

@@ -316,6 +326,8 @@ pub(crate) fn run_tests(
316326
unused_extern_reports: &Arc<Mutex<Vec<UnusedExterns>>>,
317327
mut standalone_tests: Vec<test::TestDescAndFn>,
318328
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>,
319331
) {
320332
let mut test_args = Vec::with_capacity(rustdoc_options.test_args.len() + 1);
321333
test_args.insert(0, "rustdoctest".to_string());
@@ -382,9 +394,16 @@ pub(crate) fn run_tests(
382394
// `running 0 tests...`.
383395
if ran_edition_tests == 0 || !standalone_tests.is_empty() {
384396
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+
});
386404
}
387405
if nb_errors != 0 {
406+
std::mem::drop(temp_dir);
388407
// libtest::ERROR_EXIT_CODE is not public but it's the same value.
389408
std::process::exit(101);
390409
}
@@ -450,7 +469,7 @@ enum TestFailure {
450469
}
451470

452471
enum DirState {
453-
Temp(tempfile::TempDir),
472+
Temp(TempDir),
454473
Perm(PathBuf),
455474
}
456475

src/librustdoc/doctest/markdown.rs

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ pub(crate) fn test(input: &Input, options: Options) -> Result<(), String> {
116116
&Arc::new(Mutex::new(Vec::new())),
117117
standalone_tests,
118118
mergeable_tests,
119+
None,
119120
);
120121
Ok(())
121122
}

0 commit comments

Comments
 (0)