Restore ReadOnly after failed Set-Variable option updates - #27877
Open
David Kontyko (dkontyko) wants to merge 8 commits into
Open
Restore ReadOnly after failed Set-Variable option updates#27877David Kontyko (dkontyko) wants to merge 8 commits into
David Kontyko (dkontyko) wants to merge 8 commits into
Conversation
…tate when option assignment fails
Co-authored-by: dkontyko <33853225+dkontyko@users.noreply.github.com>
Co-authored-by: dkontyko <33853225+dkontyko@users.noreply.github.com>
Co-authored-by: dkontyko <33853225+dkontyko@users.noreply.github.com>
Co-authored-by: dkontyko <33853225+dkontyko@users.noreply.github.com>
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes a Set-Variable -Force edge case where ReadOnly was temporarily removed to perform an update, but not restored when a subsequent option update (notably -Option Constant) failed. It also adds regression/compatibility tests to lock in the intended behavior for issue #27679 cases 1–3.
Changes:
- Restore the
ReadOnlyflag in the existing handled failure paths whenSet-Variable -Forcetemporarily cleared it and the option update fails. - Add Pester coverage for restoring
ReadOnlyacrossReadOnly,ReadOnly | Private, andReadOnly | AllScope, and document the intentionally retained mutation behavior for issue #27679 cases 1 and 2.
Show a summary per file
| File | Description |
|---|---|
src/Microsoft.PowerShell.Commands.Utility/commands/utility/Var.cs |
Tracks whether ReadOnly was temporarily cleared and restores it in the handled exception paths when option updates fail. |
test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Variable.Tests.ps1 |
Adds regression/compatibility tests covering the issue #27679 scenarios and option combinations. |
Review details
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15243fcb-972e-4518-b3b2-e0c5021b4199
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 3068dc60-7ac0-4d2b-bcc3-020ac4816d67
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
Restore the
ReadOnlyoption whenSet-Variable -Forcetemporarily removes it and a subsequent option update fails.Add regression coverage for
ReadOnly,ReadOnly | Private, andReadOnly | AllScope, plus compatibility tests documenting the intentionally retained behavior for cases 1 and 2 of #27679.PR Context
When
Set-Variable -Forceoperates on a ReadOnly variable, it temporarily removesReadOnlybefore applying the requested changes. If applying-Option Constantfails withVariableCannotBeMadeConstant, the temporary removal was not reverted, leaving the variable writable.This change restores
ReadOnlyin the existing handled exception paths while preserving other current option flags. It addresses case 3 of #27679, as agreed by the PowerShell cmdlets working group.For backward compatibility, cases 1 and 2 continue to retain value and description changes made before the option update fails. Tests document this behavior explicitly.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header