Skip to content

Commit 7b3786b

Browse files
rscmknyszek
authored andcommitted
[release-branch.go1.22] go/version: fix package to accept go1.21.0-bigcorp
The proposal discussion made clear that suffixes should be accepted, so that people who use custom VERSION files can still pass runtime.Version() to this code. But we forgot to do that in the CL. Do that. Note that cmd/go also strips space- and tab-prefixed suffixes, but go.dev/doc/toolchain only mentions dash, so this code only strips dash. Fixes #65061. Change-Id: I6a427b78f964eb41c024890dae30223beaef13eb Cq-Include-Trybots: luci.golang.try:go1.22-linux-amd64-longtest,go1.22-windows-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/go/+/559796 TryBot-Bypass: Russ Cox <[email protected]> Reviewed-by: Mauri de Souza Meneguzzo <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/559802 LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 9289b9c commit 7b3786b

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/go/version/version.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// Package version provides operations on [Go versions].
5+
// Package version provides operations on [Go versions]
6+
// in [Go toolchain name syntax]: strings like
7+
// "go1.20", "go1.21.0", "go1.22rc2", and "go1.23.4-bigcorp".
68
//
79
// [Go versions]: https://go.dev/doc/toolchain#version
10+
// [Go toolchain name syntax]: https://go.dev/doc/toolchain#name
811
package version // import "go/version"
912

1013
import (
1114
"internal/gover"
1215
"strings"
1316
)
1417

15-
// stripGo converts from a "go1.21" version to a "1.21" version.
18+
// stripGo converts from a "go1.21-bigcorp" version to a "1.21" version.
1619
// If v does not start with "go", stripGo returns the empty string (a known invalid version).
1720
func stripGo(v string) string {
21+
v, _, _ = strings.Cut(v, "-") // strip -bigcorp suffix.
1822
if len(v) < 2 || v[:2] != "go" {
1923
return ""
2024
}
@@ -50,8 +54,6 @@ func Lang(x string) string {
5054
// valid versions and equal to each other.
5155
// The language version "go1.21" compares less than the
5256
// release candidate and eventual releases "go1.21rc1" and "go1.21.0".
53-
// Custom toolchain suffixes are ignored during comparison:
54-
// "go1.21.0" and "go1.21.0-bigcorp" are equal.
5557
func Compare(x, y string) int {
5658
return gover.Compare(stripGo(x), stripGo(y))
5759
}

src/go/version/version_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ var compareTests = []testCase2[string, string, int]{
2323
{"go1.19", "go1.19.0", 0},
2424
{"go1.19rc1", "go1.19", -1},
2525
{"go1.20", "go1.20.0", 0},
26+
{"go1.20", "go1.20.0-bigcorp", 0},
2627
{"go1.20rc1", "go1.20", -1},
2728
{"go1.21", "go1.21.0", -1},
29+
{"go1.21", "go1.21.0-bigcorp", -1},
2830
{"go1.21", "go1.21rc1", -1},
2931
{"go1.21rc1", "go1.21.0", -1},
3032
{"go1.6", "go1.19", -1},
3133
{"go1.19", "go1.19.1", -1},
3234
{"go1.19rc1", "go1.19", -1},
35+
{"go1.19rc1", "go1.19", -1},
3336
{"go1.19rc1", "go1.19.1", -1},
3437
{"go1.19rc1", "go1.19rc2", -1},
3538
{"go1.19.0", "go1.19.1", -1},

0 commit comments

Comments
 (0)