You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: respect AllowedFileShare in recursive Directory.Delete - #1607
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.
⚠️ 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).
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
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.
Fixes #747
Directory.Delete(path, true)deleted files whoseAllowedFileSharedoesn't includeFileShare.Delete, whileFile.Deleteon the same file throwsIOException.DirectoryInfo.Delete(true)calls the same method, so it had the same gap.What was wrong
MockFile.DeletechecksAllowedFileShare.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.Deletenow goes through the affected files. If one of them doesn't allowFileShare.Delete, it throws the same exception asFile.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, sinceAllowedFileShareis 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.Deletedoes, 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 toMockFile_Lock_FileShareNoneThrowsDelete:Directory.Delete(dir, true)on a folder with a locked file and an unlocked one throws theIOExceptionwith the locked file's path, and both files are still thereDirectoryInfo.Delete(true)with a locked file two folders down throws the same wayFileShare.Deleteis still deleted along with its folderThe first two fail on
mainand 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.100container, 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.Pathlike the tests around them, so they also run on Windows.