Skip to content

Commit 3f51350

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modfetch/codehost: replace a dubious call to semver.Max
The documentation for RecentTag indicates that it returns an actual tag, not a canonicalized prefix+version blob equivalent to a tag, so the canonicalization due to semver.Max seems like a bug here. Fortunately, RecentTag is not currently ever actually used as a tag, so the removal of metadata does not result in a user-facing bug. Nonetheless, it may be a subtle source of confusion for maintainers in the future. Updates #32700 Change-Id: I525423c1c0c7ec7c36c09e53b180034474f74e5a Reviewed-on: https://go-review.googlesource.com/c/go/+/212202 Run-TryBot: Bryan C. Mills <bcmills@google.com> Reviewed-by: Jay Conrod <jayconrod@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 8257528 commit 3f51350

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/cmd/go/internal/modfetch/codehost/git.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,11 @@ func (r *gitRepo) RecentTag(rev, prefix, major string) (tag string, err error) {
682682

683683
semtag := line[len(prefix):]
684684
// Consider only tags that are valid and complete (not just major.minor prefixes).
685-
if c := semver.Canonical(semtag); c != "" && strings.HasPrefix(semtag, c) && (major == "" || semver.Major(c) == major) {
686-
highest = semver.Max(highest, semtag)
685+
// NOTE: Do not replace the call to semver.Compare with semver.Max.
686+
// We want to return the actual tag, not a canonicalized version of it,
687+
// and semver.Max currently canonicalizes (see golang.org/issue/32700).
688+
if c := semver.Canonical(semtag); c != "" && strings.HasPrefix(semtag, c) && (major == "" || semver.Major(c) == major) && semver.Compare(semtag, highest) > 0 {
689+
highest = semtag
687690
}
688691
}
689692

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ var queryTests = []struct {
6464
git add go.mod
6565
git commit -m v1 go.mod
6666
git tag start
67-
for i in v0.0.0-pre1 v0.0.0 v0.0.1 v0.0.2 v0.0.3 v0.1.0 v0.1.1 v0.1.2 v0.3.0 v1.0.0 v1.1.0 v1.9.0 v1.9.9 v1.9.10-pre1 v1.9.10-pre2+metadata; do
67+
for i in v0.0.0-pre1 v0.0.0 v0.0.1 v0.0.2 v0.0.3 v0.1.0 v0.1.1 v0.1.2 v0.3.0 v1.0.0 v1.1.0 v1.9.0 v1.9.9 v1.9.10-pre1 v1.9.10-pre2+metadata unversioned; do
6868
echo before $i >status
6969
git add status
7070
git commit -m "before $i" status
@@ -107,6 +107,7 @@ var queryTests = []struct {
107107
{path: queryRepo, query: "v0.2", err: `no matching versions for query "v0.2"`},
108108
{path: queryRepo, query: "v0.0", vers: "v0.0.3"},
109109
{path: queryRepo, query: "v1.9.10-pre2+metadata", vers: "v1.9.10-pre2.0.20190513201126-42abcb6df8ee"},
110+
{path: queryRepo, query: "ed5ffdaa", vers: "v1.9.10-pre2.0.20191220134614-ed5ffdaa1f5e"},
110111

111112
// golang.org/issue/29262: The major version for for a module without a suffix
112113
// should be based on the most recent tag (v1 as appropriate, not v0

0 commit comments

Comments
 (0)