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

View as plain text