Masterminds / semver

Work with Semantic Versions in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

alphanumeric identifier in prerelease version should be allowed to have a leading 0

oocx opened this issue · comments

According to https://semver.org/#spec-item-9, the limitation that leading zeros are not allowed applies to numeric identifiers, not alphanumeric identifiers. However, currently alphanumeric identifiers with a leading 0 are rejected as "invalid" as well.

For example, we use major.minor.patch-commitHash as version number for our helm charts. This currently fails if the commit hash randomly happens to start with a 0.

I've reported this as a bug in helm (helm/helm#7064), the helm maintainers asked me to open a ticket here.

Could you please fix this? Or is my understanding of the semver spec wrong?

Regards
Mathias

@oocx can you provide some examples that are failing as bugs. The one on the Helm issue was a number that started with 0 and did not contain characters outside of 0-9. Therefore it's a number in terms of the spec. A commit hash can be a number.

The section of code in semver that validates pre-release segments can be found at

semver/version.go

Lines 579 to 592 in 49c09bf

func validatePrerelease(p string) error {
eparts := strings.Split(p, ".")
for _, p := range eparts {
if containsOnly(p, num) {
if len(p) > 1 && p[0] == '0' {
return ErrSegmentStartsZero
}
} else if !containsOnly(p, allowed) {
return ErrInvalidPrerelease
}
}
return nil
}

@mattfarina An alphanumeric identifier is not required to contain letters, it can consist of numbers only, including a leading 0. The spec even provides a regular expression.

Identifiers MUST comprise only ASCII alphanumerics and hyphens [0-9A-Za-z-]

I think the spec could be interpreted in both ways. I'll ask in the semver repo for a clarification.

@mattfarina I found the answer: while the spec text is ambiguous, there is actually a BNF that defines what valid values are: https://semver.org/#backusnaur-form-grammar-for-valid-semver-versions

According to this spec, an alphanumeric identifier must always contain at least one non-digit character.

<alphanumeric identifier> ::= <non-digit>
                            | <non-digit> <identifier characters>
                            | <identifier characters> <non-digit>
                            | <identifier characters> <non-digit> <identifier characters>

So your implementation is correct according to the spec. Sorry for bothering you with this issue.