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: cannot find main module; see 'go help modules'")
   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: cannot find main module; see 'go help modules'")
   342  }
   343  
   344  // LoadModFile sets Target and, if there is a main module, parses the initial
   345  // build list from its go.mod file.
   346  //
   347  // LoadModFile may make changes in memory, like adding a go directive and
   348  // ensuring requirements are consistent. WriteGoMod should be called later to
   349  // write changes out to disk or report errors in readonly mode.
   350  //
   351  // As a side-effect, LoadModFile may change cfg.BuildMod to "vendor" if
   352  // -mod wasn't set explicitly and automatic vendoring should be enabled.
   353  func LoadModFile(ctx context.Context) {
   354  	if len(buildList) > 0 {
   355  		return
   356  	}
   357  
   358  	Init()
   359  	if modRoot == "" {
   360  		Target = module.Version{Path: "command-line-arguments"}
   361  		targetPrefix = "command-line-arguments"
   362  		buildList = []module.Version{Target}
   363  		return
   364  	}
   365  
   366  	gomod := ModFilePath()
   367  	data, err := lockedfile.Read(gomod)
   368  	if err != nil {
   369  		base.Fatalf("go: %v", err)
   370  	}
   371  
   372  	var fixed bool
   373  	f, err := modfile.Parse(gomod, data, fixVersion(ctx, &fixed))
   374  	if err != nil {
   375  		// Errors returned by modfile.Parse begin with file:line.
   376  		base.Fatalf("go: errors parsing go.mod:\n%s\n", err)
   377  	}
   378  	modFile = f
   379  	index = indexModFile(data, f, fixed)
   380  
   381  	if f.Module == nil {
   382  		// No module declaration. Must add module path.
   383  		base.Fatalf("go: no module declaration in go.mod. To specify the module path:\n\tgo mod edit -module=example.com/mod")
   384  	}
   385  
   386  	if err := checkModulePathLax(f.Module.Mod.Path); err != nil {
   387  		base.Fatalf("go: %v", err)
   388  	}
   389  
   390  	setDefaultBuildMod() // possibly enable automatic vendoring
   391  	modFileToBuildList()
   392  	if cfg.BuildMod == "vendor" {
   393  		readVendorList()
   394  		checkVendorConsistency()
   395  	}
   396  }
   397  
   398  // CreateModFile initializes a new module by creating a go.mod file.
   399  //
   400  // If modPath is empty, CreateModFile will attempt to infer the path from the
   401  // directory location within GOPATH.
   402  //
   403  // If a vendoring configuration file is present, CreateModFile will attempt to
   404  // translate it to go.mod directives. The resulting build list may not be
   405  // exactly the same as in the legacy configuration (for example, we can't get
   406  // packages at multiple versions from the same module).
   407  func CreateModFile(ctx context.Context, modPath string) {
   408  	modRoot = base.Cwd
   409  	Init()
   410  	modFilePath := ModFilePath()
   411  	if _, err := fsys.Stat(modFilePath); err == nil {
   412  		base.Fatalf("go: %s already exists", modFilePath)
   413  	}
   414  
   415  	if modPath == "" {
   416  		var err error
   417  		modPath, err = findModulePath(modRoot)
   418  		if err != nil {
   419  			base.Fatalf("go: %v", err)
   420  		}
   421  	} else if err := checkModulePathLax(modPath); err != nil {
   422  		base.Fatalf("go: %v", err)
   423  	}
   424  
   425  	fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", modPath)
   426  	modFile = new(modfile.File)
   427  	modFile.AddModuleStmt(modPath)
   428  	addGoStmt() // Add the go directive before converted module requirements.
   429  
   430  	convertedFrom, err := convertLegacyConfig(modPath)
   431  	if convertedFrom != "" {
   432  		fmt.Fprintf(os.Stderr, "go: copying requirements from %s\n", base.ShortPath(convertedFrom))
   433  	}
   434  	if err != nil {
   435  		base.Fatalf("go: %v", err)
   436  	}
   437  
   438  	modFileToBuildList()
   439  	WriteGoMod()
   440  
   441  	// Suggest running 'go mod tidy' unless the project is empty. Even if we
   442  	// imported all the correct requirements above, we're probably missing
   443  	// some sums, so the next build command in -mod=readonly will likely fail.
   444  	//
   445  	// We look for non-hidden .go files or subdirectories to determine whether
   446  	// this is an existing project. Walking the tree for packages would be more
   447  	// accurate, but could take much longer.
   448  	empty := true
   449  	files, _ := os.ReadDir(modRoot)
   450  	for _, f := range files {
   451  		name := f.Name()
   452  		if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
   453  			continue
   454  		}
   455  		if strings.HasSuffix(name, ".go") || f.IsDir() {
   456  			empty = false
   457  			break
   458  		}
   459  	}
   460  	if !empty {
   461  		fmt.Fprintf(os.Stderr, "go: to add module requirements and sums:\n\tgo mod tidy\n")
   462  	}
   463  }
   464  
   465  // checkModulePathLax checks that the path meets some minimum requirements
   466  // to avoid confusing users or the module cache. The requirements are weaker
   467  // than those of module.CheckPath to allow room for weakening module path
   468  // requirements in the future, but strong enough to help users avoid significant
   469  // problems.
   470  func checkModulePathLax(p string) error {
   471  	// TODO(matloob): Replace calls of this function in this CL with calls
   472  	// to module.CheckImportPath once it's been laxened, if it becomes laxened.
   473  	// See golang.org/issue/29101 for a discussion about whether to make CheckImportPath
   474  	// more lax or more strict.
   475  
   476  	errorf := func(format string, args ...interface{}) error {
   477  		return fmt.Errorf("invalid module path %q: %s", p, fmt.Sprintf(format, args...))
   478  	}
   479  
   480  	// Disallow shell characters " ' * < > ? ` | to avoid triggering bugs
   481  	// with file systems and subcommands. Disallow file path separators : and \
   482  	// because path separators other than / will confuse the module cache.
   483  	// See fileNameOK in golang.org/x/mod/module/module.go.
   484  	shellChars := "`" + `\"'*<>?|`
   485  	fsChars := `\:`
   486  	if i := strings.IndexAny(p, shellChars); i >= 0 {
   487  		return errorf("contains disallowed shell character %q", p[i])
   488  	}
   489  	if i := strings.IndexAny(p, fsChars); i >= 0 {
   490  		return errorf("contains disallowed path separator character %q", p[i])
   491  	}
   492  
   493  	// Ensure path.IsAbs and build.IsLocalImport are false, and that the path is
   494  	// invariant under path.Clean, also to avoid confusing the module cache.
   495  	if path.IsAbs(p) {
   496  		return errorf("is an absolute path")
   497  	}
   498  	if build.IsLocalImport(p) {
   499  		return errorf("is a local import path")
   500  	}
   501  	if path.Clean(p) != p {
   502  		return errorf("is not clean")
   503  	}
   504  
   505  	return nil
   506  }
   507  
   508  // fixVersion returns a modfile.VersionFixer implemented using the Query function.
   509  //
   510  // It resolves commit hashes and branch names to versions,
   511  // canonicalizes versions that appeared in early vgo drafts,
   512  // and does nothing for versions that already appear to be canonical.
   513  //
   514  // The VersionFixer sets 'fixed' if it ever returns a non-canonical version.
   515  func fixVersion(ctx context.Context, fixed *bool) modfile.VersionFixer {
   516  	return func(path, vers string) (resolved string, err error) {
   517  		defer func() {
   518  			if err == nil && resolved != vers {
   519  				*fixed = true
   520  			}
   521  		}()
   522  
   523  		// Special case: remove the old -gopkgin- hack.
   524  		if strings.HasPrefix(path, "gopkg.in/") && strings.Contains(vers, "-gopkgin-") {
   525  			vers = vers[strings.Index(vers, "-gopkgin-")+len("-gopkgin-"):]
   526  		}
   527  
   528  		// fixVersion is called speculatively on every
   529  		// module, version pair from every go.mod file.
   530  		// Avoid the query if it looks OK.
   531  		_, pathMajor, ok := module.SplitPathVersion(path)
   532  		if !ok {
   533  			return "", &module.ModuleError{
   534  				Path: path,
   535  				Err: &module.InvalidVersionError{
   536  					Version: vers,
   537  					Err:     fmt.Errorf("malformed module path %q", path),
   538  				},
   539  			}
   540  		}
   541  		if vers != "" && module.CanonicalVersion(vers) == vers {
   542  			if err := module.CheckPathMajor(vers, pathMajor); err == nil {
   543  				return vers, nil
   544  			}
   545  		}
   546  
   547  		info, err := Query(ctx, path, vers, "", nil)
   548  		if err != nil {
   549  			return "", err
   550  		}
   551  		return info.Version, nil
   552  	}
   553  }
   554  
   555  // AllowMissingModuleImports allows import paths to be resolved to modules
   556  // when there is no module root. Normally, this is forbidden because it's slow
   557  // and there's no way to make the result reproducible, but some commands
   558  // like 'go get' are expected to do this.
   559  func AllowMissingModuleImports() {
   560  	allowMissingModuleImports = true
   561  }
   562  
   563  // modFileToBuildList initializes buildList from the modFile.
   564  func modFileToBuildList() {
   565  	Target = modFile.Module.Mod
   566  	targetPrefix = Target.Path
   567  	if rel := search.InDir(base.Cwd, cfg.GOROOTsrc); rel != "" {
   568  		targetInGorootSrc = true
   569  		if Target.Path == "std" {
   570  			targetPrefix = ""
   571  		}
   572  	}
   573  
   574  	list := []module.Version{Target}
   575  	for _, r := range modFile.Require {
   576  		if index != nil && index.exclude[r.Mod] {
   577  			if cfg.BuildMod == "mod" {
   578  				fmt.Fprintf(os.Stderr, "go: dropping requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
   579  			} else {
   580  				fmt.Fprintf(os.Stderr, "go: ignoring requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
   581  			}
   582  		} else {
   583  			list = append(list, r.Mod)
   584  		}
   585  	}
   586  	buildList = list
   587  }
   588  
   589  // setDefaultBuildMod sets a default value for cfg.BuildMod if the -mod flag
   590  // wasn't provided. setDefaultBuildMod may be called multiple times.
   591  func setDefaultBuildMod() {
   592  	if cfg.BuildModExplicit {
   593  		// Don't override an explicit '-mod=' argument.
   594  		return
   595  	}
   596  
   597  	if cfg.CmdName == "get" || strings.HasPrefix(cfg.CmdName, "mod ") {
   598  		// 'get' and 'go mod' commands may update go.mod automatically.
   599  		// TODO(jayconrod): should this narrower? Should 'go mod download' or
   600  		// 'go mod graph' update go.mod by default?
   601  		cfg.BuildMod = "mod"
   602  		return
   603  	}
   604  	if modRoot == "" {
   605  		cfg.BuildMod = "readonly"
   606  		return
   607  	}
   608  
   609  	if fi, err := fsys.Stat(filepath.Join(modRoot, "vendor")); err == nil && fi.IsDir() {
   610  		modGo := "unspecified"
   611  		if index != nil && index.goVersionV != "" {
   612  			if semver.Compare(index.goVersionV, "v1.14") >= 0 {
   613  				// The Go version is at least 1.14, and a vendor directory exists.
   614  				// Set -mod=vendor by default.
   615  				cfg.BuildMod = "vendor"
   616  				cfg.BuildModReason = "Go version in go.mod is at least 1.14 and vendor directory exists."
   617  				return
   618  			} else {
   619  				modGo = index.goVersionV[1:]
   620  			}
   621  		}
   622  
   623  		// Since a vendor directory exists, we should record why we didn't use it.
   624  		// This message won't normally be shown, but it may appear with import errors.
   625  		cfg.BuildModReason = fmt.Sprintf("Go version in go.mod is %s, so vendor directory was not used.", modGo)
   626  	}
   627  
   628  	cfg.BuildMod = "readonly"
   629  }
   630  
   631  // convertLegacyConfig imports module requirements from a legacy vendoring
   632  // configuration file, if one is present.
   633  func convertLegacyConfig(modPath string) (from string, err error) {
   634  	for _, name := range altConfigs {
   635  		cfg := filepath.Join(modRoot, name)
   636  		data, err := os.ReadFile(cfg)
   637  		if err == nil {
   638  			convert := modconv.Converters[name]
   639  			if convert == nil {
   640  				return "", nil
   641  			}
   642  			cfg = filepath.ToSlash(cfg)
   643  			err := modconv.ConvertLegacyConfig(modFile, cfg, data)
   644  			return name, err
   645  		}
   646  	}
   647  	return "", nil
   648  }
   649  
   650  // addGoStmt adds a go directive to the go.mod file if it does not already include one.
   651  // The 'go' version added, if any, is the latest version supported by this toolchain.
   652  func addGoStmt() {
   653  	if modFile.Go != nil && modFile.Go.Version != "" {
   654  		return
   655  	}
   656  	tags := build.Default.ReleaseTags
   657  	version := tags[len(tags)-1]
   658  	if !strings.HasPrefix(version, "go") || !modfile.GoVersionRE.MatchString(version[2:]) {
   659  		base.Fatalf("go: unrecognized default version %q", version)
   660  	}
   661  	if err := modFile.AddGoStmt(version[2:]); err != nil {
   662  		base.Fatalf("go: internal error: %v", err)
   663  	}
   664  }
   665  
   666  var altConfigs = []string{
   667  	"Gopkg.lock",
   668  
   669  	"GLOCKFILE",
   670  	"Godeps/Godeps.json",
   671  	"dependencies.tsv",
   672  	"glide.lock",
   673  	"vendor.conf",
   674  	"vendor.yml",
   675  	"vendor/manifest",
   676  	"vendor/vendor.json",
   677  
   678  	".git/config",
   679  }
   680  
   681  func findModuleRoot(dir string) (root string) {
   682  	if dir == "" {
   683  		panic("dir not set")
   684  	}
   685  	dir = filepath.Clean(dir)
   686  
   687  	// Look for enclosing go.mod.
   688  	for {
   689  		if fi, err := fsys.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {
   690  			return dir
   691  		}
   692  		d := filepath.Dir(dir)
   693  		if d == dir {
   694  			break
   695  		}
   696  		dir = d
   697  	}
   698  	return ""
   699  }
   700  
   701  func findAltConfig(dir string) (root, name string) {
   702  	if dir == "" {
   703  		panic("dir not set")
   704  	}
   705  	dir = filepath.Clean(dir)
   706  	if rel := search.InDir(dir, cfg.BuildContext.GOROOT); rel != "" {
   707  		// Don't suggest creating a module from $GOROOT/.git/config
   708  		// or a config file found in any parent of $GOROOT (see #34191).
   709  		return "", ""
   710  	}
   711  	for {
   712  		for _, name := range altConfigs {
   713  			if fi, err := fsys.Stat(filepath.Join(dir, name)); err == nil && !fi.IsDir() {
   714  				return dir, name
   715  			}
   716  		}
   717  		d := filepath.Dir(dir)
   718  		if d == dir {
   719  			break
   720  		}
   721  		dir = d
   722  	}
   723  	return "", ""
   724  }
   725  
   726  func findModulePath(dir string) (string, error) {
   727  	// TODO(bcmills): once we have located a plausible module path, we should
   728  	// query version control (if available) to verify that it matches the major
   729  	// version of the most recent tag.
   730  	// See https://golang.org/issue/29433, https://golang.org/issue/27009, and
   731  	// https://golang.org/issue/31549.
   732  
   733  	// Cast about for import comments,
   734  	// first in top-level directory, then in subdirectories.
   735  	list, _ := os.ReadDir(dir)
   736  	for _, info := range list {
   737  		if info.Type().IsRegular() && strings.HasSuffix(info.Name(), ".go") {
   738  			if com := findImportComment(filepath.Join(dir, info.Name())); com != "" {
   739  				return com, nil
   740  			}
   741  		}
   742  	}
   743  	for _, info1 := range list {
   744  		if info1.IsDir() {
   745  			files, _ := os.ReadDir(filepath.Join(dir, info1.Name()))
   746  			for _, info2 := range files {
   747  				if info2.Type().IsRegular() && strings.HasSuffix(info2.Name(), ".go") {
   748  					if com := findImportComment(filepath.Join(dir, info1.Name(), info2.Name())); com != "" {
   749  						return path.Dir(com), nil
   750  					}
   751  				}
   752  			}
   753  		}
   754  	}
   755  
   756  	// Look for Godeps.json declaring import path.
   757  	data, _ := os.ReadFile(filepath.Join(dir, "Godeps/Godeps.json"))
   758  	var cfg1 struct{ ImportPath string }
   759  	json.Unmarshal(data, &cfg1)
   760  	if cfg1.ImportPath != "" {
   761  		return cfg1.ImportPath, nil
   762  	}
   763  
   764  	// Look for vendor.json declaring import path.
   765  	data, _ = os.ReadFile(filepath.Join(dir, "vendor/vendor.json"))
   766  	var cfg2 struct{ RootPath string }
   767  	json.Unmarshal(data, &cfg2)
   768  	if cfg2.RootPath != "" {
   769  		return cfg2.RootPath, nil
   770  	}
   771  
   772  	// Look for path in GOPATH.
   773  	var badPathErr error
   774  	for _, gpdir := range filepath.SplitList(cfg.BuildContext.GOPATH) {
   775  		if gpdir == "" {
   776  			continue
   777  		}
   778  		if rel := search.InDir(dir, filepath.Join(gpdir, "src")); rel != "" && rel != "." {
   779  			path := filepath.ToSlash(rel)
   780  			// TODO(matloob): replace this with module.CheckImportPath
   781  			// once it's been laxened.
   782  			// Only checkModulePathLax here. There are some unpublishable
   783  			// module names that are compatible with checkModulePathLax
   784  			// but they already work in GOPATH so don't break users
   785  			// trying to do a build with modules. gorelease will alert users
   786  			// publishing their modules to fix their paths.
   787  			if err := checkModulePathLax(path); err != nil {
   788  				badPathErr = err
   789  				break
   790  			}
   791  			return path, nil
   792  		}
   793  	}
   794  
   795  	reason := "outside GOPATH, module path must be specified"
   796  	if badPathErr != nil {
   797  		// return a different error message if the module was in GOPATH, but
   798  		// the module path determined above would be an invalid path.
   799  		reason = fmt.Sprintf("bad module path inferred from directory in GOPATH: %v", badPathErr)
   800  	}
   801  	msg := `cannot determine module path for source directory %s (%s)
   802  
   803  Example usage:
   804  	'go mod init example.com/m' to initialize a v0 or v1 module
   805  	'go mod init example.com/m/v2' to initialize a v2 module
   806  
   807  Run 'go help mod init' for more information.
   808  `
   809  	return "", fmt.Errorf(msg, dir, reason)
   810  }
   811  
   812  var (
   813  	importCommentRE = lazyregexp.New(`(?m)^package[ \t]+[^ \t\r\n/]+[ \t]+//[ \t]+import[ \t]+(\"[^"]+\")[ \t]*\r?\n`)
   814  )
   815  
   816  func findImportComment(file string) string {
   817  	data, err := os.ReadFile(file)
   818  	if err != nil {
   819  		return ""
   820  	}
   821  	m := importCommentRE.FindSubmatch(data)
   822  	if m == nil {
   823  		return ""
   824  	}
   825  	path, err := strconv.Unquote(string(m[1]))
   826  	if err != nil {
   827  		return ""
   828  	}
   829  	return path
   830  }
   831  
   832  var allowWriteGoMod = true
   833  
   834  // DisallowWriteGoMod causes future calls to WriteGoMod to do nothing at all.
   835  func DisallowWriteGoMod() {
   836  	allowWriteGoMod = false
   837  }
   838  
   839  // AllowWriteGoMod undoes the effect of DisallowWriteGoMod:
   840  // future calls to WriteGoMod will update go.mod if needed.
   841  // Note that any past calls have been discarded, so typically
   842  // a call to AlowWriteGoMod should be followed by a call to WriteGoMod.
   843  func AllowWriteGoMod() {
   844  	allowWriteGoMod = true
   845  }
   846  
   847  // MinReqs returns a Reqs with minimal additional dependencies of Target,
   848  // as will be written to go.mod.
   849  func MinReqs() mvs.Reqs {
   850  	retain := append([]string{}, additionalExplicitRequirements...)
   851  	for _, m := range buildList[1:] {
   852  		_, explicit := index.require[m]
   853  		if explicit || loaded.direct[m.Path] {
   854  			retain = append(retain, m.Path)
   855  		}
   856  	}
   857  	sort.Strings(retain)
   858  	str.Uniq(&retain)
   859  	min, err := mvs.Req(Target, retain, &mvsReqs{buildList: buildList})
   860  	if err != nil {
   861  		base.Fatalf("go: %v", err)
   862  	}
   863  	return &mvsReqs{buildList: append([]module.Version{Target}, min...)}
   864  }
   865  
   866  // WriteGoMod writes the current build list back to go.mod.
   867  func WriteGoMod() {
   868  	// If we're using -mod=vendor we basically ignored
   869  	// go.mod, so definitely don't try to write back our
   870  	// incomplete view of the world.
   871  	if !allowWriteGoMod || cfg.BuildMod == "vendor" {
   872  		return
   873  	}
   874  
   875  	// If we aren't in a module, we don't have anywhere to write a go.mod file.
   876  	if modRoot == "" {
   877  		return
   878  	}
   879  
   880  	if cfg.BuildMod != "readonly" {
   881  		addGoStmt()
   882  	}
   883  
   884  	if loaded != nil {
   885  		reqs := MinReqs()
   886  		min, err := reqs.Required(Target)
   887  		if err != nil {
   888  			base.Fatalf("go: %v", err)
   889  		}
   890  		var list []*modfile.Require
   891  		for _, m := range min {
   892  			list = append(list, &modfile.Require{
   893  				Mod:      m,
   894  				Indirect: !loaded.direct[m.Path],
   895  			})
   896  		}
   897  		modFile.SetRequire(list)
   898  	}
   899  	modFile.Cleanup()
   900  
   901  	dirty := index.modFileIsDirty(modFile)
   902  	if dirty && cfg.BuildMod == "readonly" {
   903  		// If we're about to fail due to -mod=readonly,
   904  		// prefer to report a dirty go.mod over a dirty go.sum
   905  		if cfg.BuildModExplicit {
   906  			base.Fatalf("go: updates to go.mod needed, disabled by -mod=readonly")
   907  		} else if cfg.BuildModReason != "" {
   908  			base.Fatalf("go: updates to go.mod needed, disabled by -mod=readonly\n\t(%s)", cfg.BuildModReason)
   909  		} else {
   910  			base.Fatalf("go: updates to go.mod needed; to update it:\n\tgo mod tidy")
   911  		}
   912  	}
   913  
   914  	if !dirty && cfg.CmdName != "mod tidy" {
   915  		// The go.mod file has the same semantic content that it had before
   916  		// (but not necessarily the same exact bytes).
   917  		// Don't write go.mod, but write go.sum in case we added or trimmed sums.
   918  		// 'go mod init' shouldn't write go.sum, since it will be incomplete.
   919  		if cfg.CmdName != "mod init" {
   920  			modfetch.WriteGoSum(keepSums(true))
   921  		}
   922  		return
   923  	}
   924  
   925  	new, err := modFile.Format()
   926  	if err != nil {
   927  		base.Fatalf("go: %v", err)
   928  	}
   929  	defer func() {
   930  		// At this point we have determined to make the go.mod file on disk equal to new.
   931  		index = indexModFile(new, modFile, false)
   932  
   933  		// Update go.sum after releasing the side lock and refreshing the index.
   934  		// 'go mod init' shouldn't write go.sum, since it will be incomplete.
   935  		if cfg.CmdName != "mod init" {
   936  			modfetch.WriteGoSum(keepSums(true))
   937  		}
   938  	}()
   939  
   940  	// Make a best-effort attempt to acquire the side lock, only to exclude
   941  	// previous versions of the 'go' command from making simultaneous edits.
   942  	if unlock, err := modfetch.SideLock(); err == nil {
   943  		defer unlock()
   944  	}
   945  
   946  	errNoChange := errors.New("no update needed")
   947  
   948  	err = lockedfile.Transform(ModFilePath(), func(old []byte) ([]byte, error) {
   949  		if bytes.Equal(old, new) {
   950  			// The go.mod file is already equal to new, possibly as the result of some
   951  			// other process.
   952  			return nil, errNoChange
   953  		}
   954  
   955  		if index != nil && !bytes.Equal(old, index.data) {
   956  			// The contents of the go.mod file have changed. In theory we could add all
   957  			// of the new modules to the build list, recompute, and check whether any
   958  			// module in *our* build list got bumped to a different version, but that's
   959  			// a lot of work for marginal benefit. Instead, fail the command: if users
   960  			// want to run concurrent commands, they need to start with a complete,
   961  			// consistent module definition.
   962  			return nil, fmt.Errorf("existing contents have changed since last read")
   963  		}
   964  
   965  		return new, nil
   966  	})
   967  
   968  	if err != nil && err != errNoChange {
   969  		base.Fatalf("go: updating go.mod: %v", err)
   970  	}
   971  }
   972  
   973  // keepSums returns a set of module sums to preserve in go.sum. The set
   974  // includes entries for all modules used to load packages (according to
   975  // the last load function such as LoadPackages or ImportFromFiles).
   976  // It also contains entries for go.mod files needed for MVS (the version
   977  // of these entries ends with "/go.mod").
   978  //
   979  // If keepBuildListZips is true, the set also includes sums for zip files for
   980  // all modules in the build list with replacements applied. 'go get' and
   981  // 'go mod download' may add sums to this set when adding a requirement on a
   982  // module without a root package or when downloading a direct or indirect
   983  // dependency.
   984  func keepSums(keepBuildListZips bool) map[module.Version]bool {
   985  	// Re-derive the build list using the current list of direct requirements.
   986  	// Keep the sum for the go.mod of each visited module version (or its
   987  	// replacement).
   988  	modkey := func(m module.Version) module.Version {
   989  		return module.Version{Path: m.Path, Version: m.Version + "/go.mod"}
   990  	}
   991  	keep := make(map[module.Version]bool)
   992  	var mu sync.Mutex
   993  	reqs := &keepSumReqs{
   994  		Reqs: &mvsReqs{buildList: buildList},
   995  		visit: func(m module.Version) {
   996  			// If we build using a replacement module, keep the sum for the replacement,
   997  			// since that's the code we'll actually use during a build.
   998  			mu.Lock()
   999  			r := Replacement(m)
  1000  			if r.Path == "" {
  1001  				keep[modkey(m)] = true
  1002  			} else {
  1003  				keep[modkey(r)] = true
  1004  			}
  1005  			mu.Unlock()
  1006  		},
  1007  	}
  1008  	buildList, err := mvs.BuildList(Target, reqs)
  1009  	if err != nil {
  1010  		panic(fmt.Sprintf("unexpected error reloading build list: %v", err))
  1011  	}
  1012  
  1013  	actualMods := make(map[string]module.Version)
  1014  	for _, m := range buildList[1:] {
  1015  		if r := Replacement(m); r.Path != "" {
  1016  			actualMods[m.Path] = r
  1017  		} else {
  1018  			actualMods[m.Path] = m
  1019  		}
  1020  	}
  1021  
  1022  	// Add entries for modules in the build list with paths that are prefixes of
  1023  	// paths of loaded packages. We need to retain sums for modules needed to
  1024  	// report ambiguous import errors. We use our re-derived build list,
  1025  	// since the global build list may have been tidied.
  1026  	if loaded != nil {
  1027  		for _, pkg := range loaded.pkgs {
  1028  			if pkg.testOf != nil || pkg.inStd || module.CheckImportPath(pkg.path) != nil {
  1029  				continue
  1030  			}
  1031  			for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) {
  1032  				if m, ok := actualMods[prefix]; ok {
  1033  					keep[m] = true
  1034  				}
  1035  			}
  1036  		}
  1037  	}
  1038  
  1039  	// Add entries for the zip of each module in the build list.
  1040  	// We might not need all of these (tidy does not add them), but they may be
  1041  	// added by a specific 'go get' or 'go mod download' command to resolve
  1042  	// missing import sum errors.
  1043  	if keepBuildListZips {
  1044  		for _, m := range actualMods {
  1045  			keep[m] = true
  1046  		}
  1047  	}
  1048  
  1049  	return keep
  1050  }
  1051  
  1052  // keepSumReqs embeds another Reqs implementation. The Required method
  1053  // calls visit for each version in the module graph.
  1054  type keepSumReqs struct {
  1055  	mvs.Reqs
  1056  	visit func(module.Version)
  1057  }
  1058  
  1059  func (r *keepSumReqs) Required(m module.Version) ([]module.Version, error) {
  1060  	r.visit(m)
  1061  	return r.Reqs.Required(m)
  1062  }
  1063  
  1064  func TrimGoSum() {
  1065  	// Don't retain sums for the zip file of every module in the build list.
  1066  	// We may not need them all to build the main module's packages.
  1067  	keepBuildListZips := false
  1068  	modfetch.TrimGoSum(keepSums(keepBuildListZips))
  1069  }
  1070  

View as plain text