aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modcmd/mod.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2018-07-18 15:36:04 -0400
committerRuss Cox <rsc@golang.org>2018-07-19 18:15:55 +0000
commitef45945718c2374234879ee184c9af126937cb2c (patch)
treeab9c8ade36e3c3e1d8365e0edc71487c37d9652d /src/cmd/go/internal/modcmd/mod.go
parent764d0bb0314c327a5149160ca77de9873dafb0fc (diff)
downloadgo-ef45945718c2374234879ee184c9af126937cb2c.tar.gz
go-ef45945718c2374234879ee184c9af126937cb2c.zip
cmd/go: scrub go.sum during go mod -sync
go.sum accumulates cruft as modules are added and removed as direct and indirect dependencies. Instead of exposing all that cruft, let "go mod -sync" clean it out. Fixes #26381. Change-Id: I7c9534cf7cc4579f7f82646d00ff691c87a13c4a Reviewed-on: https://go-review.googlesource.com/124713 Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/go/internal/modcmd/mod.go')
-rw-r--r--src/cmd/go/internal/modcmd/mod.go26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/cmd/go/internal/modcmd/mod.go b/src/cmd/go/internal/modcmd/mod.go
index b2769fd5d6..d01f699d88 100644
--- a/src/cmd/go/internal/modcmd/mod.go
+++ b/src/cmd/go/internal/modcmd/mod.go
@@ -14,6 +14,7 @@ import (
"strings"
"cmd/go/internal/base"
+ "cmd/go/internal/modfetch"
"cmd/go/internal/modfile"
"cmd/go/internal/modload"
"cmd/go/internal/module"
@@ -154,7 +155,8 @@ effectively imply 'go mod -fix'.
The -sync flag synchronizes go.mod with the source code in the module.
It adds any missing modules necessary to build the current module's
packages and dependencies, and it removes unused modules that
-don't provide any relevant packages.
+don't provide any relevant packages. It also adds any missing entries
+to go.sum and removes any unnecessary ones.
The -vendor flag resets the module's vendor directory to include all
packages needed to build and test all the module's packages.
@@ -291,6 +293,7 @@ func runMod(cmd *base.Command, args []string) {
}
}
modload.SetBuildList(keep)
+ modSyncGoSum() // updates memory copy; WriteGoMod on next line flushes it out
}
modload.WriteGoMod()
if *modVendor {
@@ -530,3 +533,24 @@ func modPrintGraph() {
}
w.Flush()
}
+
+// modSyncGoSum resets the go.sum file content
+// to be exactly what's needed for the current go.mod.
+func modSyncGoSum() {
+ // Assuming go.sum already has at least enough from the successful load,
+ // we only have to tell modfetch what needs keeping.
+ reqs := modload.Reqs()
+ keep := make(map[module.Version]bool)
+ var walk func(module.Version)
+ walk = func(m module.Version) {
+ keep[m] = true
+ list, _ := reqs.Required(m)
+ for _, r := range list {
+ if !keep[r] {
+ walk(r)
+ }
+ }
+ }
+ walk(modload.Target)
+ modfetch.TrimGoSum(keep)
+}