{{ message }}
Repository navigation
Fix Remove-Item -WhatIf removing directory symbolic links and junctions - #28228
Open
om singhal (Om-singhaI) wants to merge 1 commit into
Open
om singhal (Om-singhaI) wants to merge 1 commit into
om singhal (Om-singhaI) wants to merge 1 commit into
Conversation
`RemoveDirectoryInfoItem` asks `ShouldProcess` for the directory, but the branch that detaches symbolic links and junctions ran before the answer was checked. So `-WhatIf`, or answering No to `-Confirm`, still removed the link. Only take that branch when `ShouldProcess` approved the removal, and add tests for a directory symbolic link and a junction. Fix PowerShell#16664
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The narrow guard addresses the deletion bug, with regression coverage for both affected link types and no unresolved findings.
No open issues 🎉
What changed in this PR
Fixes #16664 by making directory symbolic-link and junction removal honor ShouldProcess.
Changes:
- Prevents link deletion with
-WhatIfor a declined-Confirm. - Adds regression tests for directory symbolic links and Windows junctions, including recursive, forced previews.
Review effort: Balanced
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.

PR Summary
Remove-Item -WhatIfno longer deletes directory symbolic links and junctions, and answering No to-Confirmnow keeps them as well.Fix #16664
RemoveDirectoryInfoItemstores theShouldProcessanswer incontinueRemoval, but the branch that detaches a symbolic link or junction ran before that value was checked, calleddirectory.Delete()and returned. The fix only takes that branch whencontinueRemovalis true. File symbolic links go throughRemoveFileInfoItem, which already checksShouldProcess, so they were not affected.I added two tests to the
Remove-Item and hard/symbolic linkscontext inFileSystem.Tests.ps1: one for a symbolic link to a directory on all platforms and one for a junction on Windows. Each runsRemove-Item -WhatIfandRemove-Item -Recurse -Force -WhatIf, then checks that the link and the target are still there.PR Context
People use
-WhatIfto preview a removal, so losing the link here is real data loss. The issue has been open since 2021, was reproduced again on 7.6.4, and #19714 was closed as a duplicate of it.I tested on macOS arm64 with a local Debug build of master at fbb4fd5. Without the fix the new symbolic link test fails because the link is gone after
-WhatIf. With the fix it passes, and the rest ofFileSystem.Tests.ps1andRemove-Item.Tests.ps1pass on macOS with the CI and Feature tags, apart from one test that needs sudo, which I did not run. I also pipedNinto the-Confirmprompt and the link now stays. I could not run the junction test myself since I do not have a Windows machine.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header