Skip to content

Correct handling of explicit -Empty:$false parameter value in New-Guid#26140

Merged
iSazonov merged 1 commit intoPowerShell:masterfrom
logiclrd:new-guid-empty-explicit-false
Oct 6, 2025
Merged

Correct handling of explicit -Empty:$false parameter value in New-Guid#26140
iSazonov merged 1 commit intoPowerShell:masterfrom
logiclrd:new-guid-empty-explicit-false

Conversation

@logiclrd
Copy link
Contributor

@logiclrd logiclrd commented Oct 3, 2025

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-Guid cmdlet to fix the bug, making the test pass.

PR Context

Fixes #25276
Related to #25242

PR Checklist

…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.
@logiclrd logiclrd changed the title Correct handling explicit -Empty:$false parameter value in New-Guid Correct handling of explicit -Empty:$false parameter value in New-Guid Oct 3, 2025
@logiclrd
Copy link
Contributor Author

logiclrd commented Oct 3, 2025

@microsoft-github-policy-service agree

@iSazonov
Copy link
Collaborator

iSazonov commented Oct 3, 2025

/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging

@azure-pipelines
Copy link

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Oct 3, 2025
else
{
guid = ParameterSetName is "Empty" ? Guid.Empty : Guid.NewGuid();
guid = Empty.ToBool() ? Guid.Empty : Guid.NewGuid();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
guid = Empty.ToBool() ? Guid.Empty : Guid.NewGuid();
guid = Empty.IsPresent ? Guid.Empty : Guid.NewGuid();

Copy link
Contributor

Choose a reason for hiding this comment

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

this seems to be the more common pattern

Copy link
Contributor Author

@logiclrd logiclrd Oct 5, 2025

Choose a reason for hiding this comment

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

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.)

Copy link
Contributor

Choose a reason for hiding this comment

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

The implementation is identical:

public bool ToBool()
{
return _isPresent;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I agree we shouldn't use IsPresent. It may be implemented identically, but this appears to be a design error.

Copy link
Contributor Author

@logiclrd logiclrd Oct 5, 2025

Choose a reason for hiding this comment

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

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

no places that use the implicit conversion

I find 379 references to the implicit operator, for example:

public SwitchParameter Paging
{
get { return _paging; }
set { _paging = value; }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's really weird. To test, I deleted the operator from the source code and ran Start-PSBuild, and it built with no errors. 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't see errors at first. But I ran Start-PSBuild -Clean and now get error CS0029.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, fair enough.

@logiclrd logiclrd mentioned this pull request Oct 5, 2025
12 tasks
@iSazonov iSazonov self-assigned this Oct 6, 2025
@iSazonov iSazonov merged commit b5279a2 into PowerShell:master Oct 6, 2025
36 of 39 checks passed
@microsoft-github-policy-service
Copy link
Contributor

microsoft-github-policy-service bot commented Oct 6, 2025

📣 Hey @@logiclrd, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@iSazonov iSazonov added Hacktoberfest Potential candidate to participate in Hacktoberfest Hacktoberfest-Accepted Accepted to participate in Hacktoberfest labels Oct 6, 2025
SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Hacktoberfest Potential candidate to participate in Hacktoberfest Hacktoberfest-Accepted Accepted to participate in Hacktoberfest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New-Guid -Empty ignores an explicit $false value

3 participants