Skip to content

Commit

Permalink
cmd/go: fix GOTOOLCHAIN parsing for +auto
Browse files Browse the repository at this point in the history
The call from toolchain.Select to gover.FromToolchain was passing the
wrong argument but this was masked by gover.IsValid being a little bit
too lax.

Fix and test IsValid, which then breaks the existing gotoolchain_local
test, and then fix toolchain.Select to fix the test.

Fixes golang#61068.

Change-Id: I505ceb227457d6a51bd5e47f009b2fb1010c0d1f
Reviewed-on: https://go-review.googlesource.com/c/go/+/508820
Reviewed-by: Bryan Mills <[email protected]>
Run-TryBot: Russ Cox <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
rsc authored and bradfitz committed Jul 15, 2023
1 parent 83ffbf3 commit b995362
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/cmd/go/internal/gover/gover.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ func parse(x string) version {
// Parse prerelease.
i := 0
for i < len(x) && (x[i] < '0' || '9' < x[i]) {
if x[i] < 'a' || 'z' < x[i] {
return version{}
}
i++
}
if i == 0 {
Expand Down
19 changes: 19 additions & 0 deletions src/cmd/go/internal/gover/gover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ var prevTests = []testCase1[string, string]{
{"1.40000000000000000", "1.39999999999999999"},
}

func TestIsValid(t *testing.T) { test1(t, isValidTests, "IsValid", IsValid) }

var isValidTests = []testCase1[string, bool]{
{"1.2rc3", true},
{"1.2.3", true},
{"1.999testmod", true},
{"1.600+auto", false},
{"1.22", true},
{"1.21.0", true},
{"1.21rc2", true},
{"1.21", true},
{"1.20.0", true},
{"1.20", true},
{"1.19", true},
{"1.3", true},
{"1.2", true},
{"1", true},
}

type testCase1[In, Out any] struct {
in In
out Out
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/gover/toolchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// FromToolchain returns the Go version for the named toolchain,
// derived from the name itself (not by running the toolchain).
// A toolchain is named "goVERSION".
// A suffix after the VERSION introduced by a +, -, space, or tab is removed.
// A suffix after the VERSION introduced by a -, space, or tab is removed.
// Examples:
//
// FromToolchain("go1.2.3") == "1.2.3"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/toolchain/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func Select() {
} else {
min, suffix, plus := strings.Cut(gotoolchain, "+") // go1.2.3+auto
if min != "local" {
v := gover.FromToolchain(gotoolchain)
v := gover.FromToolchain(min)
if v == "" {
if plus {
base.Fatalf("invalid GOTOOLCHAIN %q: invalid minimum toolchain %q", gotoolchain, min)
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/go/testdata/script/gotoolchain_local.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ env GOTOOLCHAIN=go1.600+auto
go version
stdout go1.600

env GOTOOLCHAIN=go1.400+auto
env GOTOOLCHAIN=go1.400.0+auto
go version
stdout go1.400
stdout go1.400.0

# GOTOOLCHAIN=version+path sets a minimum too.
env GOTOOLCHAIN=go1.600+path
Expand Down

0 comments on commit b995362

Please sign in to comment.