Skip to content

Commit 4fb10b2

Browse files
author
Jay Conrod
committed
cmd/go: in 'go mod download' without args, don't save module zip sums
'go mod download' without arguments is frequently used to populate the module cache. It tends to fetch a lot of extra files (for modules in the build list that aren't needed to build packages in the main module). It's annoying when sums are written for these extra files. 'go mod download mod@version' will still write sums for specific modules in the build list. 'go mod download all' still has the previous behavior. For now, all invocations of 'go mod download' still update go.mod and go.sum with changes needed to load the build list (1.15 behavior). Fixes #45332 Change-Id: I9e17d18a7466ac7271a0e1a2b663f6b3cb168c97 Reviewed-on: https://go-review.googlesource.com/c/go/+/318629 Trust: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 4fda54c commit 4fb10b2

File tree

7 files changed

+85
-17
lines changed

7 files changed

+85
-17
lines changed

doc/go1.17.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ <h4 id="password-prompts">Password prompts</h4>
186186
password-protected SSH keys.
187187
</p>
188188

189+
<h4 id="go-mod-download"><code>go</code> <code>mod</code> <code>download</code></h4>
190+
191+
<p><!-- golang.org/issue/45332 -->
192+
When <code>go</code> <code>mod</code> <code>download</code> is invoked without
193+
arguments, it will no longer save sums for downloaded module content to
194+
<code>go.sum</code>. It may still make changes to <code>go.mod</code> and
195+
<code>go.sum</code> needed to load the build list. This is the same as the
196+
behavior in Go 1.15. To save sums for all modules, use <code>go</code>
197+
<code>mod</code> <code>download</code> <code>all</code>.
198+
</p>
199+
189200
<p><!-- CL 249759 -->
190201
TODO: <a href="https://golang.org/cl/249759">https://golang.org/cl/249759</a>: cmd/cover: replace code using optimized golang.org/x/tools/cover
191202
</p>

src/cmd/go/internal/modcmd/download.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
8686
if !modload.HasModRoot() && len(args) == 0 {
8787
base.Fatalf("go mod download: no modules specified (see 'go help mod download')")
8888
}
89-
if len(args) == 0 {
89+
haveExplicitArgs := len(args) > 0
90+
if !haveExplicitArgs {
9091
args = []string{"all"}
91-
} else if modload.HasModRoot() {
92+
}
93+
if modload.HasModRoot() {
9294
modload.LoadModFile(ctx) // to fill Target
9395
targetAtUpgrade := modload.Target.Path + "@upgrade"
9496
targetAtPatch := modload.Target.Path + "@patch"
@@ -135,6 +137,18 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
135137
type token struct{}
136138
sem := make(chan token, runtime.GOMAXPROCS(0))
137139
infos, infosErr := modload.ListModules(ctx, args, 0)
140+
if !haveExplicitArgs {
141+
// 'go mod download' is sometimes run without arguments to pre-populate
142+
// the module cache. It may fetch modules that aren't needed to build
143+
// packages in the main mdoule. This is usually not intended, so don't save
144+
// sums for downloaded modules (golang.org/issue/45332).
145+
// TODO(golang.org/issue/45551): For now, save sums needed to load the
146+
// build list (same as 1.15 behavior). In the future, report an error if
147+
// go.mod or go.sum need to be updated after loading the build list.
148+
modload.WriteGoMod(ctx)
149+
modload.DisallowWriteGoMod()
150+
}
151+
138152
for _, info := range infos {
139153
if info.Replace != nil {
140154
info = info.Replace
@@ -185,8 +199,15 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
185199
base.ExitIfErrors()
186200
}
187201

188-
// Update go.mod and especially go.sum if needed.
189-
modload.WriteGoMod(ctx)
202+
// If there were explicit arguments, update go.mod and especially go.sum.
203+
// 'go mod download mod@version' is a useful way to add a sum without using
204+
// 'go get mod@version', which may have other side effects. We print this in
205+
// some error message hints.
206+
//
207+
// Don't save sums for 'go mod download' without arguments; see comment above.
208+
if haveExplicitArgs {
209+
modload.WriteGoMod(ctx)
210+
}
190211

191212
// If there was an error matching some of the requested packages, emit it now
192213
// (after we've written the checksums for the modules that were downloaded

src/cmd/go/testdata/mod/rsc.io_sampler_v1.2.1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module "rsc.io/sampler"
55

66
require "golang.org/x/text" v0.0.0-20170915032832-14c0d48ead0c
77
-- .info --
8-
{"Version":"v1.2.1","Name":"cac3af4f8a0ab40054fa6f8d423108a63a1255bb","Short":"cac3af4f8a0a","Time":"2018-02-13T18:16:22Z"}EOF
8+
{"Version":"v1.2.1","Name":"cac3af4f8a0ab40054fa6f8d423108a63a1255bb","Short":"cac3af4f8a0a","Time":"2018-02-13T18:16:22Z"}
99
-- hello.go --
1010
// Copyright 2018 The Go Authors. All rights reserved.
1111
// Use of this source code is governed by a BSD-style

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,28 @@ stderr '^go mod download: skipping argument m that resolves to the main module\n
107107
! go mod download m@latest
108108
stderr '^go mod download: m@latest: malformed module path "m": missing dot in first path element$'
109109

110-
# download updates go.mod and populates go.sum
110+
# download without arguments updates go.mod and go.sum after loading the
111+
# build list, but does not save sums for downloaded zips.
111112
cd update
113+
cp go.mod.orig go.mod
112114
! exists go.sum
113115
go mod download
116+
cmp go.mod.update go.mod
117+
cmp go.sum.update go.sum
118+
cp go.mod.orig go.mod
119+
rm go.sum
120+
121+
# download with arguments (even "all") does update go.mod and go.sum.
122+
go mod download rsc.io/sampler
123+
cmp go.mod.update go.mod
114124
grep '^rsc.io/sampler v1.3.0 ' go.sum
115-
go list -m rsc.io/sampler
116-
stdout '^rsc.io/sampler v1.3.0$'
125+
cp go.mod.orig go.mod
126+
rm go.sum
127+
128+
go mod download all
129+
cmp go.mod.update go.mod
130+
grep '^rsc.io/sampler v1.3.0 ' go.sum
131+
cd ..
117132

118133
# allow go mod download without go.mod
119134
env GO111MODULE=auto
@@ -131,7 +146,7 @@ stderr 'get '$GOPROXY
131146
-- go.mod --
132147
module m
133148

134-
-- update/go.mod --
149+
-- update/go.mod.orig --
135150
module m
136151

137152
go 1.16
@@ -140,3 +155,17 @@ require (
140155
rsc.io/quote v1.5.2
141156
rsc.io/sampler v1.2.1 // older version than in build list
142157
)
158+
-- update/go.mod.update --
159+
module m
160+
161+
go 1.16
162+
163+
require (
164+
rsc.io/quote v1.5.2
165+
rsc.io/sampler v1.3.0 // older version than in build list
166+
)
167+
-- update/go.sum.update --
168+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
169+
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
170+
rsc.io/sampler v1.2.1/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
171+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# Populate go.sum
2-
go mod download
3-
41
# go list should succeed to load a package ending with ".go" if the path does
52
# not correspond to an existing local file. Listing a pattern ending with
63
# ".go/" should try to list a package regardless of whether a file exists at the
@@ -31,3 +28,10 @@ module m
3128
go 1.13
3229

3330
require example.com/dotgo.go v1.0.0
31+
-- go.sum --
32+
example.com/dotgo.go v1.0.0 h1:XKJfs0V8x2PvY2tX8bJBCEbCDLnt15ma2onwhVpew/I=
33+
example.com/dotgo.go v1.0.0/go.mod h1:Qi6z/X3AC5vHiuMt6HF2ICx3KhIBGrMdrA7YoPDKqR0=
34+
-- use.go --
35+
package use
36+
37+
import _ "example.com/dotgo.go"

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
env GO111MODULE=on
22

3-
# Populate go.sum.
43
# TODO(golang.org/issue/41297): we shouldn't need go.sum. None of the commands
54
# below depend on the build list.
6-
go mod download
75

86
go list -m -versions rsc.io/quote
97
stdout '^rsc.io/quote v1.0.0 v1.1.0 v1.2.0 v1.2.1 v1.3.0 v1.4.0 v1.5.0 v1.5.1 v1.5.2 v1.5.3-pre1$'
@@ -36,6 +34,9 @@ stdout 'no matching versions for query ">v1.5.3"'
3634
module x
3735
require rsc.io/quote v1.0.0
3836

37+
-- go.sum --
38+
rsc.io/quote v1.0.0 h1:kQ3IZQzPTiDJxSZI98YaWgxFEhlNdYASHvh+MplbViw=
39+
rsc.io/quote v1.0.0/go.mod h1:v83Ri/njykPcgJltBc/gEkJTmjTsNgtO1Y7vyIK1CQA=
3940
-- use.go --
4041
package use
4142

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
cp go.mod go.mod.orig
22

3-
# Populate go.sum.
4-
go mod download
5-
63
# 'go list pkg' does not report an error when a retracted version is used.
74
go list -e -f '{{if .Error}}{{.Error}}{{end}}' ./use
85
! stdout .
@@ -32,6 +29,11 @@ go 1.15
3229

3330
require example.com/retract v1.0.0-bad
3431

32+
-- go.sum --
33+
example.com/retract v1.0.0-bad h1:liAW69rbtjY67x2CcNzat668L/w+YGgNX3lhJsWIJis=
34+
example.com/retract v1.0.0-bad/go.mod h1:0DvGGofJ9hr1q63cBrOY/jSY52OwhRGA0K47NE80I5Y=
35+
example.com/retract/self/prev v1.1.0 h1:0/8I/GTG+1eJTFeDQ/fUbgrMsVHHyKhh3Z8DSZp1fuA=
36+
example.com/retract/self/prev v1.1.0/go.mod h1:xl2EcklWuZZHVtHWcpzfSJQmnzAGpKZYpA/Wto7SZN4=
3537
-- use/use.go --
3638
package use
3739

0 commit comments

Comments
 (0)