🪲 [Fix]: Reject values that are not versions in CompareTo and Equals - #55
Open
Marius Storhaug (MariusStorhaug) wants to merge 1 commit into
Open
🪲 [Fix]: Reject values that are not versions in CompareTo and Equals#55Marius Storhaug (MariusStorhaug) wants to merge 1 commit into
Marius Storhaug (MariusStorhaug) wants to merge 1 commit into
Conversation
Both guards were written as 'if (-not \ -is [PSSemVer])'. PowerShell applies -not to \ first, so the expression was always false and neither guard ever ran. Convert the comparand instead, which keeps -eq and -lt against a version string working and rejects what cannot be a version. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Super-linter summary
All files and directories linted successfully For more information, see the GitHub Actions workflow run Powered by Super-linter |
Marius Storhaug (MariusStorhaug)
marked this pull request as ready for review
August 8, 2026 16:17
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.
Comparing a
[PSSemVer]against something that is not a version now behaves predictably instead of silently producing a result.Fixed: The type guards in
CompareToandEqualsactually runBoth methods opened with
if (-not $other -is [PSSemVer]). PowerShell applies-notto$otherfirst, so the expression was((-not $other) -is [PSSemVer])— a[bool]tested against[PSSemVer], which is always$false. Neither guard ever ran, and both methods carried on reading.Major,.Minor, and.Patchoff whatever was passed. Absent properties came back$null, so a comparison against an unrelated object returned a confident, meaningless answer:Changed: The comparand is converted rather than type-checked
A strict
$other -isnot [PSSemVer]guard would have introduced a worse bug than the one being fixed, so the fix converts instead. The two operators behave differently, which is measurable:Under a strict guard,
-eqagainst a version string would have silently flipped fromTruetoFalse— comparisons that quietly stop being true are far more dangerous than a guard that never fires. Converting with-askeeps that working, returns$nullfor anything that is not a version, and needs notry/catch.The resulting contract:
CompareToEquals[PSSemVer]or version string$null1False[guid],[datetime], array, hashtable without version propertiesArgumentExceptionFalse$nullreturning1fromCompareTofollows theIComparableconvention that null sorts before any value; it was previously an accident of the same absent-property arithmetic.A property-shaped hashtable such as
@{ Major = 9; Minor = 0; Patch = 0 }still compares. That is PowerShell's own hashtable-to-class conversion doing real work, which is different in kind from the old behaviour of reading absent properties off any object at all. The linked issue lists that case as a symptom; on inspection it is legitimate, and the tests pin both halves of the boundary so it stays deliberate.Technical Details
src/classes/public/PSSemVer.ps1: both methods convert with$other -as [PSSemVer]and branch on$null; the remaining body reads from the converted$comparandrather than the raw$other.tests/PSSemVer.Tests.ps1: aType guardcontext with 13 cases covering the throw, theFalse, the$nullconventions, and — importantly — that-eq,-lt, andSort-Objectagainst version strings still behave as before.mainand from this branch and running the same suite against both: 4 failures againstmain, 0 against this branch, with the full suite at 79/79 and PSScriptAnalyzer clean oversrc/andtests/using the repo linter settings.GetHashCodeis deliberately untouched. It is a separate defect with its own decision attached to howEqualstreats build metadata.Related issues