Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/go/internal/modcmd/tidy.go

Documentation: cmd/go/internal/modcmd

     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  	"cmd/go/internal/base"
    11  	"cmd/go/internal/cfg"
    12  	"cmd/go/internal/imports"
    13  	"cmd/go/internal/modload"
    14  	"context"
    15  )
    16  
    17  var cmdTidy = &base.Command{
    18  	UsageLine: "go mod tidy [-e] [-v]",
    19  	Short:     "add missing and remove unused modules",
    20  	Long: `
    21  Tidy makes sure go.mod matches the source code in the module.
    22  It adds any missing modules necessary to build the current module's
    23  packages and dependencies, and it removes unused modules that
    24  don't provide any relevant packages. It also adds any missing entries
    25  to go.sum and removes any unnecessary ones.
    26  
    27  The -v flag causes tidy to print information about removed modules
    28  to standard error.
    29  
    30  The -e flag causes tidy to attempt to proceed despite errors
    31  encountered while loading packages.
    32  
    33  See https://golang.org/ref/mod#go-mod-tidy for more about 'go mod tidy'.
    34  	`,
    35  	Run: runTidy,
    36  }
    37  
    38  var tidyE bool // if true, report errors but proceed anyway.
    39  
    40  func init() {
    41  	cmdTidy.Flag.BoolVar(&cfg.BuildV, "v", false, "")
    42  	cmdTidy.Flag.BoolVar(&tidyE, "e", false, "")
    43  	base.AddModCommonFlags(&cmdTidy.Flag)
    44  }
    45  
    46  func runTidy(ctx context.Context, cmd *base.Command, args []string) {
    47  	if len(args) > 0 {
    48  		base.Fatalf("go mod tidy: no arguments allowed")
    49  	}
    50  
    51  	// Tidy aims to make 'go test' reproducible for any package in 'all', so we
    52  	// need to include test dependencies. For modules that specify go 1.15 or
    53  	// earlier this is a no-op (because 'all' saturates transitive test
    54  	// dependencies).
    55  	//
    56  	// However, with lazy loading (go 1.16+) 'all' includes only the packages that
    57  	// are transitively imported by the main module, not the test dependencies of
    58  	// those packages. In order to make 'go test' reproducible for the packages
    59  	// that are in 'all' but outside of the main module, we must explicitly
    60  	// request that their test dependencies be included.
    61  	modload.ForceUseModules = true
    62  	modload.RootMode = modload.NeedRoot
    63  
    64  	modload.LoadPackages(ctx, modload.PackageOpts{
    65  		Tags:                  imports.AnyTags(),
    66  		ResolveMissingImports: true,
    67  		LoadTests:             true,
    68  		AllowErrors:           tidyE,
    69  	}, "all")
    70  
    71  	modload.TidyBuildList()
    72  	modload.TrimGoSum()
    73  	modload.WriteGoMod()
    74  }
    75  

View as plain text