Skip to content

Commit 0ece95a

Browse files
author
Jay Conrod
committed
cmd/go: don't let 'go mod download' save sums for inconsistent requirements
'go mod download' calls modload.LoadModFile early to find the main module path in order to validate arguments. LoadModFile may write go.mod and go.sum to fix formatting and add a go directive. This calls keepSums, which, in eager mode, loaded the complete module graph in order to find out what sums are needed to load the complete module graph. If go.mod requires a lower version of a module than will be selected later, keepSums causes the sum for that version's go.mod to be retained, even though it isn't needed later after a consistent go.mod is written. This CL fixes keepSums not to load the graph if it hasn't already been loaded (whether eager or lazy), addressing comments from CL 318629. For #45332 Change-Id: I20d4404004e4ad335450fd0fd753e7bc0060f702 Reviewed-on: https://go-review.googlesource.com/c/go/+/322369 Trust: Jay Conrod <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent cdcd028 commit 0ece95a

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
138138
sem := make(chan token, runtime.GOMAXPROCS(0))
139139
infos, infosErr := modload.ListModules(ctx, args, 0)
140140
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)
141+
// 'go mod download' is sometimes run without arguments to pre-populate the
142+
// module cache. It may fetch modules that aren't needed to build packages
143+
// in the main mdoule. This is usually not intended, so don't save sums for
144+
// downloaded modules (golang.org/issue/45332).
145+
// TODO(golang.org/issue/45551): For now, in ListModules, save sums needed
146+
// to load the build list (same as 1.15 behavior). In the future, report an
147+
// error if go.mod or go.sum need to be updated after loading the build
148+
// list.
149149
modload.DisallowWriteGoMod()
150150
}
151151

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -1122,12 +1122,11 @@ func keepSums(ctx context.Context, ld *loader, rs *Requirements, which whichSums
11221122
}
11231123
}
11241124

1125-
if rs.depth == lazy && rs.graph.Load() == nil {
1126-
// The main module is lazy and we haven't needed to load the module graph so
1127-
// far. Don't incur the cost of loading it now — since we haven't loaded the
1128-
// graph, we probably don't have any checksums to contribute to the distant
1129-
// parts of the graph anyway. Instead, just request sums for the roots that
1130-
// we know about.
1125+
if rs.graph.Load() == nil {
1126+
// The module graph was not loaded, possibly because the main module is lazy
1127+
// or possibly because we haven't needed to load the graph yet.
1128+
// Save sums for the root modules (or their replacements), but don't
1129+
// incur the cost of loading the graph just to find and retain the sums.
11311130
for _, m := range rs.rootModules {
11321131
r := resolveReplacement(m)
11331132
keep[modkey(r)] = true

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

-1
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,4 @@ require (
167167
-- update/go.sum.update --
168168
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
169169
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
170-
rsc.io/sampler v1.2.1/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
171170
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

0 commit comments

Comments
 (0)