Skip to content

Commit 25886cf

Browse files
author
Jay Conrod
committed
cmd/go: preserve sums for indirect deps fetched by 'go mod download'
Previously, commands that wrote go.sum (except 'go mod tidy') would retain sums for zip files of directly required modules. Sums of indirect dependencies wouldn't be retained unless they were used to load packages. With this change, sums for indirect dependencies will be retained if they're available. This allows users to add missing sums with 'go mod download example.com/mod', which previously only worked for directly required modules. Note that 'go mod download' without arguments now adds sums for every module in the build list. That matches 1.15 behavior. For #41103 Change-Id: I4cce2bf1c73578dae836bdb5adb32da071554f1a Reviewed-on: https://go-review.googlesource.com/c/go/+/282692 Trust: Jay Conrod <[email protected]> Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 6250833 commit 25886cf

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

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

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -976,9 +976,12 @@ func WriteGoMod() {
976976
// It also contains entries for go.mod files needed for MVS (the version
977977
// of these entries ends with "/go.mod").
978978
//
979-
// If addDirect is true, the set also includes sums for modules directly
980-
// required by go.mod, as represented by the index, with replacements applied.
981-
func keepSums(addDirect bool) map[module.Version]bool {
979+
// If keepBuildListZips is true, the set also includes sums for zip files for
980+
// all modules in the build list with replacements applied. 'go get' and
981+
// 'go mod download' may add sums to this set when adding a requirement on a
982+
// module without a root package or when downloading a direct or indirect
983+
// dependency.
984+
func keepSums(keepBuildListZips bool) map[module.Version]bool {
982985
// Re-derive the build list using the current list of direct requirements.
983986
// Keep the sum for the go.mod of each visited module version (or its
984987
// replacement).
@@ -1007,19 +1010,20 @@ func keepSums(addDirect bool) map[module.Version]bool {
10071010
panic(fmt.Sprintf("unexpected error reloading build list: %v", err))
10081011
}
10091012

1013+
actualMods := make(map[string]module.Version)
1014+
for _, m := range buildList[1:] {
1015+
if r := Replacement(m); r.Path != "" {
1016+
actualMods[m.Path] = r
1017+
} else {
1018+
actualMods[m.Path] = m
1019+
}
1020+
}
1021+
10101022
// Add entries for modules in the build list with paths that are prefixes of
10111023
// paths of loaded packages. We need to retain sums for modules needed to
10121024
// report ambiguous import errors. We use our re-derived build list,
10131025
// since the global build list may have been tidied.
10141026
if loaded != nil {
1015-
actualMods := make(map[string]module.Version)
1016-
for _, m := range buildList[1:] {
1017-
if r := Replacement(m); r.Path != "" {
1018-
actualMods[m.Path] = r
1019-
} else {
1020-
actualMods[m.Path] = m
1021-
}
1022-
}
10231027
for _, pkg := range loaded.pkgs {
10241028
if pkg.testOf != nil || pkg.inStd || module.CheckImportPath(pkg.path) != nil {
10251029
continue
@@ -1032,17 +1036,13 @@ func keepSums(addDirect bool) map[module.Version]bool {
10321036
}
10331037
}
10341038

1035-
// Add entries for modules directly required by go.mod.
1036-
if addDirect {
1037-
for m := range index.require {
1038-
var kept module.Version
1039-
if r := Replacement(m); r.Path != "" {
1040-
kept = r
1041-
} else {
1042-
kept = m
1043-
}
1044-
keep[kept] = true
1045-
keep[module.Version{Path: kept.Path, Version: kept.Version + "/go.mod"}] = true
1039+
// Add entries for the zip of each module in the build list.
1040+
// We might not need all of these (tidy does not add them), but they may be
1041+
// added by a specific 'go get' or 'go mod download' command to resolve
1042+
// missing import sum errors.
1043+
if keepBuildListZips {
1044+
for _, m := range actualMods {
1045+
keep[m] = true
10461046
}
10471047
}
10481048

@@ -1062,9 +1062,8 @@ func (r *keepSumReqs) Required(m module.Version) ([]module.Version, error) {
10621062
}
10631063

10641064
func TrimGoSum() {
1065-
// Don't retain sums for direct requirements in go.mod. When TrimGoSum is
1066-
// called, go.mod has not been updated, and it may contain requirements on
1067-
// modules deleted from the build list.
1068-
addDirect := false
1069-
modfetch.TrimGoSum(keepSums(addDirect))
1065+
// Don't retain sums for the zip file of every module in the build list.
1066+
// We may not need them all to build the main module's packages.
1067+
keepBuildListZips := false
1068+
modfetch.TrimGoSum(keepSums(keepBuildListZips))
10701069
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ go mod tidy
1010
grep '^example.com/ambiguous/a v1.0.0 h1:' go.sum
1111
grep '^example.com/ambiguous/a/b v0.0.0-empty h1:' go.sum
1212

13+
# 'go mod download' should also add sums.
14+
cp go.sum.buildlist-only go.sum
15+
go mod download example.com/ambiguous/a
16+
grep '^example.com/ambiguous/a v1.0.0 h1:' go.sum
17+
! grep '^example.com/ambiguous/a/b v0.0.0-empty h1:' go.sum
18+
go mod download example.com/ambiguous/a/b
19+
grep '^example.com/ambiguous/a/b v0.0.0-empty h1:' go.sum
20+
1321
# If two modules could provide a package, and we're missing a sum for one,
1422
# we should see a missing sum error, even if we have a sum for a module that
1523
# provides the package.

0 commit comments

Comments
 (0)