Repository navigation
fix(rm): retry rm -r when a directory is left not empty - #19637
Merged
Merged
Conversation
`std::fs::remove_dir_all` deletes entries while it reads each directory and stops at the first error. On macOS one pass can end with "Directory not empty" and leave part of the tree, while a second call removes the rest (nushell#19599). `rm` now calls `remove_dir_all` again, up to 3 times in all, while it fails with DirectoryNotEmpty. Other errors are returned right away, and a tree that is gone on a retry counts as success. Ctrl-C is checked before each retry, since each one walks the whole tree again.
2 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
On macOS,
rm -rf node_modulescan fail withDirectory not empty (os error 66)and leave part of the tree behind, while a secondrm -rfremoves the rest (#19599).rm -rhands each directory tostd::fs::remove_dir_all. That function deletes each directory's entries while it reads them, and it stops at the first error. When one nested directory still has an entry at the end, the whole removal stops. In the report, 12,399 of 41,026 entries survived the first run. The path in the error is the argumentrmwas given, not the directory that was still not empty.^rm -rfremoves the same tree on the first run, and zsh's builtinrmreads every name in a directory before deleting any of them.rmnow callsremove_dir_allagain, up to 3 times in all, when it fails with "Directory not empty". Each call opens every directory again and continues from what is left. Other errors are returned right away. Ctrl-C is checked before each retry, so a long retry can be stopped.remove_dir_allkeeps its no-follow, fd-relative traversal, so the retry doesn't reopen the symlink race.User-facing changes (Release notes)
Fixed
rm -ron macOS sometimes stopping partway through a large directory with "Directory not empty".rmnow retries the removal before it reports that error.Additional notes
closes #19599