Skip to content

Commit bdad428

Browse files
author
Jay Conrod
committed
cmd/go: make 'go get' preserve sums for content of new requirements
This preserves zip sums when 'go get' is run on a module that does not have a package in the root directory. The zip must be fetched to determine whether the package should be loaded, so we already load and verify the sum. Note that 'go mod tidy' may still remove these sums, since they aren't needed to load packages. Fixes #41103 Change-Id: I78f10a25f0392461fdc98518a7c92a38ee3233c3 Reviewed-on: https://go-review.googlesource.com/c/go/+/251880 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 5183696 commit bdad428

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -862,14 +862,11 @@ func WriteGoMod() {
862862
}
863863
}
864864

865-
// Always update go.sum, even if we didn't change go.mod: we may have
866-
// downloaded modules that we didn't have before.
867-
modfetch.WriteGoSum(keepSums())
868-
869865
if !dirty && cfg.CmdName != "mod tidy" {
870866
// The go.mod file has the same semantic content that it had before
871867
// (but not necessarily the same exact bytes).
872-
// Ignore any intervening edits.
868+
// Don't write go.mod, but write go.sum in case we added or trimmed sums.
869+
modfetch.WriteGoSum(keepSums(true))
873870
return
874871
}
875872

@@ -880,6 +877,9 @@ func WriteGoMod() {
880877
defer func() {
881878
// At this point we have determined to make the go.mod file on disk equal to new.
882879
index = indexModFile(new, modFile, false)
880+
881+
// Update go.sum after releasing the side lock and refreshing the index.
882+
modfetch.WriteGoSum(keepSums(true))
883883
}()
884884

885885
// Make a best-effort attempt to acquire the side lock, only to exclude
@@ -920,7 +920,10 @@ func WriteGoMod() {
920920
// the last load function like ImportPaths, LoadALL, etc.). It also contains
921921
// entries for go.mod files needed for MVS (the version of these entries
922922
// ends with "/go.mod").
923-
func keepSums() map[module.Version]bool {
923+
//
924+
// If addDirect is true, the set also includes sums for modules directly
925+
// required by go.mod, as represented by the index, with replacements applied.
926+
func keepSums(addDirect bool) map[module.Version]bool {
924927
// Walk the module graph and keep sums needed by MVS.
925928
modkey := func(m module.Version) module.Version {
926929
return module.Version{Path: m.Path, Version: m.Version + "/go.mod"}
@@ -932,9 +935,6 @@ func keepSums() map[module.Version]bool {
932935
walk = func(m module.Version) {
933936
// If we build using a replacement module, keep the sum for the replacement,
934937
// since that's the code we'll actually use during a build.
935-
//
936-
// TODO(golang.org/issue/29182): Perhaps we should keep both sums, and the
937-
// sums for both sets of transitive requirements.
938938
r := Replacement(m)
939939
if r.Path == "" {
940940
keep[modkey(m)] = true
@@ -964,9 +964,27 @@ func keepSums() map[module.Version]bool {
964964
}
965965
}
966966

967+
// Add entries for modules directly required by go.mod.
968+
if addDirect {
969+
for m := range index.require {
970+
var kept module.Version
971+
if r := Replacement(m); r.Path != "" {
972+
kept = r
973+
} else {
974+
kept = m
975+
}
976+
keep[kept] = true
977+
keep[module.Version{Path: kept.Path, Version: kept.Version + "/go.mod"}] = true
978+
}
979+
}
980+
967981
return keep
968982
}
969983

970984
func TrimGoSum() {
971-
modfetch.TrimGoSum(keepSums())
985+
// Don't retain sums for direct requirements in go.mod. When TrimGoSum is
986+
// called, go.mod has not been updated, and it may contain requirements on
987+
// modules deleted from the build list.
988+
addDirect := false
989+
modfetch.TrimGoSum(keepSums(addDirect))
972990
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# When 'go get' is invoked on a module without a package in the root directory,
2+
# it should add sums for the module's go.mod file and its content to go.sum.
3+
# Verifies golang.org/issue/41103.
4+
go mod init m
5+
go get rsc.io/QUOTE
6+
grep '^rsc.io/QUOTE v1.5.2/go.mod ' go.sum
7+
grep '^rsc.io/QUOTE v1.5.2 ' go.sum
8+
9+
# Double-check rsc.io/QUOTE does not have a root package.
10+
! go list -mod=readonly rsc.io/QUOTE
11+
stderr '^cannot find module providing package rsc.io/QUOTE: import lookup disabled by -mod=readonly$'

0 commit comments

Comments
 (0)