Correct handling of explicit -Empty:$false parameter value in New-Guid#26140
Conversation
…ng the bug described in issue PowerShell#25276. Updated the implementation in NewGuidCommand.cs to correct the handling of explicit $false values passed to -Empty, making the test pass.
|
@microsoft-github-policy-service agree |
|
/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging |
|
Azure Pipelines could not run because the pipeline triggers exclude this branch/path. |
src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewGuidCommand.cs
Show resolved
Hide resolved
| else | ||
| { | ||
| guid = ParameterSetName is "Empty" ? Guid.Empty : Guid.NewGuid(); | ||
| guid = Empty.ToBool() ? Guid.Empty : Guid.NewGuid(); |
There was a problem hiding this comment.
| guid = Empty.ToBool() ? Guid.Empty : Guid.NewGuid(); | |
| guid = Empty.IsPresent ? Guid.Empty : Guid.NewGuid(); |
There was a problem hiding this comment.
this seems to be the more common pattern
There was a problem hiding this comment.
The use of .ToBool() instead of simply checking for presence is the entire point of this PR, actually. The caller could pass in -Empty:$false, in which case it would be present, but the caller's intent is to not get the empty GUID. (By checking which ParameterSet is in effect, the previous code was in effect also just checking for the presence of the switch.)
There was a problem hiding this comment.
The implementation is identical:
PowerShell/src/System.Management.Automation/engine/MshCmdlet.cs
Lines 110 to 113 in be0542b
There was a problem hiding this comment.
Actually, I agree we shouldn't use IsPresent. It may be implemented identically, but this appears to be a design error.
There was a problem hiding this comment.
If it is accepted, then this PR can be rebased to use .IsSpecified. If it is not accepted, then I posit that it is essentially not important whether this PR uses .IsPresent or .ToBool() -- but that .IsPresent makes less sense since the bug being fixed in this instance is precisely related to the fact that a switch might be present with the explicit value $false.
There was a problem hiding this comment.
no places that use the implicit conversion
I find 379 references to the implicit operator, for example:
There was a problem hiding this comment.
That's really weird. To test, I deleted the operator from the source code and ran Start-PSBuild, and it built with no errors. 🤔
There was a problem hiding this comment.
I didn't see errors at first. But I ran Start-PSBuild -Clean and now get error CS0029.
|
📣 Hey @@logiclrd, how did we do? We would love to hear your feedback with the link below! 🗣️ 🔗 https://aka.ms/PSRepoFeedback |
Fix New-Guid's handling of -Empty to check the switch's value, rather than only its presence, allowing parameter values to be passed through easily.
PR Summary
This PR adds a test to reproduce the bug described in issue #25276, and then updates the implementation of the
New-Guidcmdlet to fix the bug, making the test pass.PR Context
Fixes #25276
Related to #25242
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header