Skip to content

Commit

Permalink
cmd/go: make commands other than 'tidy' prune go.mod less agressively
Browse files Browse the repository at this point in the history
Updates #31870
Updates #33326
Fixes #34822

Change-Id: I1337f171133c20800eacc6b0955ede5a394ea7eb
Reviewed-on: https://go-review.googlesource.com/c/go/+/204878
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
  • Loading branch information
Bryan C. Mills committed Nov 6, 2019
1 parent 1bd974e commit cf3be9b
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 120 deletions.
27 changes: 2 additions & 25 deletions src/cmd/go/internal/modcmd/tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
package modcmd

import (
"fmt"
"os"

"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/modfetch"
Expand Down Expand Up @@ -45,28 +42,8 @@ func runTidy(cmd *base.Command, args []string) {
base.Fatalf("go mod tidy: no arguments allowed")
}

// LoadALL adds missing modules.
// Remove unused modules.
used := make(map[module.Version]bool)
for _, pkg := range modload.LoadALL() {
used[modload.PackageModule(pkg)] = true
}
used[modload.Target] = true // note: LoadALL initializes Target

inGoMod := make(map[string]bool)
for _, r := range modload.ModFile().Require {
inGoMod[r.Mod.Path] = true
}

var keep []module.Version
for _, m := range modload.BuildList() {
if used[m] {
keep = append(keep, m)
} else if cfg.BuildV && inGoMod[m.Path] {
fmt.Fprintf(os.Stderr, "unused %s\n", m.Path)
}
}
modload.SetBuildList(keep)
modload.LoadALL()
modload.TidyBuildList()
modTidyGoSum() // updates memory copy; WriteGoMod on next line flushes it out
modload.WriteGoMod()
}
Expand Down
Loading

0 comments on commit cf3be9b

Please sign in to comment.