Repository navigation
fix(version): order pre-release spellings longest-first - #15
Merged
Merged
Conversation
…age-manager#19369) PEP 440 permits "alpha", "beta", "pre", "preview" and "c" as alternate spellings of "a", "b", "rc" and "rc". The pre_l alternation listed them shortest-first: (a|b|c|rc|alpha|beta|pre|preview) Go's regexp is leftmost-first, not leftmost-longest, so "alpha" matched as just "a". versionRegex is anchored, so backtracking hid this in Parse -- but specifierRegexp is unanchored, so it kept the short match, and FindAllString then harvested the trailing digits as a SECOND, operator-less specifier: "==1.0alpha1" -> "==1.0a,1" "==1.0-beta2" -> "==1.0-b,2" "==1.0preview1" -> "==1.0pre,1" Both halves were ANDed, so the constraint parsed without error and then matched nothing at all. pypa/packaging orders its equivalent alternation longest-before-prefix for this same reason (Python's re is also leftmost-first); this restores that ordering. post_l (`post|rev|r`) was checked and is already safe -- "r" is a prefix of "rev" but is listed last. Guard cases added. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
What
Order the PEP 440 pre-release spellings longest-first in the version regex.
Why
PEP 440 permits
alpha,beta,pre,previewandcas alternate spellings ofa,b,rcandrc. Thepre_lalternation listed them shortest-first:Go's
regexpis leftmost-first, not leftmost-longest, soalphamatched as justa.versionRegexis anchored (^\s*...\s*$, seeversion.go'sinit), so backtracking forced a correct full-token match andParsewas unaffected. ButspecifierRegexpis unanchored, so it kept the short match andFindAllStringthen harvested the trailing digits as a second, operator-less specifier:==1.0alpha1==1.0aand1==1.0-beta2==1.0-band2==1.0preview1==1.0preand1Both halves were ANDed together, so the constraint parsed without error and then matched nothing at all — a silent wrong answer rather than a rejection. The trailing fragment only parses at all because of the operator-less
""entry inspecifierOperators(itself annotated "not defined in PEP 440").Verified against
pypa/packaging26.2, which accepts==1.0alpha1and matches1.0a1. pypa orders its own equivalent alternationalpha|a|beta|b|preview|pre|c|rc— longest-before-prefix — for exactly this reason, since Python'sreis also leftmost-first (version.py#L199). This restores the upstream ordering that was lost in the port.Scope
post_l(post|rev|r) was checked and is already safe:ris a prefix ofrev, but it is listed last, so leftmost-first already picks the long form. Verified against the unfixed baseline; guard cases added so a future reorder can't silently regress it.Testing
TestNewSpecifiers_LongPreReleaseSpellingIsOneSpecifier— asserts a specifier string never yields more specifiers than it has operators, and that each long spelling matches its canonical version. Verified to fail without the one-line fix, individually.TestParse_LongPreReleaseSpellings— pins the alias mapping. Passes before and after (the anchored regex was never the broken path); kept as a characterization test and labeled as such with the anchoring explained, so it isn't mistaken for the regression test.go test ./...andgo test -race ./...green across all 14 packages.gofmt -l .clean;golangci-lint config verify+golangci-lint run ./...→ 0 issues at CI's pinned v2.11.2, from the module root.Context
Found while scoping the PEP 440/508 conformance tables (rstudio/package-manager#18640) — the defect was invisible to the existing hand-written tests, which is a direct argument for that work. Tracked as rstudio/package-manager#19369.
The same defect exists in the predecessor module
rstudio/go-pep440-version, which PPM ships today; fixed there in rstudio/go-pep440-version#5.