Skip to content

fix(version): order pre-release spellings longest-first - #15

Merged
jonyoder merged 1 commit into
mainfrom
fix-prerelease-alternation-order
Jul 31, 2026
Merged

jonyoder merged 1 commit into
mainfrom
fix-prerelease-alternation-order

Conversation

@jonyoder

Copy link
Copy Markdown
Collaborator

What

Order the PEP 440 pre-release spellings longest-first in the version regex.

Why

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:

(?P<pre_l>(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 (^\s*...\s*$, see version.go's init), so backtracking forced a correct full-token match and Parse was unaffected. But specifierRegexp is unanchored, so it kept the short match and FindAllString then harvested the trailing digits as a second, operator-less specifier:

input parsed as
==1.0alpha1 ==1.0a and 1
==1.0-beta2 ==1.0-b and 2
==1.0preview1 ==1.0pre and 1

Both 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 in specifierOperators (itself annotated "not defined in PEP 440").

Verified against pypa/packaging 26.2, which accepts ==1.0alpha1 and matches 1.0a1. pypa orders its own equivalent alternation alpha|a|beta|b|preview|pre|c|rc — longest-before-prefix — for exactly this reason, since Python's re is 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: r is a prefix of rev, 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 ./... and go 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.

…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>
@jonyoder
jonyoder merged commit 90d27c5 into main Jul 31, 2026
2 checks passed
@jonyoder
jonyoder deleted the fix-prerelease-alternation-order branch July 31, 2026 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant