]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: expand acceptable toolchains a bit
authorRuss Cox <rsc@golang.org>
Wed, 24 May 2023 19:22:33 +0000 (15:22 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 25 May 2023 17:51:31 +0000 (17:51 +0000)
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 <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/go/gotoolchain.go
src/cmd/go/internal/gover/latest.go
src/cmd/go/internal/gover/toolchain.go
src/cmd/go/testdata/script/gotoolchain.txt

index 03e2e95edd18ea11b5cb55f236892ecdb114a90d..ef56c23d3260df47d78a32b06415c26e3e805e9e 100644 (file)
@@ -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 {
index 05ee4401b5566a7a38e807ab0e733b0a5a1714c5..56d3f2d0497de0706d259daae6a52df31e1e51ef 100644 (file)
@@ -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)
 }
index 58a4d620f3501136f6c1053c0fc24671a1b6b53d..d3a2353550bcd241bdcdf46464bafd4608315dc3 100644 (file)
@@ -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
 }
 
index 406bbd7568ca6c38dd92e377ddd4c498541dc2a7..40a4b13c9d34220474f748bbb58eaedaccd6bea5 100644 (file)
@@ -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