Skip to content

Commit

Permalink
[release-branch.go1.22] cmd/go: download 1.X.0 instead of 1.X during …
Browse files Browse the repository at this point in the history
…toolchain upgrade.

This CL modifies the download behavior when downloading a toolchain for 1.21+. Previously, Go would attempt to download 1.X when upgrading the toolchain which would cause the download to fail for 1.21+ since 1.X is an invalid toolchain. We will attempt to download 1.X.0 since that's likely what the user intended.

Additionally, we will also now provide a better error message when the
user provides a language version instead of a toolchain version for
1.21+.

For #66175
For #62278

Fixes #67236

Change-Id: I28f894290a19d8e3cd220e9d70aeca8f4447e5a1
Reviewed-on: https://go-review.googlesource.com/c/go/+/580217
Reviewed-by: Michael Matloob <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
(cherry picked from commit 27ed85d)
Reviewed-on: https://go-review.googlesource.com/c/go/+/583915
  • Loading branch information
samthanawalla authored and cherrymui committed May 10, 2024
1 parent adbfb67 commit 00e6815
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/cmd/go/internal/toolchain/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ func Select() {
}
if gover.Compare(goVers, minVers) > 0 {
gotoolchain = "go" + goVers
// Starting with Go 1.21, the first released version has a .0 patch version suffix.
// Don't try to download a language version (sans patch component), such as go1.22.
// Instead, use the first toolchain of that language version, such as 1.22.0.
// See golang.org/issue/62278.
if gover.IsLang(goVers) && gover.Compare(goVers, "1.21") >= 0 {
gotoolchain += ".0"
}
gover.Startup.AutoGoVersion = goVers
gover.Startup.AutoToolchain = "" // in case we are overriding it for being too old
}
Expand Down Expand Up @@ -312,6 +319,10 @@ func Exec(gotoolchain string) {
dir, err := modfetch.Download(context.Background(), m)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
toolVers := gover.FromToolchain(gotoolchain)
if gover.IsLang(toolVers) && gover.Compare(toolVers, "1.21") >= 0 {
base.Fatalf("invalid toolchain: %s is a language version but not a toolchain version (%s.x)", gotoolchain, gotoolchain)
}
base.Fatalf("download %s for %s/%s: toolchain not available", gotoolchain, runtime.GOOS, runtime.GOARCH)
}
base.Fatalf("download %s: %v", gotoolchain, err)
Expand Down
104 changes: 104 additions & 0 deletions src/cmd/go/testdata/script/gotoolchain_issue66175.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
env TESTGO_VERSION=go1.14

# check for invalid toolchain in go.mod
go mod init m
go mod edit -go=1.14 -toolchain=go1.22
! go version
stderr 'go: invalid toolchain: go1.22 is a language version but not a toolchain version \(go1.22.x\)'

rm go.mod
go mod init m
go mod edit -go=1.14 -toolchain=go1.21
! go version
stderr 'go: invalid toolchain: go1.21 is a language version but not a toolchain version \(go1.21.x\)'

rm go.mod
go mod init m
go mod edit -go=1.14 -toolchain=go1.20
! go version
stderr 'go: downloading go1.20 '


# check for invalid GOTOOLCHAIN
env GOTOOLCHAIN=go1.14
go version
stdout 'go1.14'

env GOTOOLCHAIN=go1.20
! go version
stderr 'go: downloading go1.20 '

env GOTOOLCHAIN=go1.21
! go version
stderr 'go: invalid toolchain: go1.21 is a language version but not a toolchain version \(go1.21.x\)'

env GOTOOLCHAIN=go1.22
! go version
stderr 'go: invalid toolchain: go1.22 is a language version but not a toolchain version \(go1.22.x\)'

env GOTOOLCHAIN=go1.20+auto
! go version
stderr 'go: downloading go1.20 '

env GOTOOLCHAIN=go1.21+auto
! go version
stderr 'go: invalid toolchain: go1.21 is a language version but not a toolchain version \(go1.21.x\)'

env GOTOOLCHAIN=go1.22+auto
! go version
stderr 'go: invalid toolchain: go1.22 is a language version but not a toolchain version \(go1.22.x\)'

env GOTOOLCHAIN=go1.21rc3
! go version
stderr 'go: downloading go1.21rc3 '

env GOTOOLCHAIN=go1.22rc2
! go version
stderr 'go: downloading go1.22rc2 '

env GOTOOLCHAIN=go1.66
! go version
stderr 'go: invalid toolchain: go1.66 is a language version but not a toolchain version \(go1.66.x\)'

env GOTOOLCHAIN=go1.18beta2
! go version
stderr 'go: downloading go1.18beta2 '

# go1.X is okay for path lookups
env GOTOOLCHAIN=go1.20+path
! go version
stderr 'go: cannot find "go1.20" in PATH'

env GOTOOLCHAIN=go1.21+path
! go version
stderr 'go: cannot find "go1.21" in PATH'

env GOTOOLCHAIN=go1.22+path
! go version
stderr 'go: cannot find "go1.22" in PATH'

# When a toolchain download takes place, download 1.X.0
env GOTOOLCHAIN=auto
rm go.mod
go mod init m
go mod edit -go=1.300 -toolchain=none
! go version
stderr 'go: downloading go1.300.0 '

rm go.mod
go mod init m
go mod edit -go=1.21 -toolchain=none
! go version
stderr 'go: downloading go1.21.0 '

rm go.mod
go mod init m
go mod edit -go=1.22 -toolchain=none
! go version
stderr 'go: downloading go1.22.0 '

rm go.mod
go mod init m
go mod edit -go=1.15 -toolchain=none
! go version
stderr 'go: downloading go1.15 '

0 comments on commit 00e6815

Please sign in to comment.