github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/go/modload/init.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  package modload
     6  
     7  import (
     8  	"bytes"
     9  	"context"
    10  	"encoding/json"
    11  	"errors"
    12  	"fmt"
    13  	"io"
    14  	"os"
    15  	"path"
    16  	"path/filepath"
    17  	"slices"
    18  	"strconv"
    19  	"strings"
    20  	"sync"
    21  
    22  	"github.com/go-asm/go/lazyregexp"
    23  
    24  	"github.com/go-asm/go/cmd/go/base"
    25  	"github.com/go-asm/go/cmd/go/cfg"
    26  	"github.com/go-asm/go/cmd/go/fsys"
    27  	"github.com/go-asm/go/cmd/go/gover"
    28  	"github.com/go-asm/go/cmd/go/lockedfile"
    29  	"github.com/go-asm/go/cmd/go/modfetch"
    30  	"github.com/go-asm/go/cmd/go/search"
    31  
    32  	"golang.org/x/mod/modfile"
    33  	"golang.org/x/mod/module"
    34  )
    35  
    36  // Variables set by other packages.
    37  //
    38  // TODO(#40775): See if these can be plumbed as explicit parameters.
    39  var (
    40  	// RootMode determines whether a module root is needed.
    41  	RootMode Root
    42  
    43  	// ForceUseModules may be set to force modules to be enabled when
    44  	// GO111MODULE=auto or to report an error when GO111MODULE=off.
    45  	ForceUseModules bool
    46  
    47  	allowMissingModuleImports bool
    48  
    49  	// ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions
    50  	// from updating go.mod and go.sum or reporting errors when updates are
    51  	// needed. A package should set this if it would cause go.mod to be written
    52  	// multiple times (for example, 'go get' calls LoadPackages multiple times) or
    53  	// if it needs some other operation to be successful before go.mod and go.sum
    54  	// can be written (for example, 'go mod download' must download modules before
    55  	// adding sums to go.sum). Packages that set this are responsible for calling
    56  	// WriteGoMod explicitly.
    57  	ExplicitWriteGoMod bool
    58  )
    59  
    60  // Variables set in Init.
    61  var (
    62  	initialized bool
    63  
    64  	// These are primarily used to initialize the MainModules, and should be
    65  	// eventually superseded by them but are still used in cases where the module
    66  	// roots are required but MainModules hasn't been initialized yet. Set to
    67  	// the modRoots of the main modules.
    68  	// modRoots != nil implies len(modRoots) > 0
    69  	modRoots []string
    70  	gopath   string
    71  )
    72  
    73  // EnterModule resets MainModules and requirements to refer to just this one module.
    74  func EnterModule(ctx context.Context, enterModroot string) {
    75  	MainModules = nil // reset MainModules
    76  	requirements = nil
    77  	workFilePath = "" // Force module mode
    78  	modfetch.Reset()
    79  
    80  	modRoots = []string{enterModroot}
    81  	LoadModFile(ctx)
    82  }
    83  
    84  // Variable set in InitWorkfile
    85  var (
    86  	// Set to the path to the go.work file, or "" if workspace mode is disabled.
    87  	workFilePath string
    88  )
    89  
    90  type MainModuleSet struct {
    91  	// versions are the module.Version values of each of the main modules.
    92  	// For each of them, the Path fields are ordinary module paths and the Version
    93  	// fields are empty strings.
    94  	// versions is clipped (len=cap).
    95  	versions []module.Version
    96  
    97  	// modRoot maps each module in versions to its absolute filesystem path.
    98  	modRoot map[module.Version]string
    99  
   100  	// pathPrefix is the path prefix for packages in the module, without a trailing
   101  	// slash. For most modules, pathPrefix is just version.Path, but the
   102  	// standard-library module "std" has an empty prefix.
   103  	pathPrefix map[module.Version]string
   104  
   105  	// inGorootSrc caches whether modRoot is within GOROOT/src.
   106  	// The "std" module is special within GOROOT/src, but not otherwise.
   107  	inGorootSrc map[module.Version]bool
   108  
   109  	modFiles map[module.Version]*modfile.File
   110  
   111  	modContainingCWD module.Version
   112  
   113  	workFile *modfile.WorkFile
   114  
   115  	workFileReplaceMap map[module.Version]module.Version
   116  	// highest replaced version of each module path; empty string for wildcard-only replacements
   117  	highestReplaced map[string]string
   118  
   119  	indexMu sync.Mutex
   120  	indices map[module.Version]*modFileIndex
   121  }
   122  
   123  func (mms *MainModuleSet) PathPrefix(m module.Version) string {
   124  	return mms.pathPrefix[m]
   125  }
   126  
   127  // Versions returns the module.Version values of each of the main modules.
   128  // For each of them, the Path fields are ordinary module paths and the Version
   129  // fields are empty strings.
   130  // Callers should not modify the returned slice.
   131  func (mms *MainModuleSet) Versions() []module.Version {
   132  	if mms == nil {
   133  		return nil
   134  	}
   135  	return mms.versions
   136  }
   137  
   138  func (mms *MainModuleSet) Contains(path string) bool {
   139  	if mms == nil {
   140  		return false
   141  	}
   142  	for _, v := range mms.versions {
   143  		if v.Path == path {
   144  			return true
   145  		}
   146  	}
   147  	return false
   148  }
   149  
   150  func (mms *MainModuleSet) ModRoot(m module.Version) string {
   151  	if mms == nil {
   152  		return ""
   153  	}
   154  	return mms.modRoot[m]
   155  }
   156  
   157  func (mms *MainModuleSet) InGorootSrc(m module.Version) bool {
   158  	if mms == nil {
   159  		return false
   160  	}
   161  	return mms.inGorootSrc[m]
   162  }
   163  
   164  func (mms *MainModuleSet) mustGetSingleMainModule() module.Version {
   165  	if mms == nil || len(mms.versions) == 0 {
   166  		panic("internal error: mustGetSingleMainModule called in context with no main modules")
   167  	}
   168  	if len(mms.versions) != 1 {
   169  		if inWorkspaceMode() {
   170  			panic("internal error: mustGetSingleMainModule called in workspace mode")
   171  		} else {
   172  			panic("internal error: multiple main modules present outside of workspace mode")
   173  		}
   174  	}
   175  	return mms.versions[0]
   176  }
   177  
   178  func (mms *MainModuleSet) GetSingleIndexOrNil() *modFileIndex {
   179  	if mms == nil {
   180  		return nil
   181  	}
   182  	if len(mms.versions) == 0 {
   183  		return nil
   184  	}
   185  	return mms.indices[mms.mustGetSingleMainModule()]
   186  }
   187  
   188  func (mms *MainModuleSet) Index(m module.Version) *modFileIndex {
   189  	mms.indexMu.Lock()
   190  	defer mms.indexMu.Unlock()
   191  	return mms.indices[m]
   192  }
   193  
   194  func (mms *MainModuleSet) SetIndex(m module.Version, index *modFileIndex) {
   195  	mms.indexMu.Lock()
   196  	defer mms.indexMu.Unlock()
   197  	mms.indices[m] = index
   198  }
   199  
   200  func (mms *MainModuleSet) ModFile(m module.Version) *modfile.File {
   201  	return mms.modFiles[m]
   202  }
   203  
   204  func (mms *MainModuleSet) WorkFile() *modfile.WorkFile {
   205  	return mms.workFile
   206  }
   207  
   208  func (mms *MainModuleSet) Len() int {
   209  	if mms == nil {
   210  		return 0
   211  	}
   212  	return len(mms.versions)
   213  }
   214  
   215  // ModContainingCWD returns the main module containing the working directory,
   216  // or module.Version{} if none of the main modules contain the working
   217  // directory.
   218  func (mms *MainModuleSet) ModContainingCWD() module.Version {
   219  	return mms.modContainingCWD
   220  }
   221  
   222  func (mms *MainModuleSet) HighestReplaced() map[string]string {
   223  	return mms.highestReplaced
   224  }
   225  
   226  // GoVersion returns the go version set on the single module, in module mode,
   227  // or the go.work file in workspace mode.
   228  func (mms *MainModuleSet) GoVersion() string {
   229  	if inWorkspaceMode() {
   230  		return gover.FromGoWork(mms.workFile)
   231  	}
   232  	if mms != nil && len(mms.versions) == 1 {
   233  		f := mms.ModFile(mms.mustGetSingleMainModule())
   234  		if f == nil {
   235  			// Special case: we are outside a module, like 'go run x.go'.
   236  			// Assume the local Go version.
   237  			// TODO(#49228): Clean this up; see loadModFile.
   238  			return gover.Local()
   239  		}
   240  		return gover.FromGoMod(f)
   241  	}
   242  	return gover.DefaultGoModVersion
   243  }
   244  
   245  // Toolchain returns the toolchain set on the single module, in module mode,
   246  // or the go.work file in workspace mode.
   247  func (mms *MainModuleSet) Toolchain() string {
   248  	if inWorkspaceMode() {
   249  		if mms.workFile != nil && mms.workFile.Toolchain != nil {
   250  			return mms.workFile.Toolchain.Name
   251  		}
   252  		return "go" + mms.GoVersion()
   253  	}
   254  	if mms != nil && len(mms.versions) == 1 {
   255  		f := mms.ModFile(mms.mustGetSingleMainModule())
   256  		if f == nil {
   257  			// Special case: we are outside a module, like 'go run x.go'.
   258  			// Assume the local Go version.
   259  			// TODO(#49228): Clean this up; see loadModFile.
   260  			return gover.LocalToolchain()
   261  		}
   262  		if f.Toolchain != nil {
   263  			return f.Toolchain.Name
   264  		}
   265  	}
   266  	return "go" + mms.GoVersion()
   267  }
   268  
   269  func (mms *MainModuleSet) WorkFileReplaceMap() map[module.Version]module.Version {
   270  	return mms.workFileReplaceMap
   271  }
   272  
   273  var MainModules *MainModuleSet
   274  
   275  type Root int
   276  
   277  const (
   278  	// AutoRoot is the default for most commands. modload.Init will look for
   279  	// a go.mod file in the current directory or any parent. If none is found,
   280  	// modules may be disabled (GO111MODULE=auto) or commands may run in a
   281  	// limited module mode.
   282  	AutoRoot Root = iota
   283  
   284  	// NoRoot is used for commands that run in module mode and ignore any go.mod
   285  	// file the current directory or in parent directories.
   286  	NoRoot
   287  
   288  	// NeedRoot is used for commands that must run in module mode and don't
   289  	// make sense without a main module.
   290  	NeedRoot
   291  )
   292  
   293  // ModFile returns the parsed go.mod file.
   294  //
   295  // Note that after calling LoadPackages or LoadModGraph,
   296  // the require statements in the modfile.File are no longer
   297  // the source of truth and will be ignored: edits made directly
   298  // will be lost at the next call to WriteGoMod.
   299  // To make permanent changes to the require statements
   300  // in go.mod, edit it before loading.
   301  func ModFile() *modfile.File {
   302  	Init()
   303  	modFile := MainModules.ModFile(MainModules.mustGetSingleMainModule())
   304  	if modFile == nil {
   305  		die()
   306  	}
   307  	return modFile
   308  }
   309  
   310  func BinDir() string {
   311  	Init()
   312  	if cfg.GOBIN != "" {
   313  		return cfg.GOBIN
   314  	}
   315  	if gopath == "" {
   316  		return ""
   317  	}
   318  	return filepath.Join(gopath, "bin")
   319  }
   320  
   321  // InitWorkfile initializes the workFilePath variable for commands that
   322  // operate in workspace mode. It should not be called by other commands,
   323  // for example 'go mod tidy', that don't operate in workspace mode.
   324  func InitWorkfile() {
   325  	workFilePath = FindGoWork(base.Cwd())
   326  }
   327  
   328  // FindGoWork returns the name of the go.work file for this command,
   329  // or the empty string if there isn't one.
   330  // Most code should use Init and Enabled rather than use this directly.
   331  // It is exported mainly for Go toolchain switching, which must process
   332  // the go.work very early at startup.
   333  func FindGoWork(wd string) string {
   334  	if RootMode == NoRoot {
   335  		return ""
   336  	}
   337  
   338  	switch gowork := cfg.Getenv("GOWORK"); gowork {
   339  	case "off":
   340  		return ""
   341  	case "", "auto":
   342  		return findWorkspaceFile(wd)
   343  	default:
   344  		if !filepath.IsAbs(gowork) {
   345  			base.Fatalf("go: invalid GOWORK: not an absolute path")
   346  		}
   347  		return gowork
   348  	}
   349  }
   350  
   351  // WorkFilePath returns the absolute path of the go.work file, or "" if not in
   352  // workspace mode. WorkFilePath must be called after InitWorkfile.
   353  func WorkFilePath() string {
   354  	return workFilePath
   355  }
   356  
   357  // Reset clears all the initialized, cached state about the use of modules,
   358  // so that we can start over.
   359  func Reset() {
   360  	initialized = false
   361  	ForceUseModules = false
   362  	RootMode = 0
   363  	modRoots = nil
   364  	cfg.ModulesEnabled = false
   365  	MainModules = nil
   366  	requirements = nil
   367  	workFilePath = ""
   368  	modfetch.Reset()
   369  }
   370  
   371  // Init determines whether module mode is enabled, locates the root of the
   372  // current module (if any), sets environment variables for Git subprocesses, and
   373  // configures the cfg, codehost, load, modfetch, and search packages for use
   374  // with modules.
   375  func Init() {
   376  	if initialized {
   377  		return
   378  	}
   379  	initialized = true
   380  
   381  	// Keep in sync with WillBeEnabled. We perform extra validation here, and
   382  	// there are lots of diagnostics and side effects, so we can't use
   383  	// WillBeEnabled directly.
   384  	var mustUseModules bool
   385  	env := cfg.Getenv("GO111MODULE")
   386  	switch env {
   387  	default:
   388  		base.Fatalf("go: unknown environment setting GO111MODULE=%s", env)
   389  	case "auto":
   390  		mustUseModules = ForceUseModules
   391  	case "on", "":
   392  		mustUseModules = true
   393  	case "off":
   394  		if ForceUseModules {
   395  			base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
   396  		}
   397  		mustUseModules = false
   398  		return
   399  	}
   400  
   401  	if err := fsys.Init(base.Cwd()); err != nil {
   402  		base.Fatal(err)
   403  	}
   404  
   405  	// Disable any prompting for passwords by Git.
   406  	// Only has an effect for 2.3.0 or later, but avoiding
   407  	// the prompt in earlier versions is just too hard.
   408  	// If user has explicitly set GIT_TERMINAL_PROMPT=1, keep
   409  	// prompting.
   410  	// See golang.org/issue/9341 and golang.org/issue/12706.
   411  	if os.Getenv("GIT_TERMINAL_PROMPT") == "" {
   412  		os.Setenv("GIT_TERMINAL_PROMPT", "0")
   413  	}
   414  
   415  	// Disable any ssh connection pooling by Git.
   416  	// If a Git subprocess forks a child into the background to cache a new connection,
   417  	// that child keeps stdout/stderr open. After the Git subprocess exits,
   418  	// os/exec expects to be able to read from the stdout/stderr pipe
   419  	// until EOF to get all the data that the Git subprocess wrote before exiting.
   420  	// The EOF doesn't come until the child exits too, because the child
   421  	// is holding the write end of the pipe.
   422  	// This is unfortunate, but it has come up at least twice
   423  	// (see golang.org/issue/13453 and golang.org/issue/16104)
   424  	// and confuses users when it does.
   425  	// If the user has explicitly set GIT_SSH or GIT_SSH_COMMAND,
   426  	// assume they know what they are doing and don't step on it.
   427  	// But default to turning off ControlMaster.
   428  	if os.Getenv("GIT_SSH") == "" && os.Getenv("GIT_SSH_COMMAND") == "" {
   429  		os.Setenv("GIT_SSH_COMMAND", "ssh -o ControlMaster=no -o BatchMode=yes")
   430  	}
   431  
   432  	if os.Getenv("GCM_INTERACTIVE") == "" {
   433  		os.Setenv("GCM_INTERACTIVE", "never")
   434  	}
   435  	if modRoots != nil {
   436  		// modRoot set before Init was called ("go mod init" does this).
   437  		// No need to search for go.mod.
   438  	} else if RootMode == NoRoot {
   439  		if cfg.ModFile != "" && !base.InGOFLAGS("-modfile") {
   440  			base.Fatalf("go: -modfile cannot be used with commands that ignore the current module")
   441  		}
   442  		modRoots = nil
   443  	} else if workFilePath != "" {
   444  		// We're in workspace mode, which implies module mode.
   445  		if cfg.ModFile != "" {
   446  			base.Fatalf("go: -modfile cannot be used in workspace mode")
   447  		}
   448  	} else {
   449  		if modRoot := findModuleRoot(base.Cwd()); modRoot == "" {
   450  			if cfg.ModFile != "" {
   451  				base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.")
   452  			}
   453  			if RootMode == NeedRoot {
   454  				base.Fatal(ErrNoModRoot)
   455  			}
   456  			if !mustUseModules {
   457  				// GO111MODULE is 'auto', and we can't find a module root.
   458  				// Stay in GOPATH mode.
   459  				return
   460  			}
   461  		} else if search.InDir(modRoot, os.TempDir()) == "." {
   462  			// If you create /tmp/go.mod for experimenting,
   463  			// then any tests that create work directories under /tmp
   464  			// will find it and get modules when they're not expecting them.
   465  			// It's a bit of a peculiar thing to disallow but quite mysterious
   466  			// when it happens. See golang.org/issue/26708.
   467  			fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in system temp root %v\n", os.TempDir())
   468  			if RootMode == NeedRoot {
   469  				base.Fatal(ErrNoModRoot)
   470  			}
   471  			if !mustUseModules {
   472  				return
   473  			}
   474  		} else {
   475  			modRoots = []string{modRoot}
   476  		}
   477  	}
   478  	if cfg.ModFile != "" && !strings.HasSuffix(cfg.ModFile, ".mod") {
   479  		base.Fatalf("go: -modfile=%s: file does not have .mod extension", cfg.ModFile)
   480  	}
   481  
   482  	// We're in module mode. Set any global variables that need to be set.
   483  	cfg.ModulesEnabled = true
   484  	setDefaultBuildMod()
   485  	list := filepath.SplitList(cfg.BuildContext.GOPATH)
   486  	if len(list) > 0 && list[0] != "" {
   487  		gopath = list[0]
   488  		if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil {
   489  			fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in $GOPATH %v\n", gopath)
   490  			if RootMode == NeedRoot {
   491  				base.Fatal(ErrNoModRoot)
   492  			}
   493  			if !mustUseModules {
   494  				return
   495  			}
   496  		}
   497  	}
   498  }
   499  
   500  // WillBeEnabled checks whether modules should be enabled but does not
   501  // initialize modules by installing hooks. If Init has already been called,
   502  // WillBeEnabled returns the same result as Enabled.
   503  //
   504  // This function is needed to break a cycle. The main package needs to know
   505  // whether modules are enabled in order to install the module or GOPATH version
   506  // of 'go get', but Init reads the -modfile flag in 'go get', so it shouldn't
   507  // be called until the command is installed and flags are parsed. Instead of
   508  // calling Init and Enabled, the main package can call this function.
   509  func WillBeEnabled() bool {
   510  	if modRoots != nil || cfg.ModulesEnabled {
   511  		// Already enabled.
   512  		return true
   513  	}
   514  	if initialized {
   515  		// Initialized, not enabled.
   516  		return false
   517  	}
   518  
   519  	// Keep in sync with Init. Init does extra validation and prints warnings or
   520  	// exits, so it can't call this function directly.
   521  	env := cfg.Getenv("GO111MODULE")
   522  	switch env {
   523  	case "on", "":
   524  		return true
   525  	case "auto":
   526  		break
   527  	default:
   528  		return false
   529  	}
   530  
   531  	return FindGoMod(base.Cwd()) != ""
   532  }
   533  
   534  // FindGoMod returns the name of the go.mod file for this command,
   535  // or the empty string if there isn't one.
   536  // Most code should use Init and Enabled rather than use this directly.
   537  // It is exported mainly for Go toolchain switching, which must process
   538  // the go.mod very early at startup.
   539  func FindGoMod(wd string) string {
   540  	modRoot := findModuleRoot(wd)
   541  	if modRoot == "" {
   542  		// GO111MODULE is 'auto', and we can't find a module root.
   543  		// Stay in GOPATH mode.
   544  		return ""
   545  	}
   546  	if search.InDir(modRoot, os.TempDir()) == "." {
   547  		// If you create /tmp/go.mod for experimenting,
   548  		// then any tests that create work directories under /tmp
   549  		// will find it and get modules when they're not expecting them.
   550  		// It's a bit of a peculiar thing to disallow but quite mysterious
   551  		// when it happens. See golang.org/issue/26708.
   552  		return ""
   553  	}
   554  	return filepath.Join(modRoot, "go.mod")
   555  }
   556  
   557  // Enabled reports whether modules are (or must be) enabled.
   558  // If modules are enabled but there is no main module, Enabled returns true
   559  // and then the first use of module information will call die
   560  // (usually through MustModRoot).
   561  func Enabled() bool {
   562  	Init()
   563  	return modRoots != nil || cfg.ModulesEnabled
   564  }
   565  
   566  func VendorDir() string {
   567  	if inWorkspaceMode() {
   568  		return filepath.Join(filepath.Dir(WorkFilePath()), "vendor")
   569  	}
   570  	// Even if -mod=vendor, we could be operating with no mod root (and thus no
   571  	// vendor directory). As long as there are no dependencies that is expected
   572  	// to work. See script/vendor_outside_module.txt.
   573  	modRoot := MainModules.ModRoot(MainModules.mustGetSingleMainModule())
   574  	if modRoot == "" {
   575  		panic("vendor directory does not exist when in single module mode outside of a module")
   576  	}
   577  	return filepath.Join(modRoot, "vendor")
   578  }
   579  
   580  func inWorkspaceMode() bool {
   581  	if !initialized {
   582  		panic("inWorkspaceMode called before modload.Init called")
   583  	}
   584  	if !Enabled() {
   585  		return false
   586  	}
   587  	return workFilePath != ""
   588  }
   589  
   590  // HasModRoot reports whether a main module is present.
   591  // HasModRoot may return false even if Enabled returns true: for example, 'get'
   592  // does not require a main module.
   593  func HasModRoot() bool {
   594  	Init()
   595  	return modRoots != nil
   596  }
   597  
   598  // MustHaveModRoot checks that a main module or main modules are present,
   599  // and calls base.Fatalf if there are no main modules.
   600  func MustHaveModRoot() {
   601  	Init()
   602  	if !HasModRoot() {
   603  		die()
   604  	}
   605  }
   606  
   607  // ModFilePath returns the path that would be used for the go.mod
   608  // file, if in module mode. ModFilePath calls base.Fatalf if there is no main
   609  // module, even if -modfile is set.
   610  func ModFilePath() string {
   611  	MustHaveModRoot()
   612  	return modFilePath(findModuleRoot(base.Cwd()))
   613  }
   614  
   615  func modFilePath(modRoot string) string {
   616  	if cfg.ModFile != "" {
   617  		return cfg.ModFile
   618  	}
   619  	return filepath.Join(modRoot, "go.mod")
   620  }
   621  
   622  func die() {
   623  	if cfg.Getenv("GO111MODULE") == "off" {
   624  		base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
   625  	}
   626  	if inWorkspaceMode() {
   627  		base.Fatalf("go: no modules were found in the current workspace; see 'go help work'")
   628  	}
   629  	if dir, name := findAltConfig(base.Cwd()); dir != "" {
   630  		rel, err := filepath.Rel(base.Cwd(), dir)
   631  		if err != nil {
   632  			rel = dir
   633  		}
   634  		cdCmd := ""
   635  		if rel != "." {
   636  			cdCmd = fmt.Sprintf("cd %s && ", rel)
   637  		}
   638  		base.Fatalf("go: cannot find main module, but found %s in %s\n\tto create a module there, run:\n\t%sgo mod init", name, dir, cdCmd)
   639  	}
   640  	base.Fatal(ErrNoModRoot)
   641  }
   642  
   643  var ErrNoModRoot = errors.New("go.mod file not found in current directory or any parent directory; see 'go help modules'")
   644  
   645  type goModDirtyError struct{}
   646  
   647  func (goModDirtyError) Error() string {
   648  	if cfg.BuildModExplicit {
   649  		return fmt.Sprintf("updates to go.mod needed, disabled by -mod=%v; to update it:\n\tgo mod tidy", cfg.BuildMod)
   650  	}
   651  	if cfg.BuildModReason != "" {
   652  		return fmt.Sprintf("updates to go.mod needed, disabled by -mod=%s\n\t(%s)\n\tto update it:\n\tgo mod tidy", cfg.BuildMod, cfg.BuildModReason)
   653  	}
   654  	return "updates to go.mod needed; to update it:\n\tgo mod tidy"
   655  }
   656  
   657  var errGoModDirty error = goModDirtyError{}
   658  
   659  func loadWorkFile(path string) (workFile *modfile.WorkFile, modRoots []string, err error) {
   660  	workDir := filepath.Dir(path)
   661  	wf, err := ReadWorkFile(path)
   662  	if err != nil {
   663  		return nil, nil, err
   664  	}
   665  	seen := map[string]bool{}
   666  	for _, d := range wf.Use {
   667  		modRoot := d.Path
   668  		if !filepath.IsAbs(modRoot) {
   669  			modRoot = filepath.Join(workDir, modRoot)
   670  		}
   671  
   672  		if seen[modRoot] {
   673  			return nil, nil, fmt.Errorf("path %s appears multiple times in workspace", modRoot)
   674  		}
   675  		seen[modRoot] = true
   676  		modRoots = append(modRoots, modRoot)
   677  	}
   678  
   679  	return wf, modRoots, nil
   680  }
   681  
   682  // ReadWorkFile reads and parses the go.work file at the given path.
   683  func ReadWorkFile(path string) (*modfile.WorkFile, error) {
   684  	workData, err := os.ReadFile(path)
   685  	if err != nil {
   686  		return nil, err
   687  	}
   688  
   689  	f, err := modfile.ParseWork(path, workData, nil)
   690  	if err != nil {
   691  		return nil, err
   692  	}
   693  	if f.Go != nil && gover.Compare(f.Go.Version, gover.Local()) > 0 && cfg.CmdName != "work edit" {
   694  		base.Fatal(&gover.TooNewError{What: base.ShortPath(path), GoVersion: f.Go.Version})
   695  	}
   696  	return f, nil
   697  }
   698  
   699  // WriteWorkFile cleans and writes out the go.work file to the given path.
   700  func WriteWorkFile(path string, wf *modfile.WorkFile) error {
   701  	wf.SortBlocks()
   702  	wf.Cleanup()
   703  	out := modfile.Format(wf.Syntax)
   704  
   705  	return os.WriteFile(path, out, 0666)
   706  }
   707  
   708  // UpdateWorkGoVersion updates the go line in wf to be at least goVers,
   709  // reporting whether it changed the file.
   710  func UpdateWorkGoVersion(wf *modfile.WorkFile, goVers string) (changed bool) {
   711  	old := gover.FromGoWork(wf)
   712  	if gover.Compare(old, goVers) >= 0 {
   713  		return false
   714  	}
   715  
   716  	wf.AddGoStmt(goVers)
   717  
   718  	// We wrote a new go line. For reproducibility,
   719  	// if the toolchain running right now is newer than the new toolchain line,
   720  	// update the toolchain line to record the newer toolchain.
   721  	// The user never sets the toolchain explicitly in a 'go work' command,
   722  	// so this is only happening as a result of a go or toolchain line found
   723  	// in a module.
   724  	// If the toolchain running right now is a dev toolchain (like "go1.21")
   725  	// writing 'toolchain go1.21' will not be useful, since that's not an actual
   726  	// toolchain you can download and run. In that case fall back to at least
   727  	// checking that the toolchain is new enough for the Go version.
   728  	toolchain := "go" + old
   729  	if wf.Toolchain != nil {
   730  		toolchain = wf.Toolchain.Name
   731  	}
   732  	if gover.IsLang(gover.Local()) {
   733  		toolchain = gover.ToolchainMax(toolchain, "go"+goVers)
   734  	} else {
   735  		toolchain = gover.ToolchainMax(toolchain, "go"+gover.Local())
   736  	}
   737  
   738  	// Drop the toolchain line if it is implied by the go line
   739  	// or if it is asking for a toolchain older than Go 1.21,
   740  	// which will not understand the toolchain line.
   741  	if toolchain == "go"+goVers || gover.Compare(gover.FromToolchain(toolchain), gover.GoStrictVersion) < 0 {
   742  		wf.DropToolchainStmt()
   743  	} else {
   744  		wf.AddToolchainStmt(toolchain)
   745  	}
   746  	return true
   747  }
   748  
   749  // UpdateWorkFile updates comments on directory directives in the go.work
   750  // file to include the associated module path.
   751  func UpdateWorkFile(wf *modfile.WorkFile) {
   752  	missingModulePaths := map[string]string{} // module directory listed in file -> abspath modroot
   753  
   754  	for _, d := range wf.Use {
   755  		if d.Path == "" {
   756  			continue // d is marked for deletion.
   757  		}
   758  		modRoot := d.Path
   759  		if d.ModulePath == "" {
   760  			missingModulePaths[d.Path] = modRoot
   761  		}
   762  	}
   763  
   764  	// Clean up and annotate directories.
   765  	// TODO(matloob): update x/mod to actually add module paths.
   766  	for moddir, absmodroot := range missingModulePaths {
   767  		_, f, err := ReadModFile(filepath.Join(absmodroot, "go.mod"), nil)
   768  		if err != nil {
   769  			continue // Error will be reported if modules are loaded.
   770  		}
   771  		wf.AddUse(moddir, f.Module.Mod.Path)
   772  	}
   773  }
   774  
   775  // LoadModFile sets Target and, if there is a main module, parses the initial
   776  // build list from its go.mod file.
   777  //
   778  // LoadModFile may make changes in memory, like adding a go directive and
   779  // ensuring requirements are consistent. The caller is responsible for ensuring
   780  // those changes are written to disk by calling LoadPackages or ListModules
   781  // (unless ExplicitWriteGoMod is set) or by calling WriteGoMod directly.
   782  //
   783  // As a side-effect, LoadModFile may change cfg.BuildMod to "vendor" if
   784  // -mod wasn't set explicitly and automatic vendoring should be enabled.
   785  //
   786  // If LoadModFile or CreateModFile has already been called, LoadModFile returns
   787  // the existing in-memory requirements (rather than re-reading them from disk).
   788  //
   789  // LoadModFile checks the roots of the module graph for consistency with each
   790  // other, but unlike LoadModGraph does not load the full module graph or check
   791  // it for global consistency. Most callers outside of the modload package should
   792  // use LoadModGraph instead.
   793  func LoadModFile(ctx context.Context) *Requirements {
   794  	rs, err := loadModFile(ctx, nil)
   795  	if err != nil {
   796  		base.Fatal(err)
   797  	}
   798  	return rs
   799  }
   800  
   801  func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error) {
   802  	if requirements != nil {
   803  		return requirements, nil
   804  	}
   805  
   806  	Init()
   807  	var workFile *modfile.WorkFile
   808  	if inWorkspaceMode() {
   809  		var err error
   810  		workFile, modRoots, err = loadWorkFile(workFilePath)
   811  		if err != nil {
   812  			return nil, fmt.Errorf("reading go.work: %w", err)
   813  		}
   814  		for _, modRoot := range modRoots {
   815  			sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
   816  			modfetch.WorkspaceGoSumFiles = append(modfetch.WorkspaceGoSumFiles, sumFile)
   817  		}
   818  		modfetch.GoSumFile = workFilePath + ".sum"
   819  	} else if len(modRoots) == 0 {
   820  		// We're in module mode, but not inside a module.
   821  		//
   822  		// Commands like 'go build', 'go run', 'go list' have no go.mod file to
   823  		// read or write. They would need to find and download the latest versions
   824  		// of a potentially large number of modules with no way to save version
   825  		// information. We can succeed slowly (but not reproducibly), but that's
   826  		// not usually a good experience.
   827  		//
   828  		// Instead, we forbid resolving import paths to modules other than std and
   829  		// cmd. Users may still build packages specified with .go files on the
   830  		// command line, but they'll see an error if those files import anything
   831  		// outside std.
   832  		//
   833  		// This can be overridden by calling AllowMissingModuleImports.
   834  		// For example, 'go get' does this, since it is expected to resolve paths.
   835  		//
   836  		// See golang.org/issue/32027.
   837  	} else {
   838  		modfetch.GoSumFile = strings.TrimSuffix(modFilePath(modRoots[0]), ".mod") + ".sum"
   839  	}
   840  	if len(modRoots) == 0 {
   841  		// TODO(#49228): Instead of creating a fake module with an empty modroot,
   842  		// make MainModules.Len() == 0 mean that we're in module mode but not inside
   843  		// any module.
   844  		mainModule := module.Version{Path: "command-line-arguments"}
   845  		MainModules = makeMainModules([]module.Version{mainModule}, []string{""}, []*modfile.File{nil}, []*modFileIndex{nil}, nil)
   846  		var (
   847  			goVersion string
   848  			pruning   modPruning
   849  			roots     []module.Version
   850  			direct    = map[string]bool{"go": true}
   851  		)
   852  		if inWorkspaceMode() {
   853  			// Since we are in a workspace, the Go version for the synthetic
   854  			// "command-line-arguments" module must not exceed the Go version
   855  			// for the workspace.
   856  			goVersion = MainModules.GoVersion()
   857  			pruning = workspace
   858  			roots = []module.Version{
   859  				mainModule,
   860  				{Path: "go", Version: goVersion},
   861  				{Path: "toolchain", Version: gover.LocalToolchain()},
   862  			}
   863  		} else {
   864  			goVersion = gover.Local()
   865  			pruning = pruningForGoVersion(goVersion)
   866  			roots = []module.Version{
   867  				{Path: "go", Version: goVersion},
   868  				{Path: "toolchain", Version: gover.LocalToolchain()},
   869  			}
   870  		}
   871  		rawGoVersion.Store(mainModule, goVersion)
   872  		requirements = newRequirements(pruning, roots, direct)
   873  		if cfg.BuildMod == "vendor" {
   874  			// For issue 56536: Some users may have GOFLAGS=-mod=vendor set.
   875  			// Make sure it behaves as though the fake module is vendored
   876  			// with no dependencies.
   877  			requirements.initVendor(nil)
   878  		}
   879  		return requirements, nil
   880  	}
   881  
   882  	var modFiles []*modfile.File
   883  	var mainModules []module.Version
   884  	var indices []*modFileIndex
   885  	var errs []error
   886  	for _, modroot := range modRoots {
   887  		gomod := modFilePath(modroot)
   888  		var fixed bool
   889  		data, f, err := ReadModFile(gomod, fixVersion(ctx, &fixed))
   890  		if err != nil {
   891  			if inWorkspaceMode() {
   892  				if tooNew, ok := err.(*gover.TooNewError); ok && !strings.HasPrefix(cfg.CmdName, "work ") {
   893  					// Switching to a newer toolchain won't help - the go.work has the wrong version.
   894  					// Report this more specific error, unless we are a command like 'go work use'
   895  					// or 'go work sync', which will fix the problem after the caller sees the TooNewError
   896  					// and switches to a newer toolchain.
   897  					err = errWorkTooOld(gomod, workFile, tooNew.GoVersion)
   898  				} else {
   899  					err = fmt.Errorf("cannot load module %s listed in go.work file: %w",
   900  						base.ShortPath(filepath.Dir(gomod)), err)
   901  				}
   902  			}
   903  			errs = append(errs, err)
   904  			continue
   905  		}
   906  		if inWorkspaceMode() && !strings.HasPrefix(cfg.CmdName, "work ") {
   907  			// Refuse to use workspace if its go version is too old.
   908  			// Disable this check if we are a workspace command like work use or work sync,
   909  			// which will fix the problem.
   910  			mv := gover.FromGoMod(f)
   911  			wv := gover.FromGoWork(workFile)
   912  			if gover.Compare(mv, wv) > 0 && gover.Compare(mv, gover.GoStrictVersion) >= 0 {
   913  				errs = append(errs, errWorkTooOld(gomod, workFile, mv))
   914  				continue
   915  			}
   916  		}
   917  
   918  		modFiles = append(modFiles, f)
   919  		mainModule := f.Module.Mod
   920  		mainModules = append(mainModules, mainModule)
   921  		indices = append(indices, indexModFile(data, f, mainModule, fixed))
   922  
   923  		if err := module.CheckImportPath(f.Module.Mod.Path); err != nil {
   924  			if pathErr, ok := err.(*module.InvalidPathError); ok {
   925  				pathErr.Kind = "module"
   926  			}
   927  			errs = append(errs, err)
   928  		}
   929  	}
   930  	if len(errs) > 0 {
   931  		return nil, errors.Join(errs...)
   932  	}
   933  
   934  	MainModules = makeMainModules(mainModules, modRoots, modFiles, indices, workFile)
   935  	setDefaultBuildMod() // possibly enable automatic vendoring
   936  	rs := requirementsFromModFiles(ctx, workFile, modFiles, opts)
   937  
   938  	if cfg.BuildMod == "vendor" {
   939  		readVendorList(VendorDir())
   940  		var indexes []*modFileIndex
   941  		var modFiles []*modfile.File
   942  		var modRoots []string
   943  		for _, m := range MainModules.Versions() {
   944  			indexes = append(indexes, MainModules.Index(m))
   945  			modFiles = append(modFiles, MainModules.ModFile(m))
   946  			modRoots = append(modRoots, MainModules.ModRoot(m))
   947  		}
   948  		checkVendorConsistency(indexes, modFiles, modRoots)
   949  		rs.initVendor(vendorList)
   950  	}
   951  
   952  	if inWorkspaceMode() {
   953  		// We don't need to update the mod file so return early.
   954  		requirements = rs
   955  		return rs, nil
   956  	}
   957  
   958  	mainModule := MainModules.mustGetSingleMainModule()
   959  
   960  	if rs.hasRedundantRoot() {
   961  		// If any module path appears more than once in the roots, we know that the
   962  		// go.mod file needs to be updated even though we have not yet loaded any
   963  		// transitive dependencies.
   964  		var err error
   965  		rs, err = updateRoots(ctx, rs.direct, rs, nil, nil, false)
   966  		if err != nil {
   967  			return nil, err
   968  		}
   969  	}
   970  
   971  	if MainModules.Index(mainModule).goVersion == "" && rs.pruning != workspace {
   972  		// TODO(#45551): Do something more principled instead of checking
   973  		// cfg.CmdName directly here.
   974  		if cfg.BuildMod == "mod" && cfg.CmdName != "mod graph" && cfg.CmdName != "mod why" {
   975  			// go line is missing from go.mod; add one there and add to derived requirements.
   976  			v := gover.Local()
   977  			if opts != nil && opts.TidyGoVersion != "" {
   978  				v = opts.TidyGoVersion
   979  			}
   980  			addGoStmt(MainModules.ModFile(mainModule), mainModule, v)
   981  			rs = overrideRoots(ctx, rs, []module.Version{{Path: "go", Version: v}})
   982  
   983  			// We need to add a 'go' version to the go.mod file, but we must assume
   984  			// that its existing contents match something between Go 1.11 and 1.16.
   985  			// Go 1.11 through 1.16 do not support graph pruning, but the latest Go
   986  			// version uses a pruned module graph — so we need to convert the
   987  			// requirements to support pruning.
   988  			if gover.Compare(v, gover.ExplicitIndirectVersion) >= 0 {
   989  				var err error
   990  				rs, err = convertPruning(ctx, rs, pruned)
   991  				if err != nil {
   992  					return nil, err
   993  				}
   994  			}
   995  		} else {
   996  			rawGoVersion.Store(mainModule, gover.DefaultGoModVersion)
   997  		}
   998  	}
   999  
  1000  	requirements = rs
  1001  	return requirements, nil
  1002  }
  1003  
  1004  func errWorkTooOld(gomod string, wf *modfile.WorkFile, goVers string) error {
  1005  	return fmt.Errorf("module %s listed in go.work file requires go >= %s, but go.work lists go %s; to update it:\n\tgo work use",
  1006  		base.ShortPath(filepath.Dir(gomod)), goVers, gover.FromGoWork(wf))
  1007  }
  1008  
  1009  // CreateModFile initializes a new module by creating a go.mod file.
  1010  //
  1011  // If modPath is empty, CreateModFile will attempt to infer the path from the
  1012  // directory location within GOPATH.
  1013  //
  1014  // If a vendoring configuration file is present, CreateModFile will attempt to
  1015  // translate it to go.mod directives. The resulting build list may not be
  1016  // exactly the same as in the legacy configuration (for example, we can't get
  1017  // packages at multiple versions from the same module).
  1018  func CreateModFile(ctx context.Context, modPath string) {
  1019  	modRoot := base.Cwd()
  1020  	modRoots = []string{modRoot}
  1021  	Init()
  1022  	modFilePath := modFilePath(modRoot)
  1023  	if _, err := fsys.Stat(modFilePath); err == nil {
  1024  		base.Fatalf("go: %s already exists", modFilePath)
  1025  	}
  1026  
  1027  	if modPath == "" {
  1028  		var err error
  1029  		modPath, err = findModulePath(modRoot)
  1030  		if err != nil {
  1031  			base.Fatal(err)
  1032  		}
  1033  	} else if err := module.CheckImportPath(modPath); err != nil {
  1034  		if pathErr, ok := err.(*module.InvalidPathError); ok {
  1035  			pathErr.Kind = "module"
  1036  			// Same as build.IsLocalPath()
  1037  			if pathErr.Path == "." || pathErr.Path == ".." ||
  1038  				strings.HasPrefix(pathErr.Path, "./") || strings.HasPrefix(pathErr.Path, "../") {
  1039  				pathErr.Err = errors.New("is a local import path")
  1040  			}
  1041  		}
  1042  		base.Fatal(err)
  1043  	} else if _, _, ok := module.SplitPathVersion(modPath); !ok {
  1044  		if strings.HasPrefix(modPath, "gopkg.in/") {
  1045  			invalidMajorVersionMsg := fmt.Errorf("module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN:\n\tgo mod init %s", suggestGopkgIn(modPath))
  1046  			base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg)
  1047  		}
  1048  		invalidMajorVersionMsg := fmt.Errorf("major version suffixes must be in the form of /vN and are only allowed for v2 or later:\n\tgo mod init %s", suggestModulePath(modPath))
  1049  		base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg)
  1050  	}
  1051  
  1052  	fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", modPath)
  1053  	modFile := new(modfile.File)
  1054  	modFile.AddModuleStmt(modPath)
  1055  	MainModules = makeMainModules([]module.Version{modFile.Module.Mod}, []string{modRoot}, []*modfile.File{modFile}, []*modFileIndex{nil}, nil)
  1056  	addGoStmt(modFile, modFile.Module.Mod, gover.Local()) // Add the go directive before converted module requirements.
  1057  
  1058  	rs := requirementsFromModFiles(ctx, nil, []*modfile.File{modFile}, nil)
  1059  	rs, err := updateRoots(ctx, rs.direct, rs, nil, nil, false)
  1060  	if err != nil {
  1061  		base.Fatal(err)
  1062  	}
  1063  	requirements = rs
  1064  	if err := commitRequirements(ctx, WriteOpts{}); err != nil {
  1065  		base.Fatal(err)
  1066  	}
  1067  
  1068  	// Suggest running 'go mod tidy' unless the project is empty. Even if we
  1069  	// imported all the correct requirements above, we're probably missing
  1070  	// some sums, so the next build command in -mod=readonly will likely fail.
  1071  	//
  1072  	// We look for non-hidden .go files or subdirectories to determine whether
  1073  	// this is an existing project. Walking the tree for packages would be more
  1074  	// accurate, but could take much longer.
  1075  	empty := true
  1076  	files, _ := os.ReadDir(modRoot)
  1077  	for _, f := range files {
  1078  		name := f.Name()
  1079  		if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
  1080  			continue
  1081  		}
  1082  		if strings.HasSuffix(name, ".go") || f.IsDir() {
  1083  			empty = false
  1084  			break
  1085  		}
  1086  	}
  1087  	if !empty {
  1088  		fmt.Fprintf(os.Stderr, "go: to add module requirements and sums:\n\tgo mod tidy\n")
  1089  	}
  1090  }
  1091  
  1092  // fixVersion returns a modfile.VersionFixer implemented using the Query function.
  1093  //
  1094  // It resolves commit hashes and branch names to versions,
  1095  // canonicalizes versions that appeared in early vgo drafts,
  1096  // and does nothing for versions that already appear to be canonical.
  1097  //
  1098  // The VersionFixer sets 'fixed' if it ever returns a non-canonical version.
  1099  func fixVersion(ctx context.Context, fixed *bool) modfile.VersionFixer {
  1100  	return func(path, vers string) (resolved string, err error) {
  1101  		defer func() {
  1102  			if err == nil && resolved != vers {
  1103  				*fixed = true
  1104  			}
  1105  		}()
  1106  
  1107  		// Special case: remove the old -gopkgin- hack.
  1108  		if strings.HasPrefix(path, "gopkg.in/") && strings.Contains(vers, "-gopkgin-") {
  1109  			vers = vers[strings.Index(vers, "-gopkgin-")+len("-gopkgin-"):]
  1110  		}
  1111  
  1112  		// fixVersion is called speculatively on every
  1113  		// module, version pair from every go.mod file.
  1114  		// Avoid the query if it looks OK.
  1115  		_, pathMajor, ok := module.SplitPathVersion(path)
  1116  		if !ok {
  1117  			return "", &module.ModuleError{
  1118  				Path: path,
  1119  				Err: &module.InvalidVersionError{
  1120  					Version: vers,
  1121  					Err:     fmt.Errorf("malformed module path %q", path),
  1122  				},
  1123  			}
  1124  		}
  1125  		if vers != "" && module.CanonicalVersion(vers) == vers {
  1126  			if err := module.CheckPathMajor(vers, pathMajor); err != nil {
  1127  				return "", module.VersionError(module.Version{Path: path, Version: vers}, err)
  1128  			}
  1129  			return vers, nil
  1130  		}
  1131  
  1132  		info, err := Query(ctx, path, vers, "", nil)
  1133  		if err != nil {
  1134  			return "", err
  1135  		}
  1136  		return info.Version, nil
  1137  	}
  1138  }
  1139  
  1140  // AllowMissingModuleImports allows import paths to be resolved to modules
  1141  // when there is no module root. Normally, this is forbidden because it's slow
  1142  // and there's no way to make the result reproducible, but some commands
  1143  // like 'go get' are expected to do this.
  1144  //
  1145  // This function affects the default cfg.BuildMod when outside of a module,
  1146  // so it can only be called prior to Init.
  1147  func AllowMissingModuleImports() {
  1148  	if initialized {
  1149  		panic("AllowMissingModuleImports after Init")
  1150  	}
  1151  	allowMissingModuleImports = true
  1152  }
  1153  
  1154  // makeMainModules creates a MainModuleSet and associated variables according to
  1155  // the given main modules.
  1156  func makeMainModules(ms []module.Version, rootDirs []string, modFiles []*modfile.File, indices []*modFileIndex, workFile *modfile.WorkFile) *MainModuleSet {
  1157  	for _, m := range ms {
  1158  		if m.Version != "" {
  1159  			panic("mainModulesCalled with module.Version with non empty Version field: " + fmt.Sprintf("%#v", m))
  1160  		}
  1161  	}
  1162  	modRootContainingCWD := findModuleRoot(base.Cwd())
  1163  	mainModules := &MainModuleSet{
  1164  		versions:        slices.Clip(ms),
  1165  		inGorootSrc:     map[module.Version]bool{},
  1166  		pathPrefix:      map[module.Version]string{},
  1167  		modRoot:         map[module.Version]string{},
  1168  		modFiles:        map[module.Version]*modfile.File{},
  1169  		indices:         map[module.Version]*modFileIndex{},
  1170  		highestReplaced: map[string]string{},
  1171  		workFile:        workFile,
  1172  	}
  1173  	var workFileReplaces []*modfile.Replace
  1174  	if workFile != nil {
  1175  		workFileReplaces = workFile.Replace
  1176  		mainModules.workFileReplaceMap = toReplaceMap(workFile.Replace)
  1177  	}
  1178  	mainModulePaths := make(map[string]bool)
  1179  	for _, m := range ms {
  1180  		if mainModulePaths[m.Path] {
  1181  			base.Errorf("go: module %s appears multiple times in workspace", m.Path)
  1182  		}
  1183  		mainModulePaths[m.Path] = true
  1184  	}
  1185  	replacedByWorkFile := make(map[string]bool)
  1186  	replacements := make(map[module.Version]module.Version)
  1187  	for _, r := range workFileReplaces {
  1188  		if mainModulePaths[r.Old.Path] && r.Old.Version == "" {
  1189  			base.Errorf("go: workspace module %v is replaced at all versions in the go.work file. To fix, remove the replacement from the go.work file or specify the version at which to replace the module.", r.Old.Path)
  1190  		}
  1191  		replacedByWorkFile[r.Old.Path] = true
  1192  		v, ok := mainModules.highestReplaced[r.Old.Path]
  1193  		if !ok || gover.ModCompare(r.Old.Path, r.Old.Version, v) > 0 {
  1194  			mainModules.highestReplaced[r.Old.Path] = r.Old.Version
  1195  		}
  1196  		replacements[r.Old] = r.New
  1197  	}
  1198  	for i, m := range ms {
  1199  		mainModules.pathPrefix[m] = m.Path
  1200  		mainModules.modRoot[m] = rootDirs[i]
  1201  		mainModules.modFiles[m] = modFiles[i]
  1202  		mainModules.indices[m] = indices[i]
  1203  
  1204  		if mainModules.modRoot[m] == modRootContainingCWD {
  1205  			mainModules.modContainingCWD = m
  1206  		}
  1207  
  1208  		if rel := search.InDir(rootDirs[i], cfg.GOROOTsrc); rel != "" {
  1209  			mainModules.inGorootSrc[m] = true
  1210  			if m.Path == "std" {
  1211  				// The "std" module in GOROOT/src is the Go standard library. Unlike other
  1212  				// modules, the packages in the "std" module have no import-path prefix.
  1213  				//
  1214  				// Modules named "std" outside of GOROOT/src do not receive this special
  1215  				// treatment, so it is possible to run 'go test .' in other GOROOTs to
  1216  				// test individual packages using a combination of the modified package
  1217  				// and the ordinary standard library.
  1218  				// (See https://golang.org/issue/30756.)
  1219  				mainModules.pathPrefix[m] = ""
  1220  			}
  1221  		}
  1222  
  1223  		if modFiles[i] != nil {
  1224  			curModuleReplaces := make(map[module.Version]bool)
  1225  			for _, r := range modFiles[i].Replace {
  1226  				if replacedByWorkFile[r.Old.Path] {
  1227  					continue
  1228  				}
  1229  				var newV module.Version = r.New
  1230  				if WorkFilePath() != "" && newV.Version == "" && !filepath.IsAbs(newV.Path) {
  1231  					// Since we are in a workspace, we may be loading replacements from
  1232  					// multiple go.mod files. Relative paths in those replacement are
  1233  					// relative to the go.mod file, not the workspace, so the same string
  1234  					// may refer to two different paths and different strings may refer to
  1235  					// the same path. Convert them all to be absolute instead.
  1236  					//
  1237  					// (We could do this outside of a workspace too, but it would mean that
  1238  					// replacement paths in error strings needlessly differ from what's in
  1239  					// the go.mod file.)
  1240  					newV.Path = filepath.Join(rootDirs[i], newV.Path)
  1241  				}
  1242  				if prev, ok := replacements[r.Old]; ok && !curModuleReplaces[r.Old] && prev != newV {
  1243  					base.Fatalf("go: conflicting replacements for %v:\n\t%v\n\t%v\nuse \"go work edit -replace %v=[override]\" to resolve", r.Old, prev, newV, r.Old)
  1244  				}
  1245  				curModuleReplaces[r.Old] = true
  1246  				replacements[r.Old] = newV
  1247  
  1248  				v, ok := mainModules.highestReplaced[r.Old.Path]
  1249  				if !ok || gover.ModCompare(r.Old.Path, r.Old.Version, v) > 0 {
  1250  					mainModules.highestReplaced[r.Old.Path] = r.Old.Version
  1251  				}
  1252  			}
  1253  		}
  1254  	}
  1255  	return mainModules
  1256  }
  1257  
  1258  // requirementsFromModFiles returns the set of non-excluded requirements from
  1259  // the global modFile.
  1260  func requirementsFromModFiles(ctx context.Context, workFile *modfile.WorkFile, modFiles []*modfile.File, opts *PackageOpts) *Requirements {
  1261  	var roots []module.Version
  1262  	direct := map[string]bool{}
  1263  	var pruning modPruning
  1264  	if inWorkspaceMode() {
  1265  		pruning = workspace
  1266  		roots = make([]module.Version, len(MainModules.Versions()), 2+len(MainModules.Versions()))
  1267  		copy(roots, MainModules.Versions())
  1268  		goVersion := gover.FromGoWork(workFile)
  1269  		var toolchain string
  1270  		if workFile.Toolchain != nil {
  1271  			toolchain = workFile.Toolchain.Name
  1272  		}
  1273  		roots = appendGoAndToolchainRoots(roots, goVersion, toolchain, direct)
  1274  	} else {
  1275  		pruning = pruningForGoVersion(MainModules.GoVersion())
  1276  		if len(modFiles) != 1 {
  1277  			panic(fmt.Errorf("requirementsFromModFiles called with %v modfiles outside workspace mode", len(modFiles)))
  1278  		}
  1279  		modFile := modFiles[0]
  1280  		roots, direct = rootsFromModFile(MainModules.mustGetSingleMainModule(), modFile, withToolchainRoot)
  1281  	}
  1282  
  1283  	gover.ModSort(roots)
  1284  	rs := newRequirements(pruning, roots, direct)
  1285  	return rs
  1286  }
  1287  
  1288  type addToolchainRoot bool
  1289  
  1290  const (
  1291  	omitToolchainRoot addToolchainRoot = false
  1292  	withToolchainRoot                  = true
  1293  )
  1294  
  1295  func rootsFromModFile(m module.Version, modFile *modfile.File, addToolchainRoot addToolchainRoot) (roots []module.Version, direct map[string]bool) {
  1296  	direct = make(map[string]bool)
  1297  	padding := 2 // Add padding for the toolchain and go version, added upon return.
  1298  	if !addToolchainRoot {
  1299  		padding = 1
  1300  	}
  1301  	roots = make([]module.Version, 0, padding+len(modFile.Require))
  1302  	for _, r := range modFile.Require {
  1303  		if index := MainModules.Index(m); index != nil && index.exclude[r.Mod] {
  1304  			if cfg.BuildMod == "mod" {
  1305  				fmt.Fprintf(os.Stderr, "go: dropping requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
  1306  			} else {
  1307  				fmt.Fprintf(os.Stderr, "go: ignoring requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
  1308  			}
  1309  			continue
  1310  		}
  1311  
  1312  		roots = append(roots, r.Mod)
  1313  		if !r.Indirect {
  1314  			direct[r.Mod.Path] = true
  1315  		}
  1316  	}
  1317  	goVersion := gover.FromGoMod(modFile)
  1318  	var toolchain string
  1319  	if addToolchainRoot && modFile.Toolchain != nil {
  1320  		toolchain = modFile.Toolchain.Name
  1321  	}
  1322  	roots = appendGoAndToolchainRoots(roots, goVersion, toolchain, direct)
  1323  	return roots, direct
  1324  }
  1325  
  1326  func appendGoAndToolchainRoots(roots []module.Version, goVersion, toolchain string, direct map[string]bool) []module.Version {
  1327  	// Add explicit go and toolchain versions, inferring as needed.
  1328  	roots = append(roots, module.Version{Path: "go", Version: goVersion})
  1329  	direct["go"] = true // Every module directly uses the language and runtime.
  1330  
  1331  	if toolchain != "" {
  1332  		roots = append(roots, module.Version{Path: "toolchain", Version: toolchain})
  1333  		// Leave the toolchain as indirect: nothing in the user's module directly
  1334  		// imports a package from the toolchain, and (like an indirect dependency in
  1335  		// a module without graph pruning) we may remove the toolchain line
  1336  		// automatically if the 'go' version is changed so that it implies the exact
  1337  		// same toolchain.
  1338  	}
  1339  	return roots
  1340  }
  1341  
  1342  // setDefaultBuildMod sets a default value for cfg.BuildMod if the -mod flag
  1343  // wasn't provided. setDefaultBuildMod may be called multiple times.
  1344  func setDefaultBuildMod() {
  1345  	if cfg.BuildModExplicit {
  1346  		if inWorkspaceMode() && cfg.BuildMod != "readonly" && cfg.BuildMod != "vendor" {
  1347  			base.Fatalf("go: -mod may only be set to readonly or vendor when in workspace mode, but it is set to %q"+
  1348  				"\n\tRemove the -mod flag to use the default readonly value, "+
  1349  				"\n\tor set GOWORK=off to disable workspace mode.", cfg.BuildMod)
  1350  		}
  1351  		// Don't override an explicit '-mod=' argument.
  1352  		return
  1353  	}
  1354  
  1355  	// TODO(#40775): commands should pass in the module mode as an option
  1356  	// to modload functions instead of relying on an implicit setting
  1357  	// based on command name.
  1358  	switch cfg.CmdName {
  1359  	case "get", "mod download", "mod init", "mod tidy", "work sync":
  1360  		// These commands are intended to update go.mod and go.sum.
  1361  		cfg.BuildMod = "mod"
  1362  		return
  1363  	case "mod graph", "mod verify", "mod why":
  1364  		// These commands should not update go.mod or go.sum, but they should be
  1365  		// able to fetch modules not in go.sum and should not report errors if
  1366  		// go.mod is inconsistent. They're useful for debugging, and they need
  1367  		// to work in buggy situations.
  1368  		cfg.BuildMod = "mod"
  1369  		return
  1370  	case "mod vendor", "work vendor":
  1371  		cfg.BuildMod = "readonly"
  1372  		return
  1373  	}
  1374  	if modRoots == nil {
  1375  		if allowMissingModuleImports {
  1376  			cfg.BuildMod = "mod"
  1377  		} else {
  1378  			cfg.BuildMod = "readonly"
  1379  		}
  1380  		return
  1381  	}
  1382  
  1383  	if len(modRoots) >= 1 {
  1384  		var goVersion string
  1385  		var versionSource string
  1386  		if inWorkspaceMode() {
  1387  			versionSource = "go.work"
  1388  			if wfg := MainModules.WorkFile().Go; wfg != nil {
  1389  				goVersion = wfg.Version
  1390  			}
  1391  		} else {
  1392  			versionSource = "go.mod"
  1393  			index := MainModules.GetSingleIndexOrNil()
  1394  			if index != nil {
  1395  				goVersion = index.goVersion
  1396  			}
  1397  		}
  1398  		vendorDir := ""
  1399  		if workFilePath != "" {
  1400  			vendorDir = filepath.Join(filepath.Dir(workFilePath), "vendor")
  1401  		} else {
  1402  			if len(modRoots) != 1 {
  1403  				panic(fmt.Errorf("outside workspace mode, but have %v modRoots", modRoots))
  1404  			}
  1405  			vendorDir = filepath.Join(modRoots[0], "vendor")
  1406  		}
  1407  		if fi, err := fsys.Stat(vendorDir); err == nil && fi.IsDir() {
  1408  			modGo := "unspecified"
  1409  			if goVersion != "" {
  1410  				if gover.Compare(goVersion, "1.14") < 0 {
  1411  					// The go version is less than 1.14. Don't set -mod=vendor by default.
  1412  					// Since a vendor directory exists, we should record why we didn't use it.
  1413  					// This message won't normally be shown, but it may appear with import errors.
  1414  					cfg.BuildModReason = fmt.Sprintf("Go version in "+versionSource+" is %s, so vendor directory was not used.", modGo)
  1415  				} else {
  1416  					vendoredWorkspace, err := modulesTextIsForWorkspace(vendorDir)
  1417  					if err != nil {
  1418  						base.Fatalf("go: reading modules.txt for vendor directory: %v", err)
  1419  					}
  1420  					if vendoredWorkspace != (versionSource == "go.work") {
  1421  						if vendoredWorkspace {
  1422  							cfg.BuildModReason = "Outside workspace mode, but vendor directory is for a workspace."
  1423  						} else {
  1424  							cfg.BuildModReason = "In workspace mode, but vendor directory is not for a workspace"
  1425  						}
  1426  					} else {
  1427  						// The Go version is at least 1.14, a vendor directory exists, and
  1428  						// the modules.txt was generated in the same mode the command is running in.
  1429  						// Set -mod=vendor by default.
  1430  						cfg.BuildMod = "vendor"
  1431  						cfg.BuildModReason = "Go version in " + versionSource + " is at least 1.14 and vendor directory exists."
  1432  						return
  1433  					}
  1434  				}
  1435  				modGo = goVersion
  1436  			}
  1437  
  1438  		}
  1439  	}
  1440  
  1441  	cfg.BuildMod = "readonly"
  1442  }
  1443  
  1444  func modulesTextIsForWorkspace(vendorDir string) (bool, error) {
  1445  	f, err := fsys.Open(filepath.Join(vendorDir, "modules.txt"))
  1446  	if errors.Is(err, os.ErrNotExist) {
  1447  		// Some vendor directories exist that don't contain modules.txt.
  1448  		// This mostly happens when converting to modules.
  1449  		// We want to preserve the behavior that mod=vendor is set (even though
  1450  		// readVendorList does nothing in that case).
  1451  		return false, nil
  1452  	}
  1453  	if err != nil {
  1454  		return false, err
  1455  	}
  1456  	var buf [512]byte
  1457  	n, err := f.Read(buf[:])
  1458  	if err != nil && err != io.EOF {
  1459  		return false, err
  1460  	}
  1461  	line, _, _ := strings.Cut(string(buf[:n]), "\n")
  1462  	if annotations, ok := strings.CutPrefix(line, "## "); ok {
  1463  		for _, entry := range strings.Split(annotations, ";") {
  1464  			entry = strings.TrimSpace(entry)
  1465  			if entry == "workspace" {
  1466  				return true, nil
  1467  			}
  1468  		}
  1469  	}
  1470  	return false, nil
  1471  }
  1472  
  1473  func mustHaveCompleteRequirements() bool {
  1474  	return cfg.BuildMod != "mod" && !inWorkspaceMode()
  1475  }
  1476  
  1477  // addGoStmt adds a go directive to the go.mod file if it does not already
  1478  // include one. The 'go' version added, if any, is the latest version supported
  1479  // by this toolchain.
  1480  func addGoStmt(modFile *modfile.File, mod module.Version, v string) {
  1481  	if modFile.Go != nil && modFile.Go.Version != "" {
  1482  		return
  1483  	}
  1484  	forceGoStmt(modFile, mod, v)
  1485  }
  1486  
  1487  func forceGoStmt(modFile *modfile.File, mod module.Version, v string) {
  1488  	if err := modFile.AddGoStmt(v); err != nil {
  1489  		base.Fatalf("go: internal error: %v", err)
  1490  	}
  1491  	rawGoVersion.Store(mod, v)
  1492  }
  1493  
  1494  var altConfigs = []string{
  1495  	".git/config",
  1496  }
  1497  
  1498  func findModuleRoot(dir string) (roots string) {
  1499  	if dir == "" {
  1500  		panic("dir not set")
  1501  	}
  1502  	dir = filepath.Clean(dir)
  1503  
  1504  	// Look for enclosing go.mod.
  1505  	for {
  1506  		if fi, err := fsys.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {
  1507  			return dir
  1508  		}
  1509  		d := filepath.Dir(dir)
  1510  		if d == dir {
  1511  			break
  1512  		}
  1513  		dir = d
  1514  	}
  1515  	return ""
  1516  }
  1517  
  1518  func findWorkspaceFile(dir string) (root string) {
  1519  	if dir == "" {
  1520  		panic("dir not set")
  1521  	}
  1522  	dir = filepath.Clean(dir)
  1523  
  1524  	// Look for enclosing go.mod.
  1525  	for {
  1526  		f := filepath.Join(dir, "go.work")
  1527  		if fi, err := fsys.Stat(f); err == nil && !fi.IsDir() {
  1528  			return f
  1529  		}
  1530  		d := filepath.Dir(dir)
  1531  		if d == dir {
  1532  			break
  1533  		}
  1534  		if d == cfg.GOROOT {
  1535  			// As a special case, don't cross GOROOT to find a go.work file.
  1536  			// The standard library and commands built in go always use the vendored
  1537  			// dependencies, so avoid using a most likely irrelevant go.work file.
  1538  			return ""
  1539  		}
  1540  		dir = d
  1541  	}
  1542  	return ""
  1543  }
  1544  
  1545  func findAltConfig(dir string) (root, name string) {
  1546  	if dir == "" {
  1547  		panic("dir not set")
  1548  	}
  1549  	dir = filepath.Clean(dir)
  1550  	if rel := search.InDir(dir, cfg.BuildContext.GOROOT); rel != "" {
  1551  		// Don't suggest creating a module from $GOROOT/.git/config
  1552  		// or a config file found in any parent of $GOROOT (see #34191).
  1553  		return "", ""
  1554  	}
  1555  	for {
  1556  		for _, name := range altConfigs {
  1557  			if fi, err := fsys.Stat(filepath.Join(dir, name)); err == nil && !fi.IsDir() {
  1558  				return dir, name
  1559  			}
  1560  		}
  1561  		d := filepath.Dir(dir)
  1562  		if d == dir {
  1563  			break
  1564  		}
  1565  		dir = d
  1566  	}
  1567  	return "", ""
  1568  }
  1569  
  1570  func findModulePath(dir string) (string, error) {
  1571  	// TODO(bcmills): once we have located a plausible module path, we should
  1572  	// query version control (if available) to verify that it matches the major
  1573  	// version of the most recent tag.
  1574  	// See https://golang.org/issue/29433, https://golang.org/issue/27009, and
  1575  	// https://golang.org/issue/31549.
  1576  
  1577  	// Cast about for import comments,
  1578  	// first in top-level directory, then in subdirectories.
  1579  	list, _ := os.ReadDir(dir)
  1580  	for _, info := range list {
  1581  		if info.Type().IsRegular() && strings.HasSuffix(info.Name(), ".go") {
  1582  			if com := findImportComment(filepath.Join(dir, info.Name())); com != "" {
  1583  				return com, nil
  1584  			}
  1585  		}
  1586  	}
  1587  	for _, info1 := range list {
  1588  		if info1.IsDir() {
  1589  			files, _ := os.ReadDir(filepath.Join(dir, info1.Name()))
  1590  			for _, info2 := range files {
  1591  				if info2.Type().IsRegular() && strings.HasSuffix(info2.Name(), ".go") {
  1592  					if com := findImportComment(filepath.Join(dir, info1.Name(), info2.Name())); com != "" {
  1593  						return path.Dir(com), nil
  1594  					}
  1595  				}
  1596  			}
  1597  		}
  1598  	}
  1599  
  1600  	// Look for Godeps.json declaring import path.
  1601  	data, _ := os.ReadFile(filepath.Join(dir, "Godeps/Godeps.json"))
  1602  	var cfg1 struct{ ImportPath string }
  1603  	json.Unmarshal(data, &cfg1)
  1604  	if cfg1.ImportPath != "" {
  1605  		return cfg1.ImportPath, nil
  1606  	}
  1607  
  1608  	// Look for vendor.json declaring import path.
  1609  	data, _ = os.ReadFile(filepath.Join(dir, "vendor/vendor.json"))
  1610  	var cfg2 struct{ RootPath string }
  1611  	json.Unmarshal(data, &cfg2)
  1612  	if cfg2.RootPath != "" {
  1613  		return cfg2.RootPath, nil
  1614  	}
  1615  
  1616  	// Look for path in GOPATH.
  1617  	var badPathErr error
  1618  	for _, gpdir := range filepath.SplitList(cfg.BuildContext.GOPATH) {
  1619  		if gpdir == "" {
  1620  			continue
  1621  		}
  1622  		if rel := search.InDir(dir, filepath.Join(gpdir, "src")); rel != "" && rel != "." {
  1623  			path := filepath.ToSlash(rel)
  1624  			// gorelease will alert users publishing their modules to fix their paths.
  1625  			if err := module.CheckImportPath(path); err != nil {
  1626  				badPathErr = err
  1627  				break
  1628  			}
  1629  			return path, nil
  1630  		}
  1631  	}
  1632  
  1633  	reason := "outside GOPATH, module path must be specified"
  1634  	if badPathErr != nil {
  1635  		// return a different error message if the module was in GOPATH, but
  1636  		// the module path determined above would be an invalid path.
  1637  		reason = fmt.Sprintf("bad module path inferred from directory in GOPATH: %v", badPathErr)
  1638  	}
  1639  	msg := `cannot determine module path for source directory %s (%s)
  1640  
  1641  Example usage:
  1642  	'go mod init example.com/m' to initialize a v0 or v1 module
  1643  	'go mod init example.com/m/v2' to initialize a v2 module
  1644  
  1645  Run 'go help mod init' for more information.
  1646  `
  1647  	return "", fmt.Errorf(msg, dir, reason)
  1648  }
  1649  
  1650  var (
  1651  	importCommentRE = lazyregexp.New(`(?m)^package[ \t]+[^ \t\r\n/]+[ \t]+//[ \t]+import[ \t]+(\"[^"]+\")[ \t]*\r?\n`)
  1652  )
  1653  
  1654  func findImportComment(file string) string {
  1655  	data, err := os.ReadFile(file)
  1656  	if err != nil {
  1657  		return ""
  1658  	}
  1659  	m := importCommentRE.FindSubmatch(data)
  1660  	if m == nil {
  1661  		return ""
  1662  	}
  1663  	path, err := strconv.Unquote(string(m[1]))
  1664  	if err != nil {
  1665  		return ""
  1666  	}
  1667  	return path
  1668  }
  1669  
  1670  // WriteOpts control the behavior of WriteGoMod.
  1671  type WriteOpts struct {
  1672  	DropToolchain     bool // go get toolchain@none
  1673  	ExplicitToolchain bool // go get has set explicit toolchain version
  1674  
  1675  	// TODO(bcmills): Make 'go mod tidy' update the go version in the Requirements
  1676  	// instead of writing directly to the modfile.File
  1677  	TidyWroteGo bool // Go.Version field already updated by 'go mod tidy'
  1678  }
  1679  
  1680  // WriteGoMod writes the current build list back to go.mod.
  1681  func WriteGoMod(ctx context.Context, opts WriteOpts) error {
  1682  	requirements = LoadModFile(ctx)
  1683  	return commitRequirements(ctx, opts)
  1684  }
  1685  
  1686  // commitRequirements ensures go.mod and go.sum are up to date with the current
  1687  // requirements.
  1688  //
  1689  // In "mod" mode, commitRequirements writes changes to go.mod and go.sum.
  1690  //
  1691  // In "readonly" and "vendor" modes, commitRequirements returns an error if
  1692  // go.mod or go.sum are out of date in a semantically significant way.
  1693  //
  1694  // In workspace mode, commitRequirements only writes changes to go.work.sum.
  1695  func commitRequirements(ctx context.Context, opts WriteOpts) (err error) {
  1696  	if inWorkspaceMode() {
  1697  		// go.mod files aren't updated in workspace mode, but we still want to
  1698  		// update the go.work.sum file.
  1699  		return modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, requirements, addBuildListZipSums), mustHaveCompleteRequirements())
  1700  	}
  1701  	if MainModules.Len() != 1 || MainModules.ModRoot(MainModules.Versions()[0]) == "" {
  1702  		// We aren't in a module, so we don't have anywhere to write a go.mod file.
  1703  		return nil
  1704  	}
  1705  	mainModule := MainModules.mustGetSingleMainModule()
  1706  	modFile := MainModules.ModFile(mainModule)
  1707  	if modFile == nil {
  1708  		// command-line-arguments has no .mod file to write.
  1709  		return nil
  1710  	}
  1711  	modFilePath := modFilePath(MainModules.ModRoot(mainModule))
  1712  
  1713  	var list []*modfile.Require
  1714  	toolchain := ""
  1715  	goVersion := ""
  1716  	for _, m := range requirements.rootModules {
  1717  		if m.Path == "go" {
  1718  			goVersion = m.Version
  1719  			continue
  1720  		}
  1721  		if m.Path == "toolchain" {
  1722  			toolchain = m.Version
  1723  			continue
  1724  		}
  1725  		list = append(list, &modfile.Require{
  1726  			Mod:      m,
  1727  			Indirect: !requirements.direct[m.Path],
  1728  		})
  1729  	}
  1730  
  1731  	// Update go line.
  1732  	// Every MVS graph we consider should have go as a root,
  1733  	// and toolchain is either implied by the go line or explicitly a root.
  1734  	if goVersion == "" {
  1735  		base.Fatalf("go: internal error: missing go root module in WriteGoMod")
  1736  	}
  1737  	if gover.Compare(goVersion, gover.Local()) > 0 {
  1738  		// We cannot assume that we know how to update a go.mod to a newer version.
  1739  		return &gover.TooNewError{What: "updating go.mod", GoVersion: goVersion}
  1740  	}
  1741  	wroteGo := opts.TidyWroteGo
  1742  	if !wroteGo && modFile.Go == nil || modFile.Go.Version != goVersion {
  1743  		alwaysUpdate := cfg.BuildMod == "mod" || cfg.CmdName == "mod tidy" || cfg.CmdName == "get"
  1744  		if modFile.Go == nil && goVersion == gover.DefaultGoModVersion && !alwaysUpdate {
  1745  			// The go.mod has no go line, the implied default Go version matches
  1746  			// what we've computed for the graph, and we're not in one of the
  1747  			// traditional go.mod-updating programs, so leave it alone.
  1748  		} else {
  1749  			wroteGo = true
  1750  			forceGoStmt(modFile, mainModule, goVersion)
  1751  		}
  1752  	}
  1753  	if toolchain == "" {
  1754  		toolchain = "go" + goVersion
  1755  	}
  1756  
  1757  	// For reproducibility, if we are writing a new go line,
  1758  	// and we're not explicitly modifying the toolchain line with 'go get toolchain@something',
  1759  	// and the go version is one that supports switching toolchains,
  1760  	// and the toolchain running right now is newer than the current toolchain line,
  1761  	// then update the toolchain line to record the newer toolchain.
  1762  	//
  1763  	// TODO(#57001): This condition feels too complicated. Can we simplify it?
  1764  	// TODO(#57001): Add more tests for toolchain lines.
  1765  	toolVers := gover.FromToolchain(toolchain)
  1766  	if wroteGo && !opts.DropToolchain && !opts.ExplicitToolchain &&
  1767  		gover.Compare(goVersion, gover.GoStrictVersion) >= 0 &&
  1768  		(gover.Compare(gover.Local(), toolVers) > 0 && !gover.IsLang(gover.Local())) {
  1769  		toolchain = "go" + gover.Local()
  1770  		toolVers = gover.FromToolchain(toolchain)
  1771  	}
  1772  
  1773  	if opts.DropToolchain || toolchain == "go"+goVersion || (gover.Compare(toolVers, gover.GoStrictVersion) < 0 && !opts.ExplicitToolchain) {
  1774  		// go get toolchain@none or toolchain matches go line or isn't valid; drop it.
  1775  		// TODO(#57001): 'go get' should reject explicit toolchains below GoStrictVersion.
  1776  		modFile.DropToolchainStmt()
  1777  	} else {
  1778  		modFile.AddToolchainStmt(toolchain)
  1779  	}
  1780  
  1781  	// Update require blocks.
  1782  	if gover.Compare(goVersion, gover.SeparateIndirectVersion) < 0 {
  1783  		modFile.SetRequire(list)
  1784  	} else {
  1785  		modFile.SetRequireSeparateIndirect(list)
  1786  	}
  1787  	modFile.Cleanup()
  1788  
  1789  	index := MainModules.GetSingleIndexOrNil()
  1790  	dirty := index.modFileIsDirty(modFile)
  1791  	if dirty && cfg.BuildMod != "mod" {
  1792  		// If we're about to fail due to -mod=readonly,
  1793  		// prefer to report a dirty go.mod over a dirty go.sum
  1794  		return errGoModDirty
  1795  	}
  1796  
  1797  	if !dirty && cfg.CmdName != "mod tidy" {
  1798  		// The go.mod file has the same semantic content that it had before
  1799  		// (but not necessarily the same exact bytes).
  1800  		// Don't write go.mod, but write go.sum in case we added or trimmed sums.
  1801  		// 'go mod init' shouldn't write go.sum, since it will be incomplete.
  1802  		if cfg.CmdName != "mod init" {
  1803  			if err := modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, requirements, addBuildListZipSums), mustHaveCompleteRequirements()); err != nil {
  1804  				return err
  1805  			}
  1806  		}
  1807  		return nil
  1808  	}
  1809  	if _, ok := fsys.OverlayPath(modFilePath); ok {
  1810  		if dirty {
  1811  			return errors.New("updates to go.mod needed, but go.mod is part of the overlay specified with -overlay")
  1812  		}
  1813  		return nil
  1814  	}
  1815  
  1816  	new, err := modFile.Format()
  1817  	if err != nil {
  1818  		return err
  1819  	}
  1820  	defer func() {
  1821  		// At this point we have determined to make the go.mod file on disk equal to new.
  1822  		MainModules.SetIndex(mainModule, indexModFile(new, modFile, mainModule, false))
  1823  
  1824  		// Update go.sum after releasing the side lock and refreshing the index.
  1825  		// 'go mod init' shouldn't write go.sum, since it will be incomplete.
  1826  		if cfg.CmdName != "mod init" {
  1827  			if err == nil {
  1828  				err = modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, requirements, addBuildListZipSums), mustHaveCompleteRequirements())
  1829  			}
  1830  		}
  1831  	}()
  1832  
  1833  	// Make a best-effort attempt to acquire the side lock, only to exclude
  1834  	// previous versions of the 'go' command from making simultaneous edits.
  1835  	if unlock, err := modfetch.SideLock(ctx); err == nil {
  1836  		defer unlock()
  1837  	}
  1838  
  1839  	errNoChange := errors.New("no update needed")
  1840  
  1841  	err = lockedfile.Transform(modFilePath, func(old []byte) ([]byte, error) {
  1842  		if bytes.Equal(old, new) {
  1843  			// The go.mod file is already equal to new, possibly as the result of some
  1844  			// other process.
  1845  			return nil, errNoChange
  1846  		}
  1847  
  1848  		if index != nil && !bytes.Equal(old, index.data) {
  1849  			// The contents of the go.mod file have changed. In theory we could add all
  1850  			// of the new modules to the build list, recompute, and check whether any
  1851  			// module in *our* build list got bumped to a different version, but that's
  1852  			// a lot of work for marginal benefit. Instead, fail the command: if users
  1853  			// want to run concurrent commands, they need to start with a complete,
  1854  			// consistent module definition.
  1855  			return nil, fmt.Errorf("existing contents have changed since last read")
  1856  		}
  1857  
  1858  		return new, nil
  1859  	})
  1860  
  1861  	if err != nil && err != errNoChange {
  1862  		return fmt.Errorf("updating go.mod: %w", err)
  1863  	}
  1864  	return nil
  1865  }
  1866  
  1867  // keepSums returns the set of modules (and go.mod file entries) for which
  1868  // checksums would be needed in order to reload the same set of packages
  1869  // loaded by the most recent call to LoadPackages or ImportFromFiles,
  1870  // including any go.mod files needed to reconstruct the MVS result
  1871  // or identify go versions,
  1872  // in addition to the checksums for every module in keepMods.
  1873  func keepSums(ctx context.Context, ld *loader, rs *Requirements, which whichSums) map[module.Version]bool {
  1874  	// Every module in the full module graph contributes its requirements,
  1875  	// so in order to ensure that the build list itself is reproducible,
  1876  	// we need sums for every go.mod in the graph (regardless of whether
  1877  	// that version is selected).
  1878  	keep := make(map[module.Version]bool)
  1879  
  1880  	// Add entries for modules in the build list with paths that are prefixes of
  1881  	// paths of loaded packages. We need to retain sums for all of these modules —
  1882  	// not just the modules containing the actual packages — in order to rule out
  1883  	// ambiguous import errors the next time we load the package.
  1884  	keepModSumsForZipSums := true
  1885  	if ld == nil {
  1886  		if gover.Compare(MainModules.GoVersion(), gover.TidyGoModSumVersion) < 0 && cfg.BuildMod != "mod" {
  1887  			keepModSumsForZipSums = false
  1888  		}
  1889  	} else {
  1890  		keepPkgGoModSums := true
  1891  		if gover.Compare(ld.requirements.GoVersion(), gover.TidyGoModSumVersion) < 0 && (ld.Tidy || cfg.BuildMod != "mod") {
  1892  			keepPkgGoModSums = false
  1893  			keepModSumsForZipSums = false
  1894  		}
  1895  		for _, pkg := range ld.pkgs {
  1896  			// We check pkg.mod.Path here instead of pkg.inStd because the
  1897  			// pseudo-package "C" is not in std, but not provided by any module (and
  1898  			// shouldn't force loading the whole module graph).
  1899  			if pkg.testOf != nil || (pkg.mod.Path == "" && pkg.err == nil) || module.CheckImportPath(pkg.path) != nil {
  1900  				continue
  1901  			}
  1902  
  1903  			// We need the checksum for the go.mod file for pkg.mod
  1904  			// so that we know what Go version to use to compile pkg.
  1905  			// However, we didn't do so before Go 1.21, and the bug is relatively
  1906  			// minor, so we maintain the previous (buggy) behavior in 'go mod tidy' to
  1907  			// avoid introducing unnecessary churn.
  1908  			if keepPkgGoModSums {
  1909  				r := resolveReplacement(pkg.mod)
  1910  				keep[modkey(r)] = true
  1911  			}
  1912  
  1913  			if rs.pruning == pruned && pkg.mod.Path != "" {
  1914  				if v, ok := rs.rootSelected(pkg.mod.Path); ok && v == pkg.mod.Version {
  1915  					// pkg was loaded from a root module, and because the main module has
  1916  					// a pruned module graph we do not check non-root modules for
  1917  					// conflicts for packages that can be found in roots. So we only need
  1918  					// the checksums for the root modules that may contain pkg, not all
  1919  					// possible modules.
  1920  					for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) {
  1921  						if v, ok := rs.rootSelected(prefix); ok && v != "none" {
  1922  							m := module.Version{Path: prefix, Version: v}
  1923  							r := resolveReplacement(m)
  1924  							keep[r] = true
  1925  						}
  1926  					}
  1927  					continue
  1928  				}
  1929  			}
  1930  
  1931  			mg, _ := rs.Graph(ctx)
  1932  			for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) {
  1933  				if v := mg.Selected(prefix); v != "none" {
  1934  					m := module.Version{Path: prefix, Version: v}
  1935  					r := resolveReplacement(m)
  1936  					keep[r] = true
  1937  				}
  1938  			}
  1939  		}
  1940  	}
  1941  
  1942  	if rs.graph.Load() == nil {
  1943  		// We haven't needed to load the module graph so far.
  1944  		// Save sums for the root modules (or their replacements), but don't
  1945  		// incur the cost of loading the graph just to find and retain the sums.
  1946  		for _, m := range rs.rootModules {
  1947  			r := resolveReplacement(m)
  1948  			keep[modkey(r)] = true
  1949  			if which == addBuildListZipSums {
  1950  				keep[r] = true
  1951  			}
  1952  		}
  1953  	} else {
  1954  		mg, _ := rs.Graph(ctx)
  1955  		mg.WalkBreadthFirst(func(m module.Version) {
  1956  			if _, ok := mg.RequiredBy(m); ok {
  1957  				// The requirements from m's go.mod file are present in the module graph,
  1958  				// so they are relevant to the MVS result regardless of whether m was
  1959  				// actually selected.
  1960  				r := resolveReplacement(m)
  1961  				keep[modkey(r)] = true
  1962  			}
  1963  		})
  1964  
  1965  		if which == addBuildListZipSums {
  1966  			for _, m := range mg.BuildList() {
  1967  				r := resolveReplacement(m)
  1968  				if keepModSumsForZipSums {
  1969  					keep[modkey(r)] = true // we need the go version from the go.mod file to do anything useful with the zipfile
  1970  				}
  1971  				keep[r] = true
  1972  			}
  1973  		}
  1974  	}
  1975  
  1976  	return keep
  1977  }
  1978  
  1979  type whichSums int8
  1980  
  1981  const (
  1982  	loadedZipSumsOnly = whichSums(iota)
  1983  	addBuildListZipSums
  1984  )
  1985  
  1986  // modkey returns the module.Version under which the checksum for m's go.mod
  1987  // file is stored in the go.sum file.
  1988  func modkey(m module.Version) module.Version {
  1989  	return module.Version{Path: m.Path, Version: m.Version + "/go.mod"}
  1990  }
  1991  
  1992  func suggestModulePath(path string) string {
  1993  	var m string
  1994  
  1995  	i := len(path)
  1996  	for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9' || path[i-1] == '.') {
  1997  		i--
  1998  	}
  1999  	url := path[:i]
  2000  	url = strings.TrimSuffix(url, "/v")
  2001  	url = strings.TrimSuffix(url, "/")
  2002  
  2003  	f := func(c rune) bool {
  2004  		return c > '9' || c < '0'
  2005  	}
  2006  	s := strings.FieldsFunc(path[i:], f)
  2007  	if len(s) > 0 {
  2008  		m = s[0]
  2009  	}
  2010  	m = strings.TrimLeft(m, "0")
  2011  	if m == "" || m == "1" {
  2012  		return url + "/v2"
  2013  	}
  2014  
  2015  	return url + "/v" + m
  2016  }
  2017  
  2018  func suggestGopkgIn(path string) string {
  2019  	var m string
  2020  	i := len(path)
  2021  	for i > 0 && (('0' <= path[i-1] && path[i-1] <= '9') || (path[i-1] == '.')) {
  2022  		i--
  2023  	}
  2024  	url := path[:i]
  2025  	url = strings.TrimSuffix(url, ".v")
  2026  	url = strings.TrimSuffix(url, "/v")
  2027  	url = strings.TrimSuffix(url, "/")
  2028  
  2029  	f := func(c rune) bool {
  2030  		return c > '9' || c < '0'
  2031  	}
  2032  	s := strings.FieldsFunc(path, f)
  2033  	if len(s) > 0 {
  2034  		m = s[0]
  2035  	}
  2036  
  2037  	m = strings.TrimLeft(m, "0")
  2038  
  2039  	if m == "" {
  2040  		return url + ".v1"
  2041  	}
  2042  	return url + ".v" + m
  2043  }