Source file src/cmd/go/internal/modload/init.go

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

View as plain text