"strings"
"cmd/go/internal/base"
+ "cmd/go/internal/modfetch"
"cmd/go/internal/modfile"
"cmd/go/internal/modload"
"cmd/go/internal/module"
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.
}
}
modload.SetBuildList(keep)
+ modSyncGoSum() // updates memory copy; WriteGoMod on next line flushes it out
}
modload.WriteGoMod()
if *modVendor {
}
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)
+}
os.Remove(goSum.modverify)
}
}
+
+// TrimGoSum trims go.sum to contain only the modules for which keep[m] is true.
+func TrimGoSum(keep map[module.Version]bool) {
+ goSum.mu.Lock()
+ defer goSum.mu.Unlock()
+ if !initGoSum() {
+ return
+ }
+
+ for m := range goSum.m {
+ // If we're keeping x@v we also keep x@v/go.mod.
+ // Map x@v/go.mod back to x@v for the keep lookup.
+ noGoMod := module.Version{Path: m.Path, Version: strings.TrimSuffix(m.Version, "/go.mod")}
+ if !keep[m] && !keep[noGoMod] {
+ delete(goSum.m, m)
+ }
+ }
+}
--- /dev/null
+env GO111MODULE=on
+
+# go.sum should list directly used modules and dependencies
+go get rsc.io/quote@v1.5.2
+go mod -sync
+grep rsc.io/sampler go.sum
+
+# go.sum should not normally lose old entries
+go get rsc.io/quote@v1.0.0
+grep 'rsc.io/quote v1.0.0' go.sum
+grep 'rsc.io/quote v1.5.2' go.sum
+grep rsc.io/sampler go.sum
+
+# go mod -sync should clear dead entries from go.sum
+go mod -sync
+grep 'rsc.io/quote v1.0.0' go.sum
+! grep 'rsc.io/quote v1.5.2' go.sum
+! grep rsc.io/sampler go.sum
+
+# go.sum with no entries is OK to keep
+# (better for version control not to delete and recreate.)
+cp x.go.noimports x.go
+go mod -sync
+exists go.sum
+! grep . go.sum
+
+-- go.mod --
+module x
+-- x.go --
+package x
+import _ "rsc.io/quote"
+-- x.go.noimports --
+package x