Black Lives Matter. Support the Equal Justice Initiative.

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

Documentation: cmd/go/internal/modload

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package modload
     6  
     7  import (
     8  	"bytes"
     9  	"context"
    10  	"encoding/json"
    11  	"errors"
    12  	"fmt"
    13  	"go/build"
    14  	"internal/lazyregexp"
    15  	"os"
    16  	"path"
    17  	"path/filepath"
    18  	"sort"
    19  	"strconv"
    20  	"strings"
    21  	"sync"
    22  
    23  	"cmd/go/internal/base"
    24  	"cmd/go/internal/cfg"
    25  	"cmd/go/internal/fsys"
    26  	"cmd/go/internal/lockedfile"
    27  	"cmd/go/internal/modconv"
    28  	"cmd/go/internal/modfetch"
    29  	"cmd/go/internal/mvs"
    30  	"cmd/go/internal/search"
    31  	"cmd/go/internal/str"
    32  
    33  	"golang.org/x/mod/modfile"
    34  	"golang.org/x/mod/module"
    35  	"golang.org/x/mod/semver"
    36  )
    37  
    38  var (
    39  	initialized bool
    40  
    41  	modRoot string
    42  	Target  module.Version
    43  
    44  	// targetPrefix is the path prefix for packages in Target, without a trailing
    45  	// slash. For most modules, targetPrefix is just Target.Path, but the
    46  	// standard-library module "std" has an empty prefix.
    47  	targetPrefix string
    48  
    49  	// targetInGorootSrc caches whether modRoot is within GOROOT/src.
    50  	// The "std" module is special within GOROOT/src, but not otherwise.
    51  	targetInGorootSrc bool
    52  
    53  	gopath string
    54  
    55  	// RootMode determines whether a module root is needed.
    56  	RootMode Root
    57  
    58  	// ForceUseModules may be set to force modules to be enabled when
    59  	// GO111MODULE=auto or to report an error when GO111MODULE=off.
    60  	ForceUseModules bool
    61  
    62  	allowMissingModuleImports bool
    63  )
    64  
    65  type Root int
    66  
    67  const (
    68  	// AutoRoot is the default for most commands. modload.Init will look for
    69  	// a go.mod file in the current directory or any parent. If none is found,
    70  	// modules may be disabled (GO111MODULE=on) or commands may run in a
    71  	// limited module mode.
    72  	AutoRoot Root = iota
    73  
    74  	// NoRoot is used for commands that run in module mode and ignore any go.mod
    75  	// file the current directory or in parent directories.
    76  	NoRoot
    77  
    78  	// NeedRoot is used for commands that must run in module mode and don't
    79  	// make sense without a main module.
    80  	NeedRoot
    81  )
    82  
    83  // ModFile returns the parsed go.mod file.
    84  //
    85  // Note that after calling LoadPackages or LoadAllModules,
    86  // the require statements in the modfile.File are no longer
    87  // the source of truth and will be ignored: edits made directly
    88  // will be lost at the next call to WriteGoMod.
    89  // To make permanent changes to the require statements
    90  // in go.mod, edit it before loading.
    91  func ModFile() *modfile.File {
    92  	Init()
    93  	if modFile == nil {
    94  		die()
    95  	}
    96  	return modFile
    97  }
    98  
    99  func BinDir() string {
   100  	Init()
   101  	return filepath.Join(gopath, "bin")
   102  }
   103  
   104  // Init determines whether module mode is enabled, locates the root of the
   105  // current module (if any), sets environment variables for Git subprocesses, and
   106  // configures the cfg, codehost, load, modfetch, and search packages for use
   107  // with modules.
   108  func Init() {
   109  	if initialized {
   110  		return
   111  	}
   112  	initialized = true
   113  
   114  	// Keep in sync with WillBeEnabled. We perform extra validation here, and
   115  	// there are lots of diagnostics and side effects, so we can't use
   116  	// WillBeEnabled directly.
   117  	var mustUseModules bool
   118  	env := cfg.Getenv("GO111MODULE")
   119  	switch env {
   120  	default:
   121  		base.Fatalf("go: unknown environment setting GO111MODULE=%s", env)
   122  	case "auto":
   123  		mustUseModules = ForceUseModules
   124  	case "on", "":
   125  		mustUseModules = true
   126  	case "off":
   127  		if ForceUseModules {
   128  			base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
   129  		}
   130  		mustUseModules = false
   131  		return
   132  	}
   133  
   134  	if err := fsys.Init(base.Cwd); err != nil {
   135  		base.Fatalf("go: %v", err)
   136  	}
   137  
   138  	// Disable any prompting for passwords by Git.
   139  	// Only has an effect for 2.3.0 or later, but avoiding
   140  	// the prompt in earlier versions is just too hard.
   141  	// If user has explicitly set GIT_TERMINAL_PROMPT=1, keep
   142  	// prompting.
   143  	// See golang.org/issue/9341 and golang.org/issue/12706.
   144  	if os.Getenv("GIT_TERMINAL_PROMPT") == "" {
   145  		os.Setenv("GIT_TERMINAL_PROMPT", "0")
   146  	}
   147  
   148  	// Disable any ssh connection pooling by Git.
   149  	// If a Git subprocess forks a child into the background to cache a new connection,
   150  	// that child keeps stdout/stderr open. After the Git subprocess exits,
   151  	// os /exec expects to be able to read from the stdout/stderr pipe
   152  	// until EOF to get all the data that the Git subprocess wrote before exiting.
   153  	// The EOF doesn't come until the child exits too, because the child
   154  	// is holding the write end of the pipe.
   155  	// This is unfortunate, but it has come up at least twice
   156  	// (see golang.org/issue/13453 and golang.org/issue/16104)
   157  	// and confuses users when it does.
   158  	// If the user has explicitly set GIT_SSH or GIT_SSH_COMMAND,
   159  	// assume they know what they are doing and don't step on it.
   160  	// But default to turning off ControlMaster.
   161  	if os.Getenv("GIT_SSH") == "" && os.Getenv("GIT_SSH_COMMAND") == "" {
   162  		os.Setenv("GIT_SSH_COMMAND", "ssh -o ControlMaster=no")
   163  	}
   164  
   165  	if modRoot != "" {
   166  		// modRoot set before Init was called ("go mod init" does this).
   167  		// No need to search for go.mod.
   168  	} else if RootMode == NoRoot {
   169  		if cfg.ModFile != "" && !base.InGOFLAGS("-modfile") {
   170  			base.Fatalf("go: -modfile cannot be used with commands that ignore the current module")
   171  		}
   172  		modRoot = ""
   173  	} else {
   174  		modRoot = findModuleRoot(base.Cwd)
   175  		if modRoot == "" {
   176  			if cfg.ModFile != "" {
   177  				base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.")
   178  			}
   179  			if RootMode == NeedRoot {
   180  				base.Fatalf("go: %v", ErrNoModRoot)
   181  			}
   182  			if !mustUseModules {
   183  				// GO111MODULE is 'auto', and we can't find a module root.
   184  				// Stay in GOPATH mode.
   185  				return
   186  			}
   187  		} else if search.InDir(modRoot, os.TempDir()) == "." {
   188  			// If you create /tmp/go.mod for experimenting,
   189  			// then any tests that create work directories under /tmp
   190  			// will find it and get modules when they're not expecting them.
   191  			// It's a bit of a peculiar thing to disallow but quite mysterious
   192  			// when it happens. See golang.org/issue/26708.
   193  			modRoot = ""
   194  			fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in system temp root %v\n", os.TempDir())
   195  			if !mustUseModules {
   196  				return
   197  			}
   198  		}
   199  	}
   200  	if cfg.ModFile != "" && !strings.HasSuffix(cfg.ModFile, ".mod") {
   201  		base.Fatalf("go: -modfile=%s: file does not have .mod extension", cfg.ModFile)
   202  	}
   203  
   204  	// We're in module mode. Set any global variables that need to be set.
   205  	cfg.ModulesEnabled = true
   206  	setDefaultBuildMod()
   207  	list := filepath.SplitList(cfg.BuildContext.GOPATH)
   208  	if len(list) == 0 || list[0] == "" {
   209  		base.Fatalf("missing $GOPATH")
   210  	}
   211  	gopath = list[0]
   212  	if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil {
   213  		base.Fatalf("$GOPATH/go.mod exists but should not")
   214  	}
   215  
   216  	if modRoot == "" {
   217  		// We're in module mode, but not inside a module.
   218  		//
   219  		// Commands like 'go build', 'go run', 'go list' have no go.mod file to
   220  		// read or write. They would need to find and download the latest versions
   221  		// of a potentially large number of modules with no way to save version
   222  		// information. We can succeed slowly (but not reproducibly), but that's
   223  		// not usually a good experience.
   224  		//
   225  		// Instead, we forbid resolving import paths to modules other than std and
   226  		// cmd. Users may still build packages specified with .go files on the
   227  		// command line, but they'll see an error if those files import anything
   228  		// outside std.
   229  		//
   230  		// This can be overridden by calling AllowMissingModuleImports.
   231  		// For example, 'go get' does this, since it is expected to resolve paths.
   232  		//
   233  		// See golang.org/issue/32027.
   234  	} else {
   235  		modfetch.GoSumFile = strings.TrimSuffix(ModFilePath(), ".mod") + ".sum"
   236  		search.SetModRoot(modRoot)
   237  	}
   238  }
   239  
   240  // WillBeEnabled checks whether modules should be enabled but does not
   241  // initialize modules by installing hooks. If Init has already been called,
   242  // WillBeEnabled returns the same result as Enabled.
   243  //
   244  // This function is needed to break a cycle. The main package needs to know
   245  // whether modules are enabled in order to install the module or GOPATH version
   246  // of 'go get', but Init reads the -modfile flag in 'go get', so it shouldn't
   247  // be called until the command is installed and flags are parsed. Instead of
   248  // calling Init and Enabled, the main package can call this function.
   249  func WillBeEnabled() bool {
   250  	if modRoot != "" || cfg.ModulesEnabled {
   251  		// Already enabled.
   252  		return true
   253  	}
   254  	if initialized {
   255  		// Initialized, not enabled.
   256  		return false
   257  	}
   258  
   259  	// Keep in sync with Init. Init does extra validation and prints warnings or
   260  	// exits, so it can't call this function directly.
   261  	env := cfg.Getenv("GO111MODULE")
   262  	switch env {
   263  	case "on", "":
   264  		return true
   265  	case "auto":
   266  		break
   267  	default:
   268  		return false
   269  	}
   270  
   271  	if modRoot := findModuleRoot(base.Cwd); modRoot == "" {
   272  		// GO111MODULE is 'auto', and we can't find a module root.
   273  		// Stay in GOPATH mode.
   274  		return false
   275  	} else if search.InDir(modRoot, os.TempDir()) == "." {
   276  		// If you create /tmp/go.mod for experimenting,
   277  		// then any tests that create work directories under /tmp
   278  		// will find it and get modules when they're not expecting them.
   279  		// It's a bit of a peculiar thing to disallow but quite mysterious
   280  		// when it happens. See golang.org/issue/26708.
   281  		return false
   282  	}
   283  	return true
   284  }
   285  
   286  // Enabled reports whether modules are (or must be) enabled.
   287  // If modules are enabled but there is no main module, Enabled returns true
   288  // and then the first use of module information will call die
   289  // (usually through MustModRoot).
   290  func Enabled() bool {
   291  	Init()
   292  	return modRoot != "" || cfg.ModulesEnabled
   293  }
   294  
   295  // ModRoot returns the root of the main module.
   296  // It calls base.Fatalf if there is no main module.
   297  func ModRoot() string {
   298  	if !HasModRoot() {
   299  		die()
   300  	}
   301  	return modRoot
   302  }
   303  
   304  // HasModRoot reports whether a main module is present.
   305  // HasModRoot may return false even if Enabled returns true: for example, 'get'
   306  // does not require a main module.
   307  func HasModRoot() bool {
   308  	Init()
   309  	return modRoot != ""
   310  }
   311  
   312  // ModFilePath returns the effective path of the go.mod file. Normally, this
   313  // "go.mod" in the directory returned by ModRoot, but the -modfile flag may
   314  // change its location. ModFilePath calls base.Fatalf if there is no main
   315  // module, even if -modfile is set.
   316  func ModFilePath() string {
   317  	if !HasModRoot() {
   318  		die()
   319  	}
   320  	if cfg.ModFile != "" {
   321  		return cfg.ModFile
   322  	}
   323  	return filepath.Join(modRoot, "go.mod")
   324  }
   325  
   326  func die() {
   327  	if cfg.Getenv("GO111MODULE") == "off" {
   328  		base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
   329  	}
   330  	if dir, name := findAltConfig(base.Cwd); dir != "" {
   331  		rel, err := filepath.Rel(base.Cwd, dir)
   332  		if err != nil {
   333  			rel = dir
   334  		}
   335  		cdCmd := ""
   336  		if rel != "." {
   337  			cdCmd = fmt.Sprintf("cd %s && ", rel)
   338  		}
   339  		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)
   340  	}
   341  	base.Fatalf("go: %v", ErrNoModRoot)
   342  }
   343  
   344  var ErrNoModRoot = errors.New("go.mod file not found in current directory or any parent directory; see 'go help modules'")
   345  
   346  // LoadModFile sets Target and, if there is a main module, parses the initial
   347  // build list from its go.mod file.
   348  //
   349  // LoadModFile may make changes in memory, like adding a go directive and
   350  // ensuring requirements are consistent. WriteGoMod should be called later to
   351  // write changes out to disk or report errors in readonly mode.
   352  //
   353  // As a side-effect, LoadModFile may change cfg.BuildMod to "vendor" if
   354  // -mod wasn't set explicitly and automatic vendoring should be enabled.
   355  func LoadModFile(ctx context.Context) {
   356  	if len(buildList) > 0 {
   357  		return
   358  	}
   359  
   360  	Init()
   361  	if modRoot == "" {
   362  		Target = module.Version{Path: "command-line-arguments"}
   363  		targetPrefix = "command-line-arguments"
   364  		buildList = []module.Version{Target}
   365  		return
   366  	}
   367  
   368  	gomod := ModFilePath()
   369  	data, err := lockedfile.Read(gomod)
   370  	if err != nil {
   371  		base.Fatalf("go: %v", err)
   372  	}
   373  
   374  	var fixed bool
   375  	f, err := modfile.Parse(gomod, data, fixVersion(ctx, &fixed))
   376  	if err != nil {
   377  		// Errors returned by modfile.Parse begin with file:line.
   378  		base.Fatalf("go: errors parsing go.mod:\n%s\n", err)
   379  	}
   380  	modFile = f
   381  	index = indexModFile(data, f, fixed)
   382  
   383  	if f.Module == nil {
   384  		// No module declaration. Must add module path.
   385  		base.Fatalf("go: no module declaration in go.mod. To specify the module path:\n\tgo mod edit -module=example.com/mod")
   386  	}
   387  
   388  	if err := checkModulePathLax(f.Module.Mod.Path); err != nil {
   389  		base.Fatalf("go: %v", err)
   390  	}
   391  
   392  	setDefaultBuildMod() // possibly enable automatic vendoring
   393  	modFileToBuildList()
   394  	if cfg.BuildMod == "vendor" {
   395  		readVendorList()
   396  		checkVendorConsistency()
   397  	}
   398  }
   399  
   400  // CreateModFile initializes a new module by creating a go.mod file.
   401  //
   402  // If modPath is empty, CreateModFile will attempt to infer the path from the
   403  // directory location within GOPATH.
   404  //
   405  // If a vendoring configuration file is present, CreateModFile will attempt to
   406  // translate it to go.mod directives. The resulting build list may not be
   407  // exactly the same as in the legacy configuration (for example, we can't get
   408  // packages at multiple versions from the same module).
   409  func CreateModFile(ctx context.Context, modPath string) {
   410  	modRoot = base.Cwd
   411  	Init()
   412  	modFilePath := ModFilePath()
   413  	if _, err := fsys.Stat(modFilePath); err == nil {
   414  		base.Fatalf("go: %s already exists", modFilePath)
   415  	}
   416  
   417  	if modPath == "" {
   418  		var err error
   419  		modPath, err = findModulePath(modRoot)
   420  		if err != nil {
   421  			base.Fatalf("go: %v", err)
   422  		}
   423  	} else if err := checkModulePathLax(modPath); err != nil {
   424  		base.Fatalf("go: %v", err)
   425  	}
   426  
   427  	fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", modPath)
   428  	modFile = new(modfile.File)
   429  	modFile.AddModuleStmt(modPath)
   430  	addGoStmt() // Add the go directive before converted module requirements.
   431  
   432  	convertedFrom, err := convertLegacyConfig(modPath)
   433  	if convertedFrom != "" {
   434  		fmt.Fprintf(os.Stderr, "go: copying requirements from %s\n", base.ShortPath(convertedFrom))
   435  	}
   436  	if err != nil {
   437  		base.Fatalf("go: %v", err)
   438  	}
   439  
   440  	modFileToBuildList()
   441  	WriteGoMod()
   442  
   443  	// Suggest running 'go mod tidy' unless the project is empty. Even if we
   444  	// imported all the correct requirements above, we're probably missing
   445  	// some sums, so the next build command in -mod=readonly will likely fail.
   446  	//
   447  	// We look for non-hidden .go files or subdirectories to determine whether
   448  	// this is an existing project. Walking the tree for packages would be more
   449  	// accurate, but could take much longer.
   450  	empty := true
   451  	files, _ := os.ReadDir(modRoot)
   452  	for _, f := range files {
   453  		name := f.Name()
   454  		if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
   455  			continue
   456  		}
   457  		if strings.HasSuffix(name, ".go") || f.IsDir() {
   458  			empty = false
   459  			break
   460  		}
   461  	}
   462  	if !empty {
   463  		fmt.Fprintf(os.Stderr, "go: to add module requirements and sums:\n\tgo mod tidy\n")
   464  	}
   465  }
   466  
   467  // checkModulePathLax checks that the path meets some minimum requirements
   468  // to avoid confusing users or the module cache. The requirements are weaker
   469  // than those of module.CheckPath to allow room for weakening module path
   470  // requirements in the future, but strong enough to help users avoid significant
   471  // problems.
   472  func checkModulePathLax(p string) error {
   473  	// TODO(matloob): Replace calls of this function in this CL with calls
   474  	// to module.CheckImportPath once it's been laxened, if it becomes laxened.
   475  	// See golang.org/issue/29101 for a discussion about whether to make CheckImportPath
   476  	// more lax or more strict.
   477  
   478  	errorf := func(format string, args ...interface{}) error {
   479  		return fmt.Errorf("invalid module path %q: %s", p, fmt.Sprintf(format, args...))
   480  	}
   481  
   482  	// Disallow shell characters " ' * < > ? ` | to avoid triggering bugs
   483  	// with file systems and subcommands. Disallow file path separators : and \
   484  	// because path separators other than / will confuse the module cache.
   485  	// See fileNameOK in golang.org/x/mod/module/module.go.
   486  	shellChars := "`" + `\"'*<>?|`
   487  	fsChars := `\:`
   488  	if i := strings.IndexAny(p, shellChars); i >= 0 {
   489  		return errorf("contains disallowed shell character %q", p[i])
   490  	}
   491  	if i := strings.IndexAny(p, fsChars); i >= 0 {
   492  		return errorf("contains disallowed path separator character %q", p[i])
   493  	}
   494  
   495  	// Ensure path.IsAbs and build.IsLocalImport are false, and that the path is
   496  	// invariant under path.Clean, also to avoid confusing the module cache.
   497  	if path.IsAbs(p) {
   498  		return errorf("is an absolute path")
   499  	}
   500  	if build.IsLocalImport(p) {
   501  		return errorf("is a local import path")
   502  	}
   503  	if path.Clean(p) != p {
   504  		return errorf("is not clean")
   505  	}
   506  
   507  	return nil
   508  }
   509  
   510  // fixVersion returns a modfile.VersionFixer implemented using the Query function.
   511  //
   512  // It resolves commit hashes and branch names to versions,
   513  // canonicalizes versions that appeared in early vgo drafts,
   514  // and does nothing for versions that already appear to be canonical.
   515  //
   516  // The VersionFixer sets 'fixed' if it ever returns a non-canonical version.
   517  func fixVersion(ctx context.Context, fixed *bool) modfile.VersionFixer {
   518  	return func(path, vers string) (resolved string, err error) {
   519  		defer func() {
   520  			if err == nil && resolved != vers {
   521  				*fixed = true
   522  			}
   523  		}()
   524  
   525  		// Special case: remove the old -gopkgin- hack.
   526  		if strings.HasPrefix(path, "gopkg.in/") && strings.Contains(vers, "-gopkgin-") {
   527  			vers = vers[strings.Index(vers, "-gopkgin-")+len("-gopkgin-"):]
   528  		}
   529  
   530  		// fixVersion is called speculatively on every
   531  		// module, version pair from every go.mod file.
   532  		// Avoid the query if it looks OK.
   533  		_, pathMajor, ok := module.SplitPathVersion(path)
   534  		if !ok {
   535  			return "", &module.ModuleError{
   536  				Path: path,
   537  				Err: &module.InvalidVersionError{
   538  					Version: vers,
   539  					Err:     fmt.Errorf("malformed module path %q", path),
   540  				},
   541  			}
   542  		}
   543  		if vers != "" && module.CanonicalVersion(vers) == vers {
   544  			if err := module.CheckPathMajor(vers, pathMajor); err != nil {
   545  				return "", module.VersionError(module.Version{Path: path, Version: vers}, err)
   546  			}
   547  			return vers, nil
   548  		}
   549  
   550  		info, err := Query(ctx, path, vers, "", nil)
   551  		if err != nil {
   552  			return "", err
   553  		}
   554  		return info.Version, nil
   555  	}
   556  }
   557  
   558  // AllowMissingModuleImports allows import paths to be resolved to modules
   559  // when there is no module root. Normally, this is forbidden because it's slow
   560  // and there's no way to make the result reproducible, but some commands
   561  // like 'go get' are expected to do this.
   562  func AllowMissingModuleImports() {
   563  	allowMissingModuleImports = true
   564  }
   565  
   566  // modFileToBuildList initializes buildList from the modFile.
   567  func modFileToBuildList() {
   568  	Target = modFile.Module.Mod
   569  	targetPrefix = Target.Path
   570  	if rel := search.InDir(base.Cwd, cfg.GOROOTsrc); rel != "" {
   571  		targetInGorootSrc = true
   572  		if Target.Path == "std" {
   573  			targetPrefix = ""
   574  		}
   575  	}
   576  
   577  	list := []module.Version{Target}
   578  	for _, r := range modFile.Require {
   579  		if index != nil && index.exclude[r.Mod] {
   580  			if cfg.BuildMod == "mod" {
   581  				fmt.Fprintf(os.Stderr, "go: dropping requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
   582  			} else {
   583  				fmt.Fprintf(os.Stderr, "go: ignoring requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
   584  			}
   585  		} else {
   586  			list = append(list, r.Mod)
   587  		}
   588  	}
   589  	buildList = list
   590  }
   591  
   592  // setDefaultBuildMod sets a default value for cfg.BuildMod if the -mod flag
   593  // wasn't provided. setDefaultBuildMod may be called multiple times.
   594  func setDefaultBuildMod() {
   595  	if cfg.BuildModExplicit {
   596  		// Don't override an explicit '-mod=' argument.
   597  		return
   598  	}
   599  
   600  	if cfg.CmdName == "get" || strings.HasPrefix(cfg.CmdName, "mod ") {
   601  		// 'get' and 'go mod' commands may update go.mod automatically.
   602  		// TODO(jayconrod): should this narrower? Should 'go mod download' or
   603  		// 'go mod graph' update go.mod by default?
   604  		cfg.BuildMod = "mod"
   605  		return
   606  	}
   607  	if modRoot == "" {
   608  		cfg.BuildMod = "readonly"
   609  		return
   610  	}
   611  
   612  	if fi, err := fsys.Stat(filepath.Join(modRoot, "vendor")); err == nil && fi.IsDir() {
   613  		modGo := "unspecified"
   614  		if index != nil && index.goVersionV != "" {
   615  			if semver.Compare(index.goVersionV, "v1.14") >= 0 {
   616  				// The Go version is at least 1.14, and a vendor directory exists.
   617  				// Set -mod=vendor by default.
   618  				cfg.BuildMod = "vendor"
   619  				cfg.BuildModReason = "Go version in go.mod is at least 1.14 and vendor directory exists."
   620  				return
   621  			} else {
   622  				modGo = index.goVersionV[1:]
   623  			}
   624  		}
   625  
   626  		// Since a vendor directory exists, we should record why we didn't use it.
   627  		// This message won't normally be shown, but it may appear with import errors.
   628  		cfg.BuildModReason = fmt.Sprintf("Go version in go.mod is %s, so vendor directory was not used.", modGo)
   629  	}
   630  
   631  	cfg.BuildMod = "readonly"
   632  }
   633  
   634  // convertLegacyConfig imports module requirements from a legacy vendoring
   635  // configuration file, if one is present.
   636  func convertLegacyConfig(modPath string) (from string, err error) {
   637  	for _, name := range altConfigs {
   638  		cfg := filepath.Join(modRoot, name)
   639  		data, err := os.ReadFile(cfg)
   640  		if err == nil {
   641  			convert := modconv.Converters[name]
   642  			if convert == nil {
   643  				return "", nil
   644  			}
   645  			cfg = filepath.ToSlash(cfg)
   646  			err := modconv.ConvertLegacyConfig(modFile, cfg, data)
   647  			return name, err
   648  		}
   649  	}
   650  	return "", nil
   651  }
   652  
   653  // addGoStmt adds a go directive to the go.mod file if it does not already include one.
   654  // The 'go' version added, if any, is the latest version supported by this toolchain.
   655  func addGoStmt() {
   656  	if modFile.Go != nil && modFile.Go.Version != "" {
   657  		return
   658  	}
   659  	tags := build.Default.ReleaseTags
   660  	version := tags[len(tags)-1]
   661  	if !strings.HasPrefix(version, "go") || !modfile.GoVersionRE.MatchString(version[2:]) {
   662  		base.Fatalf("go: unrecognized default version %q", version)
   663  	}
   664  	if err := modFile.AddGoStmt(version[2:]); err != nil {
   665  		base.Fatalf("go: internal error: %v", err)
   666  	}
   667  }
   668  
   669  var altConfigs = []string{
   670  	"Gopkg.lock",
   671  
   672  	"GLOCKFILE",
   673  	"Godeps/Godeps.json",
   674  	"dependencies.tsv",
   675  	"glide.lock",
   676  	"vendor.conf",
   677  	"vendor.yml",
   678  	"vendor/manifest",
   679  	"vendor/vendor.json",
   680  
   681  	".git/config",
   682  }
   683  
   684  func findModuleRoot(dir string) (root string) {
   685  	if dir == "" {
   686  		panic("dir not set")
   687  	}
   688  	dir = filepath.Clean(dir)
   689  
   690  	// Look for enclosing go.mod.
   691  	for {
   692  		if fi, err := fsys.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {
   693  			return dir
   694  		}
   695  		d := filepath.Dir(dir)
   696  		if d == dir {
   697  			break
   698  		}
   699  		dir = d
   700  	}
   701  	return ""
   702  }
   703  
   704  func findAltConfig(dir string) (root, name string) {
   705  	if dir == "" {
   706  		panic("dir not set")
   707  	}
   708  	dir = filepath.Clean(dir)
   709  	if rel := search.InDir(dir, cfg.BuildContext.GOROOT); rel != "" {
   710  		// Don't suggest creating a module from $GOROOT/.git/config
   711  		// or a config file found in any parent of $GOROOT (see #34191).
   712  		return "", ""
   713  	}
   714  	for {
   715  		for _, name := range altConfigs {
   716  			if fi, err := fsys.Stat(filepath.Join(dir, name)); err == nil && !fi.IsDir() {
   717  				return dir, name
   718  			}
   719  		}
   720  		d := filepath.Dir(dir)
   721  		if d == dir {
   722  			break
   723  		}
   724  		dir = d
   725  	}
   726  	return "", ""
   727  }
   728  
   729  func findModulePath(dir string) (string, error) {
   730  	// TODO(bcmills): once we have located a plausible module path, we should
   731  	// query version control (if available) to verify that it matches the major
   732  	// version of the most recent tag.
   733  	// See https://golang.org/issue/29433, https://golang.org/issue/27009, and
   734  	// https://golang.org/issue/31549.
   735  
   736  	// Cast about for import comments,
   737  	// first in top-level directory, then in subdirectories.
   738  	list, _ := os.ReadDir(dir)
   739  	for _, info := range list {
   740  		if info.Type().IsRegular() && strings.HasSuffix(info.Name(), ".go") {
   741  			if com := findImportComment(filepath.Join(dir, info.Name())); com != "" {
   742  				return com, nil
   743  			}
   744  		}
   745  	}
   746  	for _, info1 := range list {
   747  		if info1.IsDir() {
   748  			files, _ := os.ReadDir(filepath.Join(dir, info1.Name()))
   749  			for _, info2 := range files {
   750  				if info2.Type().IsRegular() && strings.HasSuffix(info2.Name(), ".go") {
   751  					if com := findImportComment(filepath.Join(dir, info1.Name(), info2.Name())); com != "" {
   752  						return path.Dir(com), nil
   753  					}
   754  				}
   755  			}
   756  		}
   757  	}
   758  
   759  	// Look for Godeps.json declaring import path.
   760  	data, _ := os.ReadFile(filepath.Join(dir, "Godeps/Godeps.json"))
   761  	var cfg1 struct{ ImportPath string }
   762  	json.Unmarshal(data, &cfg1)
   763  	if cfg1.ImportPath != "" {
   764  		return cfg1.ImportPath, nil
   765  	}
   766  
   767  	// Look for vendor.json declaring import path.
   768  	data, _ = os.ReadFile(filepath.Join(dir, "vendor/vendor.json"))
   769  	var cfg2 struct{ RootPath string }
   770  	json.Unmarshal(data, &cfg2)
   771  	if cfg2.RootPath != "" {
   772  		return cfg2.RootPath, nil
   773  	}
   774  
   775  	// Look for path in GOPATH.
   776  	var badPathErr error
   777  	for _, gpdir := range filepath.SplitList(cfg.BuildContext.GOPATH) {
   778  		if gpdir == "" {
   779  			continue
   780  		}
   781  		if rel := search.InDir(dir, filepath.Join(gpdir, "src")); rel != "" && rel != "." {
   782  			path := filepath.ToSlash(rel)
   783  			// TODO(matloob): replace this with module.CheckImportPath
   784  			// once it's been laxened.
   785  			// Only checkModulePathLax here. There are some unpublishable
   786  			// module names that are compatible with checkModulePathLax
   787  			// but they already work in GOPATH so don't break users
   788  			// trying to do a build with modules. gorelease will alert users
   789  			// publishing their modules to fix their paths.
   790  			if err := checkModulePathLax(path); err != nil {
   791  				badPathErr = err
   792  				break
   793  			}
   794  			return path, nil
   795  		}
   796  	}
   797  
   798  	reason := "outside GOPATH, module path must be specified"
   799  	if badPathErr != nil {
   800  		// return a different error message if the module was in GOPATH, but
   801  		// the module path determined above would be an invalid path.
   802  		reason = fmt.Sprintf("bad module path inferred from directory in GOPATH: %v", badPathErr)
   803  	}
   804  	msg := `cannot determine module path for source directory %s (%s)
   805  
   806  Example usage:
   807  	'go mod init example.com/m' to initialize a v0 or v1 module
   808  	'go mod init example.com/m/v2' to initialize a v2 module
   809  
   810  Run 'go help mod init' for more information.
   811  `
   812  	return "", fmt.Errorf(msg, dir, reason)
   813  }
   814  
   815  var (
   816  	importCommentRE = lazyregexp.New(`(?m)^package[ \t]+[^ \t\r\n/]+[ \t]+//[ \t]+import[ \t]+(\"[^"]+\")[ \t]*\r?\n`)
   817  )
   818  
   819  func findImportComment(file string) string {
   820  	data, err := os.ReadFile(file)
   821  	if err != nil {
   822  		return ""
   823  	}
   824  	m := importCommentRE.FindSubmatch(data)
   825  	if m == nil {
   826  		return ""
   827  	}
   828  	path, err := strconv.Unquote(string(m[1]))
   829  	if err != nil {
   830  		return ""
   831  	}
   832  	return path
   833  }
   834  
   835  var allowWriteGoMod = true
   836  
   837  // DisallowWriteGoMod causes future calls to WriteGoMod to do nothing at all.
   838  func DisallowWriteGoMod() {
   839  	allowWriteGoMod = false
   840  }
   841  
   842  // AllowWriteGoMod undoes the effect of DisallowWriteGoMod:
   843  // future calls to WriteGoMod will update go.mod if needed.
   844  // Note that any past calls have been discarded, so typically
   845  // a call to AlowWriteGoMod should be followed by a call to WriteGoMod.
   846  func AllowWriteGoMod() {
   847  	allowWriteGoMod = true
   848  }
   849  
   850  // MinReqs returns a Reqs with minimal additional dependencies of Target,
   851  // as will be written to go.mod.
   852  func MinReqs() mvs.Reqs {
   853  	retain := append([]string{}, additionalExplicitRequirements...)
   854  	for _, m := range buildList[1:] {
   855  		_, explicit := index.require[m]
   856  		if explicit || loaded.direct[m.Path] {
   857  			retain = append(retain, m.Path)
   858  		}
   859  	}
   860  	sort.Strings(retain)
   861  	str.Uniq(&retain)
   862  	min, err := mvs.Req(Target, retain, &mvsReqs{buildList: buildList})
   863  	if err != nil {
   864  		base.Fatalf("go: %v", err)
   865  	}
   866  	return &mvsReqs{buildList: append([]module.Version{Target}, min...)}
   867  }
   868  
   869  // WriteGoMod writes the current build list back to go.mod.
   870  func WriteGoMod() {
   871  	// If we're using -mod=vendor we basically ignored
   872  	// go.mod, so definitely don't try to write back our
   873  	// incomplete view of the world.
   874  	if !allowWriteGoMod || cfg.BuildMod == "vendor" {
   875  		return
   876  	}
   877  
   878  	// If we aren't in a module, we don't have anywhere to write a go.mod file.
   879  	if modRoot == "" {
   880  		return
   881  	}
   882  
   883  	if cfg.BuildMod != "readonly" {
   884  		addGoStmt()
   885  	}
   886  
   887  	if loaded != nil {
   888  		reqs := MinReqs()
   889  		min, err := reqs.Required(Target)
   890  		if err != nil {
   891  			base.Fatalf("go: %v", err)
   892  		}
   893  		var list []*modfile.Require
   894  		for _, m := range min {
   895  			list = append(list, &modfile.Require{
   896  				Mod:      m,
   897  				Indirect: !loaded.direct[m.Path],
   898  			})
   899  		}
   900  		modFile.SetRequire(list)
   901  	}
   902  	modFile.Cleanup()
   903  
   904  	dirty := index.modFileIsDirty(modFile)
   905  	if dirty && cfg.BuildMod == "readonly" {
   906  		// If we're about to fail due to -mod=readonly,
   907  		// prefer to report a dirty go.mod over a dirty go.sum
   908  		if cfg.BuildModExplicit {
   909  			base.Fatalf("go: updates to go.mod needed, disabled by -mod=readonly")
   910  		} else if cfg.BuildModReason != "" {
   911  			base.Fatalf("go: updates to go.mod needed, disabled by -mod=readonly\n\t(%s)", cfg.BuildModReason)
   912  		} else {
   913  			base.Fatalf("go: updates to go.mod needed; to update it:\n\tgo mod tidy")
   914  		}
   915  	}
   916  
   917  	if !dirty && cfg.CmdName != "mod tidy" {
   918  		// The go.mod file has the same semantic content that it had before
   919  		// (but not necessarily the same exact bytes).
   920  		// Don't write go.mod, but write go.sum in case we added or trimmed sums.
   921  		// 'go mod init' shouldn't write go.sum, since it will be incomplete.
   922  		if cfg.CmdName != "mod init" {
   923  			modfetch.WriteGoSum(keepSums(true))
   924  		}
   925  		return
   926  	}
   927  
   928  	new, err := modFile.Format()
   929  	if err != nil {
   930  		base.Fatalf("go: %v", err)
   931  	}
   932  	defer func() {
   933  		// At this point we have determined to make the go.mod file on disk equal to new.
   934  		index = indexModFile(new, modFile, false)
   935  
   936  		// Update go.sum after releasing the side lock and refreshing the index.
   937  		// 'go mod init' shouldn't write go.sum, since it will be incomplete.
   938  		if cfg.CmdName != "mod init" {
   939  			modfetch.WriteGoSum(keepSums(true))
   940  		}
   941  	}()
   942  
   943  	// Make a best-effort attempt to acquire the side lock, only to exclude
   944  	// previous versions of the 'go' command from making simultaneous edits.
   945  	if unlock, err := modfetch.SideLock(); err == nil {
   946  		defer unlock()
   947  	}
   948  
   949  	errNoChange := errors.New("no update needed")
   950  
   951  	err = lockedfile.Transform(ModFilePath(), func(old []byte) ([]byte, error) {
   952  		if bytes.Equal(old, new) {
   953  			// The go.mod file is already equal to new, possibly as the result of some
   954  			// other process.
   955  			return nil, errNoChange
   956  		}
   957  
   958  		if index != nil && !bytes.Equal(old, index.data) {
   959  			// The contents of the go.mod file have changed. In theory we could add all
   960  			// of the new modules to the build list, recompute, and check whether any
   961  			// module in *our* build list got bumped to a different version, but that's
   962  			// a lot of work for marginal benefit. Instead, fail the command: if users
   963  			// want to run concurrent commands, they need to start with a complete,
   964  			// consistent module definition.
   965  			return nil, fmt.Errorf("existing contents have changed since last read")
   966  		}
   967  
   968  		return new, nil
   969  	})
   970  
   971  	if err != nil && err != errNoChange {
   972  		base.Fatalf("go: updating go.mod: %v", err)
   973  	}
   974  }
   975  
   976  // keepSums returns a set of module sums to preserve in go.sum. The set
   977  // includes entries for all modules used to load packages (according to
   978  // the last load function such as LoadPackages or ImportFromFiles).
   979  // It also contains entries for go.mod files needed for MVS (the version
   980  // of these entries ends with "/go.mod").
   981  //
   982  // If keepBuildListZips is true, the set also includes sums for zip files for
   983  // all modules in the build list with replacements applied. 'go get' and
   984  // 'go mod download' may add sums to this set when adding a requirement on a
   985  // module without a root package or when downloading a direct or indirect
   986  // dependency.
   987  func keepSums(keepBuildListZips bool) map[module.Version]bool {
   988  	// Re-derive the build list using the current list of direct requirements.
   989  	// Keep the sum for the go.mod of each visited module version (or its
   990  	// replacement).
   991  	modkey := func(m module.Version) module.Version {
   992  		return module.Version{Path: m.Path, Version: m.Version + "/go.mod"}
   993  	}
   994  	keep := make(map[module.Version]bool)
   995  	var mu sync.Mutex
   996  	reqs := &keepSumReqs{
   997  		Reqs: &mvsReqs{buildList: buildList},
   998  		visit: func(m module.Version) {
   999  			// If we build using a replacement module, keep the sum for the replacement,
  1000  			// since that's the code we'll actually use during a build.
  1001  			mu.Lock()
  1002  			r := Replacement(m)
  1003  			if r.Path == "" {
  1004  				keep[modkey(m)] = true
  1005  			} else {
  1006  				keep[modkey(r)] = true
  1007  			}
  1008  			mu.Unlock()
  1009  		},
  1010  	}
  1011  	buildList, err := mvs.BuildList(Target, reqs)
  1012  	if err != nil {
  1013  		panic(fmt.Sprintf("unexpected error reloading build list: %v", err))
  1014  	}
  1015  
  1016  	actualMods := make(map[string]module.Version)
  1017  	for _, m := range buildList[1:] {
  1018  		if r := Replacement(m); r.Path != "" {
  1019  			actualMods[m.Path] = r
  1020  		} else {
  1021  			actualMods[m.Path] = m
  1022  		}
  1023  	}
  1024  
  1025  	// Add entries for modules in the build list with paths that are prefixes of
  1026  	// paths of loaded packages. We need to retain sums for modules needed to
  1027  	// report ambiguous import errors. We use our re-derived build list,
  1028  	// since the global build list may have been tidied.
  1029  	if loaded != nil {
  1030  		for _, pkg := range loaded.pkgs {
  1031  			if pkg.testOf != nil || pkg.inStd || module.CheckImportPath(pkg.path) != nil {
  1032  				continue
  1033  			}
  1034  			for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) {
  1035  				if m, ok := actualMods[prefix]; ok {
  1036  					keep[m] = true
  1037  				}
  1038  			}
  1039  		}
  1040  	}
  1041  
  1042  	// Add entries for the zip of each module in the build list.
  1043  	// We might not need all of these (tidy does not add them), but they may be
  1044  	// added by a specific 'go get' or 'go mod download' command to resolve
  1045  	// missing import sum errors.
  1046  	if keepBuildListZips {
  1047  		for _, m := range actualMods {
  1048  			keep[m] = true
  1049  		}
  1050  	}
  1051  
  1052  	return keep
  1053  }
  1054  
  1055  // keepSumReqs embeds another Reqs implementation. The Required method
  1056  // calls visit for each version in the module graph.
  1057  type keepSumReqs struct {
  1058  	mvs.Reqs
  1059  	visit func(module.Version)
  1060  }
  1061  
  1062  func (r *keepSumReqs) Required(m module.Version) ([]module.Version, error) {
  1063  	r.visit(m)
  1064  	return r.Reqs.Required(m)
  1065  }
  1066  
  1067  func TrimGoSum() {
  1068  	// Don't retain sums for the zip file of every module in the build list.
  1069  	// We may not need them all to build the main module's packages.
  1070  	keepBuildListZips := false
  1071  	modfetch.TrimGoSum(keepSums(keepBuildListZips))
  1072  }
  1073  

View as plain text