Skip to content

Commit

Permalink
Auto merge of #11442 - trevyn:issue-11441, r=weihanglo
Browse files Browse the repository at this point in the history
cargo clean: use `remove_dir_all`

Fixes #11441
  • Loading branch information
bors committed May 31, 2023
2 parents 9b85e11 + ac3ccdd commit 58b22f0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
15 changes: 13 additions & 2 deletions crates/cargo-util/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,22 @@ fn _create_dir_all(p: &Path) -> Result<()> {
Ok(())
}

/// Recursively remove all files and directories at the given directory.
/// Equivalent to [`std::fs::remove_dir_all`] with better error messages.
///
/// This does *not* follow symlinks.
pub fn remove_dir_all<P: AsRef<Path>>(p: P) -> Result<()> {
_remove_dir_all(p.as_ref())
_remove_dir_all(p.as_ref()).or_else(|prev_err| {
// `std::fs::remove_dir_all` is highly specialized for different platforms
// and may be more reliable than a simple walk. We try the walk first in
// order to report more detailed errors.
fs::remove_dir_all(p.as_ref()).with_context(|| {
format!(
"{:?}\n\nError: failed to remove directory `{}`",
prev_err,
p.as_ref().display(),
)
})
})
}

fn _remove_dir_all(p: &Path) -> Result<()> {
Expand Down
7 changes: 6 additions & 1 deletion src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,12 @@ fn rm_rf(path: &Path, config: &Config, progress: &mut dyn CleaningProgressBar) -
let entry = entry?;
progress.on_clean()?;
if entry.file_type().is_dir() {
paths::remove_dir(entry.path()).with_context(|| "could not remove build directory")?;
// The contents should have been removed by now, but sometimes a race condition is hit
// where other files have been added by the OS. `paths::remove_dir_all` also falls back
// to `std::fs::remove_dir_all`, which may be more reliable than a simple walk in
// platform-specific edge cases.
paths::remove_dir_all(entry.path())
.with_context(|| "could not remove build directory")?;
} else {
paths::remove_file(entry.path()).with_context(|| "failed to remove build artifact")?;
}
Expand Down

0 comments on commit 58b22f0

Please sign in to comment.