Skip to content

fix(rm): retry rm -r when a directory is left not empty - #19637

Merged
fdncred merged 1 commit into
nushell:mainfrom
fdncred:rm_dir_not_empty
Nov 23, 2026
Merged

fdncred merged 1 commit into
nushell:mainfrom
fdncred:rm_dir_not_empty

Conversation

@fdncred

@fdncred fdncred commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Description

On macOS, rm -rf node_modules can fail with Directory not empty (os error 66) and leave part of the tree behind, while a second rm -rf removes the rest (#19599).

rm -r hands each directory to std::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 argument rm was given, not the directory that was still not empty. ^rm -rf removes the same tree on the first run, and zsh's builtin rm reads every name in a directory before deleting any of them.

rm now calls remove_dir_all again, 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_all keeps its no-follow, fd-relative traversal, so the retry doesn't reopen the symlink race.

User-facing changes (Release notes)

Fixed rm -r on macOS sometimes stopping partway through a large directory with "Directory not empty". rm now retries the removal before it reports that error.

Additional notes

closes #19599

  • I couldn't reproduce the failure locally, so there is no before/after output. The reporter sees it only in their original project, never in a fresh install from the same lockfile. The unit tests pass a fake removal function to the retry loop and check it retries on "Directory not empty", gives up after the last try, returns other errors without retrying, and stops on Ctrl-C before a retry.
  • If the tree is already gone on a retry, that counts as success, because an earlier call removed part of it.
  • The retry isn't gated to macOS. Elsewhere it only runs after a removal that would have failed anyway.

`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.
@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Nov 23, 2026
@fdncred
fdncred merged commit 43661e3 into nushell:main Nov 23, 2026
15 checks passed
@fdncred
fdncred deleted the rm_dir_not_empty branch November 23, 2026 12:28
@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rm -rf on a large node_modules directory fails on the first attempt with os error 66 (Directory not empty) and succeeds on the second attempt on macOS

1 participant