Skip to content

Commit 0b8c12f

Browse files
committed
Document reasoning and report previous error chain
1 parent ddc4978 commit 0b8c12f

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

crates/cargo-util/src/paths.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,21 @@ fn _create_dir_all(p: &Path) -> Result<()> {
413413
Ok(())
414414
}
415415

416-
/// Recursively remove all files and directories at the given directory.
416+
/// Equivalent to [`std::fs::remove_dir_all`] with better error messages.
417417
///
418418
/// This does *not* follow symlinks.
419419
pub fn remove_dir_all<P: AsRef<Path>>(p: P) -> Result<()> {
420-
_remove_dir_all(p.as_ref()).or_else(|_| {
421-
fs::remove_dir_all(p.as_ref())
422-
.with_context(|| format!("failed to remove directory `{}`", p.as_ref().display()))
420+
_remove_dir_all(p.as_ref()).or_else(|prev_err| {
421+
// `std::fs::remove_dir_all` is highly specialized for different platforms
422+
// and may be more reliable than a simple walk. We try the walk first in
423+
// order to report more detailed errors.
424+
fs::remove_dir_all(p.as_ref()).with_context(|| {
425+
format!(
426+
"failed to remove directory `{}` \n\n---\nPrevious error: {:?}\n---",
427+
p.as_ref().display(),
428+
prev_err
429+
)
430+
})
423431
})
424432
}
425433

src/cargo/ops/cargo_clean.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ fn rm_rf(path: &Path, config: &Config, progress: &mut dyn CleaningProgressBar) -
297297
let entry = entry?;
298298
progress.on_clean()?;
299299
if entry.file_type().is_dir() {
300+
// `remove_dir_all` is used here due to https://github.com/rust-lang/cargo/issues/11441
300301
paths::remove_dir_all(entry.path())
301302
.with_context(|| "could not remove build directory")?;
302303
} else {

0 commit comments

Comments
 (0)