Build metadata should be ignored when determining version precedence

#11 · closed · 8 comments

View on GitHub ↗

peterkorodi

I think I've found an issue with metadata: Based on https://semver.org/#spec-item-10 when you compare two version the metadata should be ignored. > Build metadata MAY be denoted by appending a plus sign and a series of dot separated identifiers immediately following the patch or pre-release version. Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-]. Identifiers MUST NOT be empty. Build metadata MUST be ignored when determining version precedence. Thus two versions that differ only in the build metadata, have the same precedence. Examples: 1.0.0-alpha+001, 1.0.0+20130313144700, 1.0.0-beta+exp.sha.5114f85. So I think the following code should work: ```swift if let v1 = Version("1.0.0+build1"), let v2 = Version("1.0.0+build2") { assert(v1 == v2) } ```

Comments

mxcl

Precedence suggests only when comparing with `<` or `>` and that is indeed what we do.

peterkorodi

I've checked other semver libs. For example using the semver python lib (https://github.com/python-semver/python-semver) this code is valid: ```python import semver result = semver.compare('1.0.0+build1', '1.0.0+build2') assert result == 0 ``` ```python def compare(ver1, ver2): """Compare two versions :param ver1: version string 1 :param ver2: version string 2 :return: The return value is negative if ver1 < ver2, zero if ver1 == ver2 and strictly positive if ver1 > ver2 :rtype: int """ v1, v2 = parse(ver1), parse(ver2) return _compare_by_keys(v1, v2) ``` Please no offence, I'd just like to help to make your lib better. Or maybe there is another way to check that the two versions have the same precedence?

mxcl

Well, that’s compare, which is not an equality check though. > Please no offence, I'd just like to help to make your lib better. Non taken, this is all professional here.

ddddxxx

I think `Version` should respect Semver equivalence just like `String` respect Unicode equivalence. ```swift Version("1.0.0") == Version("1.0.0+metadata") // false, should be true IMO "\u{E9}" == "\u{65}\u{301}" // true ```

mxcl

My understanding of the spec is build metadata is ignored for Comparable but taken into account for Equatable which is what we do. I will look at some more Version libraries to see what they do.

mxcl

Changed my mind on the spec interpretation. See PR.

mxcl

2.0.0 released.

peterkorodi

Sounds great! Thanks!