Skip to content

fix: respect AllowedFileShare in recursive Directory.Delete - #1607

Merged
mergify[bot] merged 2 commits into
TestableIO:mainfrom
mohsinaliwork686-rgb:fix/directory-delete-locked-file
Nov 28, 2026
Merged

mergify[bot] merged 2 commits into
TestableIO:mainfrom
mohsinaliwork686-rgb:fix/directory-delete-locked-file

Conversation

@mohsinaliwork686-rgb

Copy link
Copy Markdown
Contributor

Fixes #747

Directory.Delete(path, true) deleted files whose AllowedFileShare doesn't include FileShare.Delete, while File.Delete on the same file throws IOException. DirectoryInfo.Delete(true) calls the same method, so it had the same gap.

What was wrong

MockFile.Delete checks AllowedFileShare.HasFlag(FileShare.Delete) before it removes a file. MockDirectory.Delete(string, bool) collects every path under the directory and removes them one by one, without that check.

What changed

Before it removes anything, MockDirectory.Delete now goes through the affected files. If one of them doesn't allow FileShare.Delete, it throws the same exception as File.Delete (CommonExceptions.ProcessCannotAccessFileInUse, "The process cannot access the file '...' because it is being used by another process.") with that file's full path. Only files are checked, not the directories in the tree, since AllowedFileShare is documented as the file's sharing mode.

Because the check runs first, nothing is deleted when it throws. Real Windows is different here: it deletes every file it can, removes the folders that end up empty, and then throws for the first file it couldn't delete. I kept the all-or-nothing version I proposed on the issue. It leaves the file system as it was when the call throws, like File.Delete does, and copying Windows exactly would mean working out which folders still hold a locked file and keeping only those. Happy to change it if you'd rather match Windows.

How it's tested

I added three tests to MockFileLockTests, next to MockFile_Lock_FileShareNoneThrowsDelete:

  • Directory.Delete(dir, true) on a folder with a locked file and an unlocked one throws the IOException with the locked file's path, and both files are still there
  • DirectoryInfo.Delete(true) with a locked file two folders down throws the same way
  • a file with FileShare.Delete is still deleted along with its folder

The first two fail on main and pass with the fix. The third passes on both and makes sure the new check doesn't block files that allow deletion.

I ran this on Linux in the mcr.microsoft.com/dotnet/sdk:11.0.100 container, with the .NET 10, 9 and 8 SDKs added. The Release build of the solution has no warnings or errors. The Wrappers, TestingHelpers and Parity tests all pass on net11.0, net10.0, net9.0 and net8.0, and the API tests pass too. On macOS, the TestingHelpers, Wrappers and Parity tests also pass on net10.0, net9.0 and net8.0 (there is no .NET 11 SDK on that machine).

I couldn't run the Windows tests or net472. The new tests build their paths with XFS.Path like the tests around them, so they also run on Windows.

@mergify

mergify Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@vbreuss vbreuss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mergify

mergify Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

queue

⚠️ Configuration not compatible with required_status_checks ruleset rule

Details

The ruleset main requires branches to be up to date before merging (strict_required_status_checks_policy). The merge queue tests pull requests in a draft PR built on the latest base branch, then merges the original pull requests. Their own branches were not updated with that base branch, so GitHub considers them out of date and refuses to merge them.

To fix it, turn off Require branches to be up to date before merging in the ruleset main. It is safe to turn off: the merge queue already tests every pull request it merges on the latest base branch, together with the ones queued ahead of it. That is what this setting checks. Changing the setting needs admin access.

If you need to keep the setting:

  • Add Mergify as a bypass actor on the ruleset main, with exempt mode: the rule stays enforced for everyone else.
  • Use merge_method: fast-forward: the queue pushes the tested draft PR's branch to the base branch itself, which the setting does not refuse.
  • Use in-place checks: Mergify tests each pull request on its own branch, one at a time, which gives up parallel checks and batches. To do so, set merge_queue.max_parallel_checks: 1 and merge_queue.mode: serial, set every queue rule batch_size: 1 and max_checks_retries: 0, use a merge_method other than merge-batch, and avoid two-step CI (make merge_conditions identical to queue_conditions).

Learn more in the documentation.

@vbreuss
vbreuss force-pushed the fix/directory-delete-locked-file branch from 6086cca to effc70a Compare November 28, 2026 04:42
@mergify
mergify Bot merged commit dcd0445 into TestableIO:main Nov 28, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MockDirectory.Delete recursive does not consider nested files MockFileData.AllowedFileShare property

2 participants