Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use remove_dir_all in cargo clean to resolve race condition #11442

Merged
merged 5 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions crates/cargo-util/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,21 @@ 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()).or_else(|_| {
fs::remove_dir_all(p.as_ref())
.with_context(|| format!("failed to remove directory `{}`", p.as_ref().display()))
_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!(
"failed to remove directory `{}` \n\n---\nPrevious error: {:?}\n---",
p.as_ref().display(),
prev_err
)
})
})
}

Expand Down
1 change: 1 addition & 0 deletions src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ fn rm_rf(path: &Path, config: &Config, progress: &mut dyn CleaningProgressBar) -
let entry = entry?;
progress.on_clean()?;
if entry.file_type().is_dir() {
// `remove_dir_all` is used here due to https://github.com/rust-lang/cargo/issues/11441
trevyn marked this conversation as resolved.
Show resolved Hide resolved
paths::remove_dir_all(entry.path())
.with_context(|| "could not remove build directory")?;
} else {
Expand Down