Skip to content

Commit 9892cd6

Browse files
author
Bryan C. Mills
committed
cmd/go: do not allow version prefixes to match prereleases of that version
Fixes #31972 Change-Id: I3bb9ef3a1134e67d2d062bea2f0e4032647e12e6 Reviewed-on: https://go-review.googlesource.com/c/go/+/176898 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 5b4ea62 commit 9892cd6

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/cmd/go/internal/modload/query.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func Query(path, query string, allowed func(module.Version) bool) (*modfetch.Rev
186186
return nil, &NoMatchingVersionError{query: query}
187187
}
188188

189-
// isSemverPrefix reports whether v is a semantic version prefix: v1 or v1.2 (not wv1.2.3).
189+
// isSemverPrefix reports whether v is a semantic version prefix: v1 or v1.2 (not v1.2.3).
190190
// The caller is assumed to have checked that semver.IsValid(v) is true.
191191
func isSemverPrefix(v string) bool {
192192
dots := 0
@@ -207,7 +207,7 @@ func isSemverPrefix(v string) bool {
207207
// matchSemverPrefix reports whether the shortened semantic version p
208208
// matches the full-width (non-shortened) semantic version v.
209209
func matchSemverPrefix(p, v string) bool {
210-
return len(v) > len(p) && v[len(p)] == '.' && v[:len(p)] == p
210+
return len(v) > len(p) && v[len(p)] == '.' && v[:len(p)] == p && semver.Prerelease(v) == ""
211211
}
212212

213213
type QueryResult struct {

src/cmd/go/internal/modload/query_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ var queryTests = []struct {
7676
git checkout v2
7777
echo module vcs-test.golang.org/git/querytest.git/v2 >go.mod
7878
git commit -m v2 go.mod
79-
for i in v2.0.0 v2.1.0 v2.2.0 v2.5.5; do
79+
for i in v2.0.0 v2.1.0 v2.2.0 v2.5.5 v2.6.0-pre1; do
8080
echo before $i >status
8181
git add status
8282
git commit -m "before $i" status
8383
echo at $i >status
8484
git commit -m "at $i" status
8585
git tag $i
8686
done
87+
git checkout v2.5.5
8788
echo after v2.5.5 >status
8889
git commit -m 'after v2.5.5' status
8990
git checkout master
@@ -117,6 +118,10 @@ var queryTests = []struct {
117118
{path: queryRepoV2, query: ">v0.0.0", vers: "v2.0.0"},
118119
{path: queryRepoV2, query: ">=v0.0.0", vers: "v2.0.0"},
119120
{path: queryRepoV2, query: "v0.0.1+foo", vers: "v2.0.0-20180704023347-179bc86b1be3"},
121+
{path: queryRepoV2, query: "v2", vers: "v2.5.5"},
122+
{path: queryRepoV2, query: "v2.5", vers: "v2.5.5"},
123+
{path: queryRepoV2, query: "v2.6", err: `no matching versions for query "v2.6"`},
124+
{path: queryRepoV2, query: "v2.6.0-pre1", vers: "v2.6.0-pre1"},
120125
{path: queryRepoV2, query: "latest", vers: "v2.5.5"},
121126

122127
{path: queryRepoV3, query: "latest", vers: "v3.0.0-20180704024501-e0cf3de987e6"},

src/cmd/go/testdata/script/mod_query.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@ env GO111MODULE=on
33
go list -m -versions rsc.io/quote
44
stdout '^rsc.io/quote v1.0.0 v1.1.0 v1.2.0 v1.2.1 v1.3.0 v1.4.0 v1.5.0 v1.5.1 v1.5.2 v1.5.3-pre1$'
55

6-
# latest rsc.io/quote should be v1.5.2 not v1.5.3-pre1
6+
# Latest rsc.io/quote should be v1.5.2, not v1.5.3-pre1.
77
go list -m rsc.io/quote@latest
88
stdout 'rsc.io/quote v1.5.2$'
99

10+
# Same for rsc.io/quote@v1 and rsc.io/[email protected] (with no patch version).
11+
go list -m rsc.io/quote@v1
12+
stdout 'rsc.io/quote v1.5.2$'
13+
go list -m rsc.io/[email protected]
14+
stdout 'rsc.io/quote v1.5.2$'
15+
16+
# We should fall back to prereleases if no release tags match...
1017
go list -m rsc.io/quote@>v1.5.2
1118
stdout 'rsc.io/quote v1.5.3-pre1$'
1219

20+
# ...but prefer release versions when given the option.
1321
go list -m rsc.io/quote@<v1.5.4
1422
stdout 'rsc.io/quote v1.5.2$'
1523

0 commit comments

Comments
 (0)