From: Russ Cox Date: Wed, 24 May 2023 19:22:33 +0000 (-0400) Subject: cmd/go: expand acceptable toolchains a bit X-Git-Tag: go1.21rc1~229 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e8c6003023b64b80bea6af8c4bb0b54d55f65dc0;p=gostls13.git cmd/go: expand acceptable toolchains a bit Allow both prefix-goVERSION and goVERSION-suffix for custom toolchains. Also make sure that a tie in the toolchain and min version goes to the toolchain line. For #57001. Change-Id: Ibeea5f47db7349cfdf36da188bb43e195e196f5b Reviewed-on: https://go-review.googlesource.com/c/go/+/498076 Auto-Submit: Russ Cox Run-TryBot: Russ Cox Reviewed-by: Bryan Mills TryBot-Result: Gopher Robot --- diff --git a/src/cmd/go/gotoolchain.go b/src/cmd/go/gotoolchain.go index 03e2e95edd..ef56c23d32 100644 --- a/src/cmd/go/gotoolchain.go +++ b/src/cmd/go/gotoolchain.go @@ -126,7 +126,7 @@ func switchGoToolchain() { } else if toolchain != "" { // Accept toolchain only if it is >= our min. toolVers := gover.ToolchainVersion(toolchain) - if gover.Compare(toolVers, minVers) > 0 { + if gover.Compare(toolVers, minVers) >= 0 { gotoolchain = toolchain } } else { diff --git a/src/cmd/go/internal/gover/latest.go b/src/cmd/go/internal/gover/latest.go index 05ee4401b5..56d3f2d049 100644 --- a/src/cmd/go/internal/gover/latest.go +++ b/src/cmd/go/internal/gover/latest.go @@ -8,7 +8,6 @@ import ( "internal/goversion" "runtime" "strconv" - "strings" ) // TestVersion is initialized in the go command test binary @@ -22,16 +21,10 @@ func Local() string { if TestVersion != "" { v = TestVersion } - if strings.HasPrefix(v, "go") { - v := strings.TrimPrefix(v, "go") - // Some builds use custom suffixes; strip them. - if i := strings.IndexAny(v, " \t+-"); i >= 0 { - v = v[:i] - } - if IsValid(v) { - return v - } + if v := ToolchainVersion(v); v != "" { + return v } + // Development branch. Use "Dev" version with just 1.N, no rc1 or .0 suffix. return "1." + strconv.Itoa(goversion.Version) } diff --git a/src/cmd/go/internal/gover/toolchain.go b/src/cmd/go/internal/gover/toolchain.go index 58a4d620f3..d3a2353550 100644 --- a/src/cmd/go/internal/gover/toolchain.go +++ b/src/cmd/go/internal/gover/toolchain.go @@ -14,18 +14,28 @@ import ( // ToolchainVersion returns the Go version for the named toolchain, // derived from the name itself (not by running the toolchain). // A toolchain is named "goVERSION" or "anything-goVERSION". +// A suffix after the VERSION introduced by a +, -, space, or tab is removed. // Examples: // // ToolchainVersion("go1.2.3") == "1.2.3" +// ToolchainVersion("go1.2.3+bigcorp") == "1.2.3" +// ToolchainVersion("go1.2.3-bigcorp") == "1.2.3" // ToolchainVersion("gccgo-go1.23rc4") == "1.23rc4" // ToolchainVersion("invalid") == "" func ToolchainVersion(name string) string { var v string - if strings.HasPrefix(name, "go") && IsValid(name[2:]) { + if strings.HasPrefix(name, "go") { v = name[2:] - } else if i := strings.Index(name, "-go"); i >= 0 && IsValid(name[i+3:]) { + } else if i := strings.Index(name, "-go"); i >= 0 { v = name[i+3:] } + // Some builds use custom suffixes; strip them. + if i := strings.IndexAny(v, " \t+-"); i >= 0 { + v = v[:i] + } + if !IsValid(v) { + return "" + } return v } diff --git a/src/cmd/go/testdata/script/gotoolchain.txt b/src/cmd/go/testdata/script/gotoolchain.txt index 406bbd7568..40a4b13c9d 100644 --- a/src/cmd/go/testdata/script/gotoolchain.txt +++ b/src/cmd/go/testdata/script/gotoolchain.txt @@ -21,6 +21,8 @@ mkdir $WORK/bin [!GOOS:plan9] env PATH=$WORK/bin${:}$PATH [GOOS:plan9] env path=$WORK/bin${:}$path go build -o $WORK/bin/ ./go1.999testpath.go # adds .exe extension implicitly on Windows +cp $WORK/bin/go1.999testpath$GOEXE $WORK/bin/custom-go1.999.0$GOEXE +cp $WORK/bin/go1.999testpath$GOEXE $WORK/bin/go1.999.0-custom$GOEXE # GOTOOLCHAIN from PATH env GOTOOLCHAIN=go1.999testpath @@ -49,6 +51,25 @@ cp go119toolchain1999 go.mod go version stdout go1.999 +# custom toolchain line in go.mod +env TESTGO_VERSION=go1.999 +go version +stdout testpath # go1.999 < go1.999testpath + +env TESTGO_VERSION=go1.999.0 +go version +! stdout testpath # go1.999testpath < go1.999.0 + +cp go119customtoolchain1999 go.mod +go version +stdout go1.999testpath # custom-go1.999.0 >= go1.999.0 + +cp go119customtoolchain1999b go.mod +go version +stdout go1.999testpath # go1.999.0-custom >= go1.999.0 + +env TESTGO_VERSION=go1.100 + # toolchain local in go.mod cp go1999toolchainlocal go.mod ! go build @@ -258,6 +279,14 @@ go 1.19 go 1.19 toolchain go1.999testpath +-- go119customtoolchain1999 -- +go 1.19 +toolchain custom-go1.999.0 + +-- go119customtoolchain1999b -- +go 1.19 +toolchain go1.999.0-custom + -- go1999toolchainlocal -- go 1.999 toolchain local