Skip to content

Commit cb72406

Browse files
committed
cmd/go: fix two-step toolchain upgrade through go install, GOTOOLCHAIN
If we do one upgrade because of a go install target's go.mod file, we still might need a second upgrade to implement the GOTOOLCHAIN minimum. Instead of allowing a two-step switch (which we were cutting off anyway), skip the first step and go straight to the GOTOOLCHAIN min upgrade. Fixes #69051. Change-Id: I16f060f473574d8b8f84c55fae2fd0cdabc8aa19 Reviewed-on: https://go-review.googlesource.com/c/go/+/637496 Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 4f0561f commit cb72406

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/cmd/go/internal/toolchain/select.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func Select() {
169169
}
170170

171171
gotoolchain = minToolchain
172-
if (mode == "auto" || mode == "path") && !goInstallVersion() {
172+
if (mode == "auto" || mode == "path") && !goInstallVersion(minVers) {
173173
// Read go.mod to find new minimum and suggested toolchain.
174174
file, goVers, toolchain := modGoToolchain()
175175
gover.Startup.AutoFile = file
@@ -549,7 +549,7 @@ func modGoToolchain() (file, goVers, toolchain string) {
549549

550550
// goInstallVersion reports whether the command line is go install m@v or go run m@v.
551551
// If so, Select must not read the go.mod or go.work file in "auto" or "path" mode.
552-
func goInstallVersion() bool {
552+
func goInstallVersion(minVers string) bool {
553553
// Note: We assume there are no flags between 'go' and 'install' or 'run'.
554554
// During testing there are some debugging flags that are accepted
555555
// in that position, but in production go binaries there are not.
@@ -708,7 +708,11 @@ func goInstallVersion() bool {
708708
if errors.Is(err, gover.ErrTooNew) {
709709
// Run early switch, same one go install or go run would eventually do,
710710
// if it understood all the command-line flags.
711-
SwitchOrFatal(ctx, err)
711+
var s Switcher
712+
s.Error(err)
713+
if s.TooNew != nil && gover.Compare(s.TooNew.GoVersion, minVers) > 0 {
714+
SwitchOrFatal(ctx, err)
715+
}
712716
}
713717

714718
return true // pkg@version found

src/cmd/go/testdata/script/gotoolchain_local.txt

+11
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ go mod edit -go=1.501 -toolchain=none
197197
go version
198198
stdout go1.501
199199

200+
# avoid two-step switch, first from install target requirement, then from GOTOOLCHAIN min
201+
# instead, just jump directly to GOTOOLCHAIN min
202+
env TESTGO_VERSION=go1.2.3
203+
env GODEBUG=toolchaintrace=1
204+
env GOTOOLCHAIN=go1.23.0+auto
205+
! go install rsc.io/fortune/[email protected]
206+
! stderr 'switching to go1.22.9'
207+
stderr 'using go1.23.0'
208+
env GODEBUG=
209+
env GOTOOLCHAIN=auto
210+
200211
# go install m@v and go run m@v should ignore go.mod and use m@v
201212
env TESTGO_VERSION=go1.2.3
202213
go mod edit -go=1.999 -toolchain=go1.998

0 commit comments

Comments
 (0)