Skip to content

Commit a68bf75

Browse files
committed
cmd/go: don't write own toolchain line when updating go line
The Go command had a behavior of writing its own toolchain name when updating the go line in a go.mod (for example when a user runs go get go@version). This behavior was often undesirable and the toolchain line was often removed by users before checking in go.mod files (including in the x/ repos). It also led to user confusion. This change removes that behavior. A toolchain line will not be added if one wasn't present before. The toolchain line can still be removed though: the toolchain line must be at least the go version, so if the go version is increased above the toolchain version, the toolchain version will be bumped up to that go version. Then the toolchain line will then be dropped because go <version> implies toolchain <version>. Making this change slightly hurts reproducability because future go commands run on the go.mod file may be run with a different toolchain than the one that used it, but that doesn't seem to be worth the confusion the behavior resulted in. We expect this change will not have negative consequences, but it could be possible, and we would like to hear from any users that depended on the previous behavior in case we need to roll it back before the release. Fixes #65847 Change-Id: Id795b7f762e4f90ba0fa8c7935d03f32dfc8590e Reviewed-on: https://go-review.googlesource.com/c/go/+/656835 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 485480f commit a68bf75

11 files changed

+52
-74
lines changed

src/cmd/go/internal/modload/init.go

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -745,34 +745,25 @@ func UpdateWorkGoVersion(wf *modfile.WorkFile, goVers string) (changed bool) {
745745

746746
wf.AddGoStmt(goVers)
747747

748-
// We wrote a new go line. For reproducibility,
749-
// if the toolchain running right now is newer than the new toolchain line,
750-
// update the toolchain line to record the newer toolchain.
751-
// The user never sets the toolchain explicitly in a 'go work' command,
752-
// so this is only happening as a result of a go or toolchain line found
753-
// in a module.
754-
// If the toolchain running right now is a dev toolchain (like "go1.21")
755-
// writing 'toolchain go1.21' will not be useful, since that's not an actual
756-
// toolchain you can download and run. In that case fall back to at least
757-
// checking that the toolchain is new enough for the Go version.
758-
toolchain := "go" + old
759-
if wf.Toolchain != nil {
760-
toolchain = wf.Toolchain.Name
761-
}
762-
if gover.IsLang(gover.Local()) {
763-
toolchain = gover.ToolchainMax(toolchain, "go"+goVers)
764-
} else {
765-
toolchain = gover.ToolchainMax(toolchain, "go"+gover.Local())
748+
if wf.Toolchain == nil {
749+
return true
766750
}
767751

768-
// Drop the toolchain line if it is implied by the go line
752+
// Drop the toolchain line if it is implied by the go line,
753+
// if its version is older than the version in the go line,
769754
// or if it is asking for a toolchain older than Go 1.21,
770755
// which will not understand the toolchain line.
771-
if toolchain == "go"+goVers || gover.Compare(gover.FromToolchain(toolchain), gover.GoStrictVersion) < 0 {
756+
// Previously, a toolchain line set to the local toolchain
757+
// version was added so that future operations on the go file
758+
// would use the same toolchain logic for reproducibility.
759+
// This behavior seemed to cause user confusion without much
760+
// benefit so it was removed. See #65847.
761+
toolchain := wf.Toolchain.Name
762+
toolVers := gover.FromToolchain(toolchain)
763+
if toolchain == "go"+goVers || gover.Compare(toolVers, goVers) < 0 || gover.Compare(toolVers, gover.GoStrictVersion) < 0 {
772764
wf.DropToolchainStmt()
773-
} else {
774-
wf.AddToolchainStmt(toolchain)
775765
}
766+
776767
return true
777768
}
778769

@@ -1833,22 +1824,7 @@ func UpdateGoModFromReqs(ctx context.Context, opts WriteOpts) (before, after []b
18331824
toolchain = "go" + goVersion
18341825
}
18351826

1836-
// For reproducibility, if we are writing a new go line,
1837-
// and we're not explicitly modifying the toolchain line with 'go get toolchain@something',
1838-
// and the go version is one that supports switching toolchains,
1839-
// and the toolchain running right now is newer than the current toolchain line,
1840-
// then update the toolchain line to record the newer toolchain.
1841-
//
1842-
// TODO(#57001): This condition feels too complicated. Can we simplify it?
1843-
// TODO(#57001): Add more tests for toolchain lines.
18441827
toolVers := gover.FromToolchain(toolchain)
1845-
if wroteGo && !opts.DropToolchain && !opts.ExplicitToolchain &&
1846-
gover.Compare(goVersion, gover.GoStrictVersion) >= 0 &&
1847-
(gover.Compare(gover.Local(), toolVers) > 0 && !gover.IsLang(gover.Local())) {
1848-
toolchain = "go" + gover.Local()
1849-
toolVers = gover.FromToolchain(toolchain)
1850-
}
1851-
18521828
if opts.DropToolchain || toolchain == "go"+goVersion || (gover.Compare(toolVers, gover.GoStrictVersion) < 0 && !opts.ExplicitToolchain) {
18531829
// go get toolchain@none or toolchain matches go line or isn't valid; drop it.
18541830
// TODO(#57001): 'go get' should reject explicit toolchains below GoStrictVersion.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ stderr '^go: rsc.io/[email protected]: module rsc.io/[email protected] requires go >= 1.
2121
go get .
2222
stderr '^go: module rsc.io/[email protected] requires go >= 1.999; switching to go1.999testmod$'
2323
stderr '^go: upgraded go 1.21 => 1.999$'
24-
stderr '^go: added toolchain go1.999testmod$'
24+
! stderr '^go: added toolchain'
2525

2626

2727
# Now, the various 'go mod' subcommands should succeed.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,4 @@ module example
102102

103103
go 1.23
104104

105-
toolchain go1.23.9
106-
107105
require rsc.io/needall v0.0.1

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ stderr '^go: rsc.io/[email protected] requires go >= 1.23; switching to go1.23.9$'
99
! stderr '\(running'
1010
stderr '^go: added rsc.io/needall v0.0.1'
1111
grep 'go 1.23' go.mod
12-
grep 'toolchain go1.23.9' go.mod
12+
! grep toolchain go.mod
1313

1414
# GOTOOLCHAIN=min+auto should run the newer toolchain
1515
env GOTOOLCHAIN=go1.21+auto
@@ -19,7 +19,7 @@ stderr '^go: rsc.io/[email protected] requires go >= 1.23; switching to go1.23.9$'
1919
! stderr '\(running'
2020
stderr '^go: added rsc.io/needall v0.0.1'
2121
grep 'go 1.23' go.mod
22-
grep 'toolchain go1.23.9' go.mod
22+
! grep toolchain go.mod
2323

2424
# GOTOOLCHAIN=go1.21 should NOT run the newer toolchain
2525
env GOTOOLCHAIN=go1.21
@@ -67,7 +67,7 @@ cp go.mod.new go.mod
6767
6868
stderr '^go: updating go.mod requires go >= 1.22rc1; switching to go1.22.9$'
6969
stderr '^go: upgraded go 1.1 => 1.22rc1$'
70-
stderr '^go: added toolchain go1.22.9$'
70+
! stderr '^go: added toolchain$'
7171

7272
# go get [email protected] should use 1.22.1 exactly, not a later release.
7373
env GOTOOLCHAIN=local
@@ -80,7 +80,7 @@ cp go.mod.new go.mod
8080
8181
stderr '^go: updating go.mod requires go >= 1.22.1; switching to go1.22.9$'
8282
stderr '^go: upgraded go 1.1 => 1.22.1$'
83-
stderr '^go: added toolchain go1.22.9$'
83+
! stderr '^go: added toolchain$'
8484

8585
# go get needgo122 (says 'go 1.22') should use 1.22.0, the earliest release we have available
8686
# (ignoring prereleases).
@@ -94,7 +94,7 @@ cp go.mod.new go.mod
9494
go get rsc.io/needgo122
9595
stderr '^go: upgraded go 1.1 => 1.22$'
9696
stderr '^go: rsc.io/[email protected] requires go >= 1.22; switching to go1.22.9$'
97-
stderr '^go: added toolchain go1.22.9$'
97+
! stderr '^go: added toolchain$'
9898

9999
# go get needgo1223 (says 'go 1.22.3') should use go 1.22.3
100100
env GOTOOLCHAIN=local
@@ -107,7 +107,7 @@ cp go.mod.new go.mod
107107
go get rsc.io/needgo1223
108108
stderr '^go: upgraded go 1.1 => 1.22.3$'
109109
stderr '^go: rsc.io/[email protected] requires go >= 1.22.3; switching to go1.22.9$'
110-
stderr '^go: added toolchain go1.22.9$'
110+
! stderr '^go: added toolchain$'
111111

112112
# go get needgo124 (says 'go 1.24') should use go 1.24rc1, the only version available
113113
env GOTOOLCHAIN=local
@@ -120,7 +120,7 @@ cp go.mod.new go.mod
120120
go get rsc.io/needgo124
121121
stderr '^go: rsc.io/[email protected] requires go >= 1.24; switching to go1.24rc1$'
122122
stderr '^go: upgraded go 1.1 => 1.24$'
123-
stderr '^go: added toolchain go1.24rc1$'
123+
! stderr '^go: added toolchain$'
124124

125125
# The -C flag should not happen more than once due to switching.
126126
mkdir dir dir/dir
@@ -132,7 +132,7 @@ cp p.go dir/dir/p.go
132132
go get -C dir rsc.io/needgo124
133133
stderr '^go: rsc.io/[email protected] requires go >= 1.24; switching to go1.24rc1$'
134134
stderr '^go: upgraded go 1.1 => 1.24$'
135-
stderr '^go: added toolchain go1.24rc1$'
135+
! stderr '^go: added toolchain1$'
136136
cmp go.mod.new go.mod
137137
cmp go.mod.new dir/dir/go.mod
138138
grep 'go 1.24$' dir/go.mod

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ cp go.mod.orig go.mod
77
go get go
88
stderr '^go: upgraded go 1.21 => 1.23.9$'
99
grep 'go 1.23.9' go.mod
10-
grep 'toolchain go1.99rc1' go.mod
10+
! grep toolchain go.mod
1111

1212
# go get [email protected] should use the latest Go 1.23
1313
cp go.mod.orig go.mod
1414
1515
stderr '^go: upgraded go 1.21 => 1.23.9$'
1616
grep 'go 1.23.9' go.mod
17-
grep 'toolchain go1.99rc1' go.mod
17+
! grep toolchain go.mod
1818

1919
# go get [email protected] should use the latest Go 1.22
2020
cp go.mod.orig go.mod
2121
2222
stderr '^go: upgraded go 1.21 => 1.22.9$'
2323
grep 'go 1.22.9' go.mod
24-
grep 'toolchain go1.99rc1' go.mod
24+
! grep toolchain1 go.mod
2525

2626
# go get go@patch should use the latest patch release
2727
2828
go get go@patch
2929
stderr '^go: upgraded go 1.22.1 => 1.22.9$'
3030
grep 'go 1.22.9' go.mod
31-
grep 'toolchain go1.99rc1' go.mod
31+
! grep toolchain go.mod
3232

3333
# go get [email protected] does NOT find the release candidate
3434
cp go.mod.orig go.mod
@@ -40,17 +40,22 @@ cp go.mod.orig go.mod
4040
4141
stderr '^go: upgraded go 1.21 => 1.24rc1$'
4242
grep 'go 1.24rc1' go.mod
43-
grep 'toolchain go1.99rc1' go.mod
43+
! grep toolchain go.mod
4444

4545
# go get go@latest finds the latest Go 1.23
4646
cp go.mod.orig go.mod
4747
go get go@latest
4848
stderr '^go: upgraded go 1.21 => 1.23.9$'
4949
grep 'go 1.23.9' go.mod
50-
grep 'toolchain go1.99rc1' go.mod
50+
! grep toolchain go.mod
5151

5252
# Again, with toolchains.
5353

54+
55+
stderr '^go: added toolchain go1.99rc1$'
56+
grep 'go 1.23.9' go.mod
57+
grep 'toolchain go1.99rc1' go.mod
58+
5459
# go get toolchain should find go1.999testmod.
5560
go get toolchain
5661
stderr '^go: upgraded toolchain go1.99rc1 => go1.999testmod$'

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This test verifies that 'go get' and 'go mod tidy' switch to a newer toolchain
2-
# if needed to process newly-reolved imports.
2+
# if needed to process newly-resolved imports.
33

44
env TESTGO_VERSION=go1.21.0
55
env TESTGO_VERSION_SWITCH=switch
@@ -46,7 +46,6 @@ go: trying upgrade to example.net/[email protected]
4646
go: accepting indirect upgrade from [email protected] to 1.22.0
4747
go: trying upgrade to example.net/[email protected]
4848
go: upgraded go 1.20 => 1.22.0
49-
go: added toolchain go1.22.9
5049
go: added example.net/b v0.1.0
5150
go: added example.net/c v0.1.0
5251
go: added example.net/d v0.1.0
@@ -67,7 +66,6 @@ go: trying upgrade to example.net/[email protected]
6766
go: trying upgrade to example.net/[email protected]
6867
go: accepting indirect upgrade from [email protected] to 1.23.0
6968
go: upgraded go 1.20 => 1.23.0
70-
go: added toolchain go1.23.9
7169
go: upgraded example.net/a v0.1.0 => v0.2.0
7270
go: added example.net/b v0.1.0
7371
go: added example.net/c v0.1.0
@@ -92,8 +90,6 @@ module example
9290

9391
go 1.22.0
9492

95-
toolchain go1.22.9
96-
9793
require (
9894
example.net/a v0.1.0
9995
example.net/b v0.1.0
@@ -117,8 +113,6 @@ module example
117113

118114
go 1.23.0
119115

120-
toolchain go1.23.9
121-
122116
require (
123117
example.net/a v0.2.0
124118
example.net/b v0.1.0

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ cmpenv go.mod go.mod.latest
131131

132132
cp go.mod.117 go.mod
133133
go mod tidy -go=1.21.0 # lower than $goversion
134-
cmpenv go.mod go.mod.121toolchain
134+
cmp go.mod go.mod.121toolchain
135135

136136

137137
-- go.mod --
@@ -334,8 +334,6 @@ module example.com/m
334334

335335
go 1.21.0
336336

337-
toolchain $TESTGO_VERSION
338-
339337
require example.net/a v0.1.0
340338

341339
require (

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ grep 'toolchain go1.22.1' go.mod
1818

1919
2020
stderr '^go: upgraded go 1.10 => 1.22.3$'
21-
stderr '^go: upgraded toolchain go1.22.1 => go1.100.0$'
21+
! stderr '^go: upgraded toolchain$'
2222
grep 'go 1.22.3' go.mod
2323

24+
25+
stderr '^go: added toolchain go1.100.0$'
26+
2427
2528
stderr '^go: removed toolchain go1.100.0$'
2629
! grep toolchain go.mod
@@ -65,6 +68,10 @@ stderr '^go: removed toolchain go1.23.9'
6568
! stderr ' go 1'
6669
grep 'go 1.23.5' go.mod
6770

71+
72+
73+
! grep toolchain go.mod
74+
6875
-- go.mod --
6976
module m
7077
go 1.10

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# go get should update the go and toolchain lines in go.work
1+
# go get should update the go line in go.work
22
env TESTGO_VERSION=go1.21
33
env TESTGO_VERSION_SWITCH=switch
44
env GOTOOLCHAIN=auto
@@ -9,8 +9,8 @@ stderr '^go: rsc.io/[email protected] requires go >= 1.23; switching to go1.23.9$'
99
stderr '^go: added rsc.io/needall v0.0.1'
1010
grep 'go 1.23$' go.mod
1111
grep 'go 1.23$' go.work
12-
grep 'toolchain go1.23.9' go.mod
13-
grep 'toolchain go1.23.9' go.work
12+
! grep toolchain go.mod
13+
! grep toolchain go.work
1414

1515
-- go.mod.new --
1616
module m

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ env GOTOOLCHAIN=auto
3434
go work sync
3535
stderr '^go: m1_22_1'${/}'go.mod requires go >= 1.22.1; switching to go1.22.9$'
3636
grep '^go 1.22.1$' go.work
37-
grep '^toolchain go1.22.9$' go.work
37+
! grep toolchain go.work
3838

3939
# work sync with newer modules should update go 1.22.1 -> 1.24rc1 and drop toolchain
4040
go work edit -use=./m1_24_rc0
4141
go work sync
4242
stderr '^go: m1_24_rc0'${/}'go.mod requires go >= 1.24rc0; switching to go1.24rc1$'
4343
cat go.work
4444
grep '^go 1.24rc0$' go.work
45-
grep '^toolchain go1.24rc1$' go.work
45+
! grep toolchain go.work

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ env GOTOOLCHAIN=auto
3232
go work use ./m1_22_0
3333
stderr '^go: m1_22_0'${/}'go.mod requires go >= 1.22.0; switching to go1.22.9$'
3434
grep '^go 1.22.0$' go.work
35-
grep '^toolchain go1.22.9$' go.work
35+
! grep toolchain go.work
3636

3737
# work use with an even newer module should bump go again.
3838
go work use ./m1_22_1
39-
! stderr switching
39+
stderr '^go: m1_22_1'${/}'go.mod requires go >= 1.22.1; switching to go1.22.9$'
4040
grep '^go 1.22.1$' go.work
41-
grep '^toolchain go1.22.9$' go.work # unchanged
41+
! grep toolchain go.work
4242

4343
# work use with an even newer module should bump go and toolchain again.
4444
env GOTOOLCHAIN=go1.22.9
@@ -48,4 +48,4 @@ env GOTOOLCHAIN=auto
4848
go work use ./m1_24_rc0
4949
stderr '^go: m1_24_rc0'${/}'go.mod requires go >= 1.24rc0; switching to go1.24rc1$'
5050
grep '^go 1.24rc0$' go.work
51-
grep '^toolchain go1.24rc1$' go.work
51+
! grep 'toolchain' go.work

0 commit comments

Comments
 (0)