Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected override void ProcessRecord()
}
else
{
guid = ParameterSetName is "Empty" ? Guid.Empty : Guid.NewGuid();
guid = Empty.ToBool() ? Guid.Empty : Guid.NewGuid();
Comment thread
logiclrd marked this conversation as resolved.
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

}

WriteObject(guid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Describe "New-Guid" -Tags "CI" {
$guid.ToString() | Should -BeExactly "00000000-0000-0000-0000-000000000000"
}

It "Should respect explicit false value for -Empty" {
$guid = New-Guid -Empty:$false
$guid.ToString() | Should -Not -BeExactly "00000000-0000-0000-0000-000000000000"
}

It "Should convert a string to a guid" {
$guid1 = New-Guid
$guid2 = New-Guid -InputObject $guid1.ToString()
Expand Down
Loading