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

View as plain text