c++: diagnose this specifier in requires expr [PR116798]
[official-gcc.git] / libgo / go / cmd / go / internal / modcmd / tidy.go
blobd35476eb5393d3e92c6e61f384c99f5bf04463a7
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.
5 // go mod tidy
7 package modcmd
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 "fmt"
17 "golang.org/x/mod/modfile"
18 "golang.org/x/mod/semver"
21 var cmdTidy = &base.Command{
22 UsageLine: "go mod tidy [-e] [-v] [-go=version] [-compat=version]",
23 Short: "add missing and remove unused modules",
24 Long: `
25 Tidy makes sure go.mod matches the source code in the module.
26 It adds any missing modules necessary to build the current module's
27 packages and dependencies, and it removes unused modules that
28 don't provide any relevant packages. It also adds any missing entries
29 to go.sum and removes any unnecessary ones.
31 The -v flag causes tidy to print information about removed modules
32 to standard error.
34 The -e flag causes tidy to attempt to proceed despite errors
35 encountered while loading packages.
37 The -go flag causes tidy to update the 'go' directive in the go.mod
38 file to the given version, which may change which module dependencies
39 are retained as explicit requirements in the go.mod file.
40 (Go versions 1.17 and higher retain more requirements in order to
41 support lazy module loading.)
43 The -compat flag preserves any additional checksums needed for the
44 'go' command from the indicated major Go release to successfully load
45 the module graph, and causes tidy to error out if that version of the
46 'go' command would load any imported package from a different module
47 version. By default, tidy acts as if the -compat flag were set to the
48 version prior to the one indicated by the 'go' directive in the go.mod
49 file.
51 See https://golang.org/ref/mod#go-mod-tidy for more about 'go mod tidy'.
53 Run: runTidy,
56 var (
57 tidyE bool // if true, report errors but proceed anyway.
58 tidyGo goVersionFlag // go version to write to the tidied go.mod file (toggles lazy loading)
59 tidyCompat goVersionFlag // go version for which the tidied go.mod and go.sum files should be “compatible”
62 func init() {
63 cmdTidy.Flag.BoolVar(&cfg.BuildV, "v", false, "")
64 cmdTidy.Flag.BoolVar(&tidyE, "e", false, "")
65 cmdTidy.Flag.Var(&tidyGo, "go", "")
66 cmdTidy.Flag.Var(&tidyCompat, "compat", "")
67 base.AddModCommonFlags(&cmdTidy.Flag)
70 // A goVersionFlag is a flag.Value representing a supported Go version.
72 // (Note that the -go argument to 'go mod edit' is *not* a goVersionFlag.
73 // It intentionally allows newer-than-supported versions as arguments.)
74 type goVersionFlag struct {
75 v string
78 func (f *goVersionFlag) String() string { return f.v }
79 func (f *goVersionFlag) Get() any { return f.v }
81 func (f *goVersionFlag) Set(s string) error {
82 if s != "" {
83 latest := modload.LatestGoVersion()
84 if !modfile.GoVersionRE.MatchString(s) {
85 return fmt.Errorf("expecting a Go version like %q", latest)
87 if semver.Compare("v"+s, "v"+latest) > 0 {
88 return fmt.Errorf("maximum supported Go version is %s", latest)
92 f.v = s
93 return nil
96 func runTidy(ctx context.Context, cmd *base.Command, args []string) {
97 if len(args) > 0 {
98 base.Fatalf("go: 'go mod tidy' accepts no arguments")
101 // Tidy aims to make 'go test' reproducible for any package in 'all', so we
102 // need to include test dependencies. For modules that specify go 1.15 or
103 // earlier this is a no-op (because 'all' saturates transitive test
104 // dependencies).
106 // However, with lazy loading (go 1.16+) 'all' includes only the packages that
107 // are transitively imported by the main module, not the test dependencies of
108 // those packages. In order to make 'go test' reproducible for the packages
109 // that are in 'all' but outside of the main module, we must explicitly
110 // request that their test dependencies be included.
111 modload.ForceUseModules = true
112 modload.RootMode = modload.NeedRoot
114 modload.LoadPackages(ctx, modload.PackageOpts{
115 GoVersion: tidyGo.String(),
116 Tags: imports.AnyTags(),
117 Tidy: true,
118 TidyCompatibleVersion: tidyCompat.String(),
119 VendorModulesInGOROOTSrc: true,
120 ResolveMissingImports: true,
121 LoadTests: true,
122 AllowErrors: tidyE,
123 SilenceMissingStdImports: true,
124 }, "all")