github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-internal/modcmd/tidy.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // go mod tidy
     6  
     7  package modcmd
     8  
     9  import (
    10  	"github.com/gagliardetto/golang-go/cmd/go/not-internal/base"
    11  	"github.com/gagliardetto/golang-go/cmd/go/not-internal/cfg"
    12  	"github.com/gagliardetto/golang-go/cmd/go/not-internal/modfetch"
    13  	"github.com/gagliardetto/golang-go/cmd/go/not-internal/modload"
    14  	"github.com/gagliardetto/golang-go/cmd/go/not-internal/work"
    15  
    16  	"golang.org/x/mod/module"
    17  )
    18  
    19  var cmdTidy = &base.Command{
    20  	UsageLine: "go mod tidy [-v]",
    21  	Short:     "add missing and remove unused modules",
    22  	Long: `
    23  Tidy makes sure go.mod matches the source code in the module.
    24  It adds any missing modules necessary to build the current module's
    25  packages and dependencies, and it removes unused modules that
    26  don't provide any relevant packages. It also adds any missing entries
    27  to go.sum and removes any unnecessary ones.
    28  
    29  The -v flag causes tidy to print information about removed modules
    30  to standard error.
    31  	`,
    32  }
    33  
    34  func init() {
    35  	cmdTidy.Run = runTidy // break init cycle
    36  	cmdTidy.Flag.BoolVar(&cfg.BuildV, "v", false, "")
    37  	work.AddModCommonFlags(cmdTidy)
    38  }
    39  
    40  func runTidy(cmd *base.Command, args []string) {
    41  	if len(args) > 0 {
    42  		base.Fatalf("go mod tidy: no arguments allowed")
    43  	}
    44  
    45  	modload.LoadALL()
    46  	modload.TidyBuildList()
    47  	modTidyGoSum() // updates memory copy; WriteGoMod on next line flushes it out
    48  	modload.WriteGoMod()
    49  }
    50  
    51  // modTidyGoSum resets the go.sum file content
    52  // to be exactly what's needed for the current go.mod.
    53  func modTidyGoSum() {
    54  	// Assuming go.sum already has at least enough from the successful load,
    55  	// we only have to tell modfetch what needs keeping.
    56  	reqs := modload.Reqs()
    57  	keep := make(map[module.Version]bool)
    58  	replaced := make(map[module.Version]bool)
    59  	var walk func(module.Version)
    60  	walk = func(m module.Version) {
    61  		// If we build using a replacement module, keep the sum for the replacement,
    62  		// since that's the code we'll actually use during a build.
    63  		//
    64  		// TODO(golang.org/issue/29182): Perhaps we should keep both sums, and the
    65  		// sums for both sets of transitive requirements.
    66  		r := modload.Replacement(m)
    67  		if r.Path == "" {
    68  			keep[m] = true
    69  		} else {
    70  			keep[r] = true
    71  			replaced[m] = true
    72  		}
    73  		list, _ := reqs.Required(m)
    74  		for _, r := range list {
    75  			if !keep[r] && !replaced[r] {
    76  				walk(r)
    77  			}
    78  		}
    79  	}
    80  	walk(modload.Target)
    81  	modfetch.TrimGoSum(keep)
    82  }