Comments (5)
As a test case:
package main
import (
"log"
"github.com/hashicorp/go-version"
)
func main() {
constraint, _ := version.NewConstraint(">= 2.0")
version, _ := version.NewVersion("2.2.0-302bd8b")
ok := constraint.Check(version)
log.Printf("version check: %#v", ok)
}
$ go run ./test-version.go
2018/05/10 13:54:22 version check: false
from go-version.
Same problem.
from go-version.
@jbardin You're probably more intimately familiar with this, do you mind taking a look?
from go-version.
Hi @nevans,
I think the comparison is correctly happening according to the specification. Semantic versioning alone doesn't define the constraint operations, only the comparisons between versions. If you take the examples versions you provided and compare them, you get
major := version.Must(version.NewVersion("2.0"))
pre := version.Must(version.NewVersion("2.2.0-302bd8b"))
fmt.Printf("%s > %s = %t\n", pre, major, pre.GreaterThan(major))
// 2.2.0-302bd8b > 2.0.0 = true
Which follows Item 11 in the SemVer spec.
The difference from your first example is that when a user adds ">= 2.0"
as a versions constraint, it implies "any released version greater than or equal to 2.0". Looking at NPM, which has another published specification with the similar constraint operators you can see in the Prerelease tags section:
If a version has a prerelease tag (for example, 1.2.3-alpha.3) then it will only be allowed to satisfy comparator sets if at least one comparator with the same [major, minor, patch] tuple also has a prerelease tag
The proper constraint to include 2.2.0
alphanumeric preleases like this 2.2.0-302bd8b
would be >=2.2.0-0
.
There is however still a point of contention here. As shown above, NPM would require (Major, Minor, Patch) to match a pre-release, which is the behavior we followed in this package. I did mention Ruby Gems though, and we differ there as they chose to match any (Major Minor, Patch) as long as a prerelease was specified. For example
> Gem::Dependency.new('', '>= 2.0').match?('', '2.2.0-302bd8b')
=> false
> Gem::Dependency.new('', '>= 2.0.0-0').match?('', '2.2.0-302bd8b')
=> true
So Ruby would allow matching between the versions you specified, provided you start with a more precise constraint string.
Now we did choose to follow the former behavior rather than the latter, since it provided a safer constraint set without compound statements being provided. Hopefully this clears up the situation, and we can better document this behavior for users.
from go-version.
Thanks, @jbardin for your explanation. I can see why versioning constraints for selecting compatible libraries would want these constraints for prereleases, even though both Ruby gems and NPM fail the principle of least surprise for me. 😉 I'd expected the >=
to follow strict comparison ordering.
For our use case, we needed feature detection on a running server, which means that pre-releases shouldn't be treated as special. At any rate, as per your example, we could use a simple GreaterThan
comparison without issue. I hadn't thought of that. Since our use case was very simple, we simply handled it by replacing go-version
with a strings.Split
, strconv.Atoi
, and major >= 2
. https://github.com/nevans/couchdb-prometheus-exporter/blob/df807430dc1007376f189581a8b1f9d51e58e18d/lib/couchdb-client.go#L53-L66
Thanks for taking the time to explain it.
from go-version.
Related Issues (20)
- Incorrect hanling of prerelease in constraints HOT 4
- Prerelease String shouldn't always be treated as lower version
- Invalid semver versions are treated as "valid" HOT 1
- Memory increasing every Version.String() called HOT 3
- Version `v1.0.2-alpha.0` should be greater than `v1.0.1` but `version.NewConstraint(...).Check` returns false HOT 1
- When i compare with Empty version string Getting Error
- Must() method for Constraint
- Incorrectly evaluates contraints involving prereleases
- Malformed version for linux kernel HOT 2
- Comparing alpine packages versions
- Prerelease bug hidden due to bad tests HOT 1
- panic when version is empty string HOT 2
- [Bug] Cant pars pereleased git version HOT 2
- Incorrectly says some inputs are semver
- Double-digit prerelease sorting broken HOT 1
- Leading zeros are explicitly forbidden in Semantic Versioning HOT 2
- [question] which is bigger according the constraint? HOT 1
- Support json marshaling/unmarshaling
- Proposal: Add version diff
- Skipping some versions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-version.