Skip to content

Commit b9d5a25

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cmd/go: save zip sums for downloaded modules in 'go mod download' in a workspace
Within a single module we expect all needed checksums to have already been recorded by a previous call to 'go get' or 'go mod tidy' in that module. However, when we combine multiple modules in a workspace, they may upgrade each other's dependencies, so a given module might be upgraded above the highest version recorded in the individual go.sum files for the workspace modules. Since the checksums might not be present in individual go.sum files, record them in go.work.sum. Fixes #51946. Change-Id: Icb4ea874b9e5c5b1950d42650974a24b5d6543d4 Reviewed-on: https://go-review.googlesource.com/c/go/+/417654 Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Bryan Mills <[email protected]>
1 parent a906d3d commit b9d5a25

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

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

+19-7
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,13 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
206206
type token struct{}
207207
sem := make(chan token, runtime.GOMAXPROCS(0))
208208
infos, infosErr := modload.ListModules(ctx, args, 0, *downloadReuse)
209-
if !haveExplicitArgs {
209+
if !haveExplicitArgs && modload.WorkFilePath() == "" {
210210
// 'go mod download' is sometimes run without arguments to pre-populate the
211-
// module cache. It may fetch modules that aren't needed to build packages
212-
// in the main module. This is usually not intended, so don't save sums for
213-
// downloaded modules (golang.org/issue/45332). We do still fix
214-
// inconsistencies in go.mod though.
211+
// module cache. In modules that aren't at go 1.17 or higher, it may fetch
212+
// modules that aren't needed to build packages in the main module. This is
213+
// usually not intended, so don't save sums for downloaded modules
214+
// (golang.org/issue/45332). We do still fix inconsistencies in go.mod
215+
// though.
215216
//
216217
// TODO(#45551): In the future, report an error if go.mod or go.sum need to
217218
// be updated after loading the build list. This may require setting
@@ -282,8 +283,19 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
282283
// 'go get mod@version', which may have other side effects. We print this in
283284
// some error message hints.
284285
//
285-
// Don't save sums for 'go mod download' without arguments; see comment above.
286-
if haveExplicitArgs {
286+
// If we're in workspace mode, update go.work.sum with checksums for all of
287+
// the modules we downloaded that aren't already recorded. Since a requirement
288+
// in one module may upgrade a dependency of another, we can't be sure that
289+
// the import graph matches the import graph of any given module in isolation,
290+
// so we may end up needing to load packages from modules that wouldn't
291+
// otherwise be relevant.
292+
//
293+
// TODO(#44435): If we adjust the set of modules downloaded in workspace mode,
294+
// we may also need to adjust the logic for saving checksums here.
295+
//
296+
// Don't save sums for 'go mod download' without arguments unless we're in
297+
// workspace mode; see comment above.
298+
if haveExplicitArgs || modload.WorkFilePath() != "" {
287299
if err := modload.WriteGoMod(ctx); err != nil {
288300
base.Errorf("go: %v", err)
289301
}

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.mod
77
exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.zip
88
! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.info
99
! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.mod
10+
grep '^rsc\.io/quote v1\.5\.2/go\.mod h1:' go.work.sum
11+
grep '^rsc\.io/quote v1\.5\.2 h1:' go.work.sum
1012

13+
go clean -modcache
14+
rm go.work.sum
1115
go mod download
1216
exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.info
1317
exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.mod
1418
exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.zip
1519
! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.info
1620
! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.mod
21+
grep '^rsc\.io/quote v1\.5\.2/go\.mod h1:' go.work.sum
22+
grep '^rsc\.io/quote v1\.5\.2 h1:' go.work.sum
1723

1824
go mod why rsc.io/quote
1925
stdout '# rsc.io/quote\nexample.com/a\nrsc.io/quote'
@@ -25,8 +31,8 @@ stdout 'example.com/a rsc.io/[email protected]\nexample.com/b example.com/[email protected]\nr
2531
go 1.18
2632

2733
use (
28-
./a
29-
./b
34+
./a
35+
./b
3036
)
3137
-- a/go.mod --
3238
go 1.18

0 commit comments

Comments
 (0)