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.
23 "cmd/go/internal/base"
25 "cmd/go/internal/fsys"
26 "cmd/go/internal/gover"
27 "cmd/go/internal/lockedfile"
28 "cmd/go/internal/modfetch"
29 "cmd/go/internal/search"
31 "golang.org/x/mod/modfile"
32 "golang.org/x/mod/module"
35 // Variables set by other packages.
37 // TODO(#40775): See if these can be plumbed as explicit parameters.
39 // RootMode determines whether a module root is needed.
42 // ForceUseModules may be set to force modules to be enabled when
43 // GO111MODULE=auto or to report an error when GO111MODULE=off.
46 allowMissingModuleImports bool
48 // ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions
49 // from updating go.mod and go.sum or reporting errors when updates are
50 // needed. A package should set this if it would cause go.mod to be written
51 // multiple times (for example, 'go get' calls LoadPackages multiple times) or
52 // if it needs some other operation to be successful before go.mod and go.sum
53 // can be written (for example, 'go mod download' must download modules before
54 // adding sums to go.sum). Packages that set this are responsible for calling
55 // WriteGoMod explicitly.
56 ExplicitWriteGoMod bool
59 // Variables set in Init.
63 // These are primarily used to initialize the MainModules, and should be
64 // eventually superseded by them but are still used in cases where the module
65 // roots are required but MainModules hasn't been initialized yet. Set to
66 // the modRoots of the main modules.
67 // modRoots != nil implies len(modRoots) > 0
72 // EnterModule resets MainModules and requirements to refer to just this one module.
73 func EnterModule(ctx context.Context, enterModroot string) {
74 MainModules = nil // reset MainModules
76 workFilePath = "" // Force module mode
79 modRoots = []string{enterModroot}
83 // Variable set in InitWorkfile
85 // Set to the path to the go.work file, or "" if workspace mode is disabled.
89 type MainModuleSet struct {
90 // versions are the module.Version values of each of the main modules.
91 // For each of them, the Path fields are ordinary module paths and the Version
92 // fields are empty strings.
93 // versions is clipped (len=cap).
94 versions []module.Version
96 // modRoot maps each module in versions to its absolute filesystem path.
97 modRoot map[module.Version]string
99 // pathPrefix is the path prefix for packages in the module, without a trailing
100 // slash. For most modules, pathPrefix is just version.Path, but the
101 // standard-library module "std" has an empty prefix.
102 pathPrefix map[module.Version]string
104 // inGorootSrc caches whether modRoot is within GOROOT/src.
105 // The "std" module is special within GOROOT/src, but not otherwise.
106 inGorootSrc map[module.Version]bool
108 modFiles map[module.Version]*modfile.File
110 modContainingCWD module.Version
112 workFile *modfile.WorkFile
114 workFileReplaceMap map[module.Version]module.Version
115 // highest replaced version of each module path; empty string for wildcard-only replacements
116 highestReplaced map[string]string
119 indices map[module.Version]*modFileIndex
122 func (mms *MainModuleSet) PathPrefix(m module.Version) string {
123 return mms.pathPrefix[m]
126 // Versions returns the module.Version values of each of the main modules.
127 // For each of them, the Path fields are ordinary module paths and the Version
128 // fields are empty strings.
129 // Callers should not modify the returned slice.
130 func (mms *MainModuleSet) Versions() []module.Version {
137 func (mms *MainModuleSet) Contains(path string) bool {
141 for _, v := range mms.versions {
149 func (mms *MainModuleSet) ModRoot(m module.Version) string {
153 return mms.modRoot[m]
156 func (mms *MainModuleSet) InGorootSrc(m module.Version) bool {
160 return mms.inGorootSrc[m]
163 func (mms *MainModuleSet) mustGetSingleMainModule() module.Version {
164 if mms == nil || len(mms.versions) == 0 {
165 panic("internal error: mustGetSingleMainModule called in context with no main modules")
167 if len(mms.versions) != 1 {
168 if inWorkspaceMode() {
169 panic("internal error: mustGetSingleMainModule called in workspace mode")
171 panic("internal error: multiple main modules present outside of workspace mode")
174 return mms.versions[0]
177 func (mms *MainModuleSet) GetSingleIndexOrNil() *modFileIndex {
181 if len(mms.versions) == 0 {
184 return mms.indices[mms.mustGetSingleMainModule()]
187 func (mms *MainModuleSet) Index(m module.Version) *modFileIndex {
189 defer mms.indexMu.Unlock()
190 return mms.indices[m]
193 func (mms *MainModuleSet) SetIndex(m module.Version, index *modFileIndex) {
195 defer mms.indexMu.Unlock()
196 mms.indices[m] = index
199 func (mms *MainModuleSet) ModFile(m module.Version) *modfile.File {
200 return mms.modFiles[m]
203 func (mms *MainModuleSet) WorkFile() *modfile.WorkFile {
207 func (mms *MainModuleSet) Len() int {
211 return len(mms.versions)
214 // ModContainingCWD returns the main module containing the working directory,
215 // or module.Version{} if none of the main modules contain the working
217 func (mms *MainModuleSet) ModContainingCWD() module.Version {
218 return mms.modContainingCWD
221 func (mms *MainModuleSet) HighestReplaced() map[string]string {
222 return mms.highestReplaced
225 // GoVersion returns the go version set on the single module, in module mode,
226 // or the go.work file in workspace mode.
227 func (mms *MainModuleSet) GoVersion() string {
228 if inWorkspaceMode() {
229 return gover.FromGoWork(mms.workFile)
231 if mms != nil && len(mms.versions) == 1 {
232 f := mms.ModFile(mms.mustGetSingleMainModule())
234 // Special case: we are outside a module, like 'go run x.go'.
235 // Assume the local Go version.
236 // TODO(#49228): Clean this up; see loadModFile.
239 return gover.FromGoMod(f)
241 return gover.DefaultGoModVersion
244 // Toolchain returns the toolchain set on the single module, in module mode,
245 // or the go.work file in workspace mode.
246 func (mms *MainModuleSet) Toolchain() string {
247 if inWorkspaceMode() {
248 if mms.workFile != nil && mms.workFile.Toolchain != nil {
249 return mms.workFile.Toolchain.Name
251 return "go" + mms.GoVersion()
253 if mms != nil && len(mms.versions) == 1 {
254 f := mms.ModFile(mms.mustGetSingleMainModule())
256 // Special case: we are outside a module, like 'go run x.go'.
257 // Assume the local Go version.
258 // TODO(#49228): Clean this up; see loadModFile.
259 return gover.LocalToolchain()
261 if f.Toolchain != nil {
262 return f.Toolchain.Name
265 return "go" + mms.GoVersion()
268 func (mms *MainModuleSet) WorkFileReplaceMap() map[module.Version]module.Version {
269 return mms.workFileReplaceMap
272 var MainModules *MainModuleSet
277 // AutoRoot is the default for most commands. modload.Init will look for
278 // a go.mod file in the current directory or any parent. If none is found,
279 // modules may be disabled (GO111MODULE=auto) or commands may run in a
280 // limited module mode.
283 // NoRoot is used for commands that run in module mode and ignore any go.mod
284 // file the current directory or in parent directories.
287 // NeedRoot is used for commands that must run in module mode and don't
288 // make sense without a main module.
292 // ModFile returns the parsed go.mod file.
294 // Note that after calling LoadPackages or LoadModGraph,
295 // the require statements in the modfile.File are no longer
296 // the source of truth and will be ignored: edits made directly
297 // will be lost at the next call to WriteGoMod.
298 // To make permanent changes to the require statements
299 // in go.mod, edit it before loading.
300 func ModFile() *modfile.File {
302 modFile := MainModules.ModFile(MainModules.mustGetSingleMainModule())
309 func BinDir() string {
317 return filepath.Join(gopath, "bin")
320 // InitWorkfile initializes the workFilePath variable for commands that
321 // operate in workspace mode. It should not be called by other commands,
322 // for example 'go mod tidy', that don't operate in workspace mode.
323 func InitWorkfile() {
324 workFilePath = FindGoWork(base.Cwd())
327 // FindGoWork returns the name of the go.work file for this command,
328 // or the empty string if there isn't one.
329 // Most code should use Init and Enabled rather than use this directly.
330 // It is exported mainly for Go toolchain switching, which must process
331 // the go.work very early at startup.
332 func FindGoWork(wd string) string {
333 if RootMode == NoRoot {
337 switch gowork := cfg.Getenv("GOWORK"); gowork {
341 return findWorkspaceFile(wd)
343 if !filepath.IsAbs(gowork) {
344 base.Fatalf("go: invalid GOWORK: not an absolute path")
350 // WorkFilePath returns the absolute path of the go.work file, or "" if not in
351 // workspace mode. WorkFilePath must be called after InitWorkfile.
352 func WorkFilePath() string {
356 // Reset clears all the initialized, cached state about the use of modules,
357 // so that we can start over.
360 ForceUseModules = false
363 cfg.ModulesEnabled = false
370 // Init determines whether module mode is enabled, locates the root of the
371 // current module (if any), sets environment variables for Git subprocesses, and
372 // configures the cfg, codehost, load, modfetch, and search packages for use
380 // Keep in sync with WillBeEnabled. We perform extra validation here, and
381 // there are lots of diagnostics and side effects, so we can't use
382 // WillBeEnabled directly.
383 var mustUseModules bool
384 env := cfg.Getenv("GO111MODULE")
387 base.Fatalf("go: unknown environment setting GO111MODULE=%s", env)
389 mustUseModules = ForceUseModules
391 mustUseModules = true
394 base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
396 mustUseModules = false
400 if err := fsys.Init(base.Cwd()); err != nil {
404 // Disable any prompting for passwords by Git.
405 // Only has an effect for 2.3.0 or later, but avoiding
406 // the prompt in earlier versions is just too hard.
407 // If user has explicitly set GIT_TERMINAL_PROMPT=1, keep
409 // See golang.org/issue/9341 and golang.org/issue/12706.
410 if os.Getenv("GIT_TERMINAL_PROMPT") == "" {
411 os.Setenv("GIT_TERMINAL_PROMPT", "0")
414 // Disable any ssh connection pooling by Git.
415 // If a Git subprocess forks a child into the background to cache a new connection,
416 // that child keeps stdout/stderr open. After the Git subprocess exits,
417 // os/exec expects to be able to read from the stdout/stderr pipe
418 // until EOF to get all the data that the Git subprocess wrote before exiting.
419 // The EOF doesn't come until the child exits too, because the child
420 // is holding the write end of the pipe.
421 // This is unfortunate, but it has come up at least twice
422 // (see golang.org/issue/13453 and golang.org/issue/16104)
423 // and confuses users when it does.
424 // If the user has explicitly set GIT_SSH or GIT_SSH_COMMAND,
425 // assume they know what they are doing and don't step on it.
426 // But default to turning off ControlMaster.
427 if os.Getenv("GIT_SSH") == "" && os.Getenv("GIT_SSH_COMMAND") == "" {
428 os.Setenv("GIT_SSH_COMMAND", "ssh -o ControlMaster=no -o BatchMode=yes")
431 if os.Getenv("GCM_INTERACTIVE") == "" {
432 os.Setenv("GCM_INTERACTIVE", "never")
435 // modRoot set before Init was called ("go mod init" does this).
436 // No need to search for go.mod.
437 } else if RootMode == NoRoot {
438 if cfg.ModFile != "" && !base.InGOFLAGS("-modfile") {
439 base.Fatalf("go: -modfile cannot be used with commands that ignore the current module")
442 } else if workFilePath != "" {
443 // We're in workspace mode, which implies module mode.
444 if cfg.ModFile != "" {
445 base.Fatalf("go: -modfile cannot be used in workspace mode")
448 if modRoot := findModuleRoot(base.Cwd()); modRoot == "" {
449 if cfg.ModFile != "" {
450 base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.")
452 if RootMode == NeedRoot {
453 base.Fatal(ErrNoModRoot)
456 // GO111MODULE is 'auto', and we can't find a module root.
457 // Stay in GOPATH mode.
460 } else if search.InDir(modRoot, os.TempDir()) == "." {
461 // If you create /tmp/go.mod for experimenting,
462 // then any tests that create work directories under /tmp
463 // will find it and get modules when they're not expecting them.
464 // It's a bit of a peculiar thing to disallow but quite mysterious
465 // when it happens. See golang.org/issue/26708.
466 fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in system temp root %v\n", os.TempDir())
467 if RootMode == NeedRoot {
468 base.Fatal(ErrNoModRoot)
474 modRoots = []string{modRoot}
477 if cfg.ModFile != "" && !strings.HasSuffix(cfg.ModFile, ".mod") {
478 base.Fatalf("go: -modfile=%s: file does not have .mod extension", cfg.ModFile)
481 // We're in module mode. Set any global variables that need to be set.
482 cfg.ModulesEnabled = true
484 list := filepath.SplitList(cfg.BuildContext.GOPATH)
485 if len(list) > 0 && list[0] != "" {
487 if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil {
488 fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in $GOPATH %v\n", gopath)
489 if RootMode == NeedRoot {
490 base.Fatal(ErrNoModRoot)
499 // WillBeEnabled checks whether modules should be enabled but does not
500 // initialize modules by installing hooks. If Init has already been called,
501 // WillBeEnabled returns the same result as Enabled.
503 // This function is needed to break a cycle. The main package needs to know
504 // whether modules are enabled in order to install the module or GOPATH version
505 // of 'go get', but Init reads the -modfile flag in 'go get', so it shouldn't
506 // be called until the command is installed and flags are parsed. Instead of
507 // calling Init and Enabled, the main package can call this function.
508 func WillBeEnabled() bool {
509 if modRoots != nil || cfg.ModulesEnabled {
514 // Initialized, not enabled.
518 // Keep in sync with Init. Init does extra validation and prints warnings or
519 // exits, so it can't call this function directly.
520 env := cfg.Getenv("GO111MODULE")
530 return FindGoMod(base.Cwd()) != ""
533 // FindGoMod returns the name of the go.mod file for this command,
534 // or the empty string if there isn't one.
535 // Most code should use Init and Enabled rather than use this directly.
536 // It is exported mainly for Go toolchain switching, which must process
537 // the go.mod very early at startup.
538 func FindGoMod(wd string) string {
539 modRoot := findModuleRoot(wd)
541 // GO111MODULE is 'auto', and we can't find a module root.
542 // Stay in GOPATH mode.
545 if search.InDir(modRoot, os.TempDir()) == "." {
546 // If you create /tmp/go.mod for experimenting,
547 // then any tests that create work directories under /tmp
548 // will find it and get modules when they're not expecting them.
549 // It's a bit of a peculiar thing to disallow but quite mysterious
550 // when it happens. See golang.org/issue/26708.
553 return filepath.Join(modRoot, "go.mod")
556 // Enabled reports whether modules are (or must be) enabled.
557 // If modules are enabled but there is no main module, Enabled returns true
558 // and then the first use of module information will call die
559 // (usually through MustModRoot).
560 func Enabled() bool {
562 return modRoots != nil || cfg.ModulesEnabled
565 func VendorDir() string {
566 if inWorkspaceMode() {
567 return filepath.Join(filepath.Dir(WorkFilePath()), "vendor")
569 // Even if -mod=vendor, we could be operating with no mod root (and thus no
570 // vendor directory). As long as there are no dependencies that is expected
571 // to work. See script/vendor_outside_module.txt.
572 modRoot := MainModules.ModRoot(MainModules.mustGetSingleMainModule())
574 panic("vendor directory does not exist when in single module mode outside of a module")
576 return filepath.Join(modRoot, "vendor")
579 func inWorkspaceMode() bool {
581 panic("inWorkspaceMode called before modload.Init called")
586 return workFilePath != ""
589 // HasModRoot reports whether a main module is present.
590 // HasModRoot may return false even if Enabled returns true: for example, 'get'
591 // does not require a main module.
592 func HasModRoot() bool {
594 return modRoots != nil
597 // MustHaveModRoot checks that a main module or main modules are present,
598 // and calls base.Fatalf if there are no main modules.
599 func MustHaveModRoot() {
606 // ModFilePath returns the path that would be used for the go.mod
607 // file, if in module mode. ModFilePath calls base.Fatalf if there is no main
608 // module, even if -modfile is set.
609 func ModFilePath() string {
611 return modFilePath(findModuleRoot(base.Cwd()))
614 func modFilePath(modRoot string) string {
615 if cfg.ModFile != "" {
618 return filepath.Join(modRoot, "go.mod")
622 if cfg.Getenv("GO111MODULE") == "off" {
623 base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
625 if inWorkspaceMode() {
626 base.Fatalf("go: no modules were found in the current workspace; see 'go help work'")
628 if dir, name := findAltConfig(base.Cwd()); dir != "" {
629 rel, err := filepath.Rel(base.Cwd(), dir)
635 cdCmd = fmt.Sprintf("cd %s && ", rel)
637 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)
639 base.Fatal(ErrNoModRoot)
642 var ErrNoModRoot = errors.New("go.mod file not found in current directory or any parent directory; see 'go help modules'")
644 type goModDirtyError struct{}
646 func (goModDirtyError) Error() string {
647 if cfg.BuildModExplicit {
648 return fmt.Sprintf("updates to go.mod needed, disabled by -mod=%v; to update it:\n\tgo mod tidy", cfg.BuildMod)
650 if cfg.BuildModReason != "" {
651 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)
653 return "updates to go.mod needed; to update it:\n\tgo mod tidy"
656 var errGoModDirty error = goModDirtyError{}
658 func loadWorkFile(path string) (workFile *modfile.WorkFile, modRoots []string, err error) {
659 workDir := filepath.Dir(path)
660 wf, err := ReadWorkFile(path)
664 seen := map[string]bool{}
665 for _, d := range wf.Use {
667 if !filepath.IsAbs(modRoot) {
668 modRoot = filepath.Join(workDir, modRoot)
672 return nil, nil, fmt.Errorf("path %s appears multiple times in workspace", modRoot)
675 modRoots = append(modRoots, modRoot)
678 return wf, modRoots, nil
681 // ReadWorkFile reads and parses the go.work file at the given path.
682 func ReadWorkFile(path string) (*modfile.WorkFile, error) {
683 workData, err := os.ReadFile(path)
688 f, err := modfile.ParseWork(path, workData, nil)
692 if f.Go != nil && gover.Compare(f.Go.Version, gover.Local()) > 0 && cfg.CmdName != "work edit" {
693 base.Fatal(&gover.TooNewError{What: base.ShortPath(path), GoVersion: f.Go.Version})
698 // WriteWorkFile cleans and writes out the go.work file to the given path.
699 func WriteWorkFile(path string, wf *modfile.WorkFile) error {
702 out := modfile.Format(wf.Syntax)
704 return os.WriteFile(path, out, 0666)
707 // UpdateWorkGoVersion updates the go line in wf to be at least goVers,
708 // reporting whether it changed the file.
709 func UpdateWorkGoVersion(wf *modfile.WorkFile, goVers string) (changed bool) {
710 old := gover.FromGoWork(wf)
711 if gover.Compare(old, goVers) >= 0 {
717 // We wrote a new go line. For reproducibility,
718 // if the toolchain running right now is newer than the new toolchain line,
719 // update the toolchain line to record the newer toolchain.
720 // The user never sets the toolchain explicitly in a 'go work' command,
721 // so this is only happening as a result of a go or toolchain line found
723 // If the toolchain running right now is a dev toolchain (like "go1.21")
724 // writing 'toolchain go1.21' will not be useful, since that's not an actual
725 // toolchain you can download and run. In that case fall back to at least
726 // checking that the toolchain is new enough for the Go version.
727 toolchain := "go" + old
728 if wf.Toolchain != nil {
729 toolchain = wf.Toolchain.Name
731 if gover.IsLang(gover.Local()) {
732 toolchain = gover.ToolchainMax(toolchain, "go"+goVers)
734 toolchain = gover.ToolchainMax(toolchain, "go"+gover.Local())
737 // Drop the toolchain line if it is implied by the go line
738 // or if it is asking for a toolchain older than Go 1.21,
739 // which will not understand the toolchain line.
740 if toolchain == "go"+goVers || gover.Compare(gover.FromToolchain(toolchain), gover.GoStrictVersion) < 0 {
741 wf.DropToolchainStmt()
743 wf.AddToolchainStmt(toolchain)
748 // UpdateWorkFile updates comments on directory directives in the go.work
749 // file to include the associated module path.
750 func UpdateWorkFile(wf *modfile.WorkFile) {
751 missingModulePaths := map[string]string{} // module directory listed in file -> abspath modroot
753 for _, d := range wf.Use {
755 continue // d is marked for deletion.
758 if d.ModulePath == "" {
759 missingModulePaths[d.Path] = modRoot
763 // Clean up and annotate directories.
764 // TODO(matloob): update x/mod to actually add module paths.
765 for moddir, absmodroot := range missingModulePaths {
766 _, f, err := ReadModFile(filepath.Join(absmodroot, "go.mod"), nil)
768 continue // Error will be reported if modules are loaded.
770 wf.AddUse(moddir, f.Module.Mod.Path)
774 // LoadModFile sets Target and, if there is a main module, parses the initial
775 // build list from its go.mod file.
777 // LoadModFile may make changes in memory, like adding a go directive and
778 // ensuring requirements are consistent. The caller is responsible for ensuring
779 // those changes are written to disk by calling LoadPackages or ListModules
780 // (unless ExplicitWriteGoMod is set) or by calling WriteGoMod directly.
782 // As a side-effect, LoadModFile may change cfg.BuildMod to "vendor" if
783 // -mod wasn't set explicitly and automatic vendoring should be enabled.
785 // If LoadModFile or CreateModFile has already been called, LoadModFile returns
786 // the existing in-memory requirements (rather than re-reading them from disk).
788 // LoadModFile checks the roots of the module graph for consistency with each
789 // other, but unlike LoadModGraph does not load the full module graph or check
790 // it for global consistency. Most callers outside of the modload package should
791 // use LoadModGraph instead.
792 func LoadModFile(ctx context.Context) *Requirements {
793 rs, err := loadModFile(ctx, nil)
800 func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error) {
801 if requirements != nil {
802 return requirements, nil
806 var workFile *modfile.WorkFile
807 if inWorkspaceMode() {
809 workFile, modRoots, err = loadWorkFile(workFilePath)
811 return nil, fmt.Errorf("reading go.work: %w", err)
813 for _, modRoot := range modRoots {
814 sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
815 modfetch.WorkspaceGoSumFiles = append(modfetch.WorkspaceGoSumFiles, sumFile)
817 modfetch.GoSumFile = workFilePath + ".sum"
818 } else if len(modRoots) == 0 {
819 // We're in module mode, but not inside a module.
821 // Commands like 'go build', 'go run', 'go list' have no go.mod file to
822 // read or write. They would need to find and download the latest versions
823 // of a potentially large number of modules with no way to save version
824 // information. We can succeed slowly (but not reproducibly), but that's
825 // not usually a good experience.
827 // Instead, we forbid resolving import paths to modules other than std and
828 // cmd. Users may still build packages specified with .go files on the
829 // command line, but they'll see an error if those files import anything
832 // This can be overridden by calling AllowMissingModuleImports.
833 // For example, 'go get' does this, since it is expected to resolve paths.
835 // See golang.org/issue/32027.
837 modfetch.GoSumFile = strings.TrimSuffix(modFilePath(modRoots[0]), ".mod") + ".sum"
839 if len(modRoots) == 0 {
840 // TODO(#49228): Instead of creating a fake module with an empty modroot,
841 // make MainModules.Len() == 0 mean that we're in module mode but not inside
843 mainModule := module.Version{Path: "command-line-arguments"}
844 MainModules = makeMainModules([]module.Version{mainModule}, []string{""}, []*modfile.File{nil}, []*modFileIndex{nil}, nil)
848 roots []module.Version
849 direct = map[string]bool{"go": true}
851 if inWorkspaceMode() {
852 // Since we are in a workspace, the Go version for the synthetic
853 // "command-line-arguments" module must not exceed the Go version
854 // for the workspace.
855 goVersion = MainModules.GoVersion()
857 roots = []module.Version{
859 {Path: "go", Version: goVersion},
860 {Path: "toolchain", Version: gover.LocalToolchain()},
863 goVersion = gover.Local()
864 pruning = pruningForGoVersion(goVersion)
865 roots = []module.Version{
866 {Path: "go", Version: goVersion},
867 {Path: "toolchain", Version: gover.LocalToolchain()},
870 rawGoVersion.Store(mainModule, goVersion)
871 requirements = newRequirements(pruning, roots, direct)
872 if cfg.BuildMod == "vendor" {
873 // For issue 56536: Some users may have GOFLAGS=-mod=vendor set.
874 // Make sure it behaves as though the fake module is vendored
875 // with no dependencies.
876 requirements.initVendor(nil)
878 return requirements, nil
881 var modFiles []*modfile.File
882 var mainModules []module.Version
883 var indices []*modFileIndex
885 for _, modroot := range modRoots {
886 gomod := modFilePath(modroot)
888 data, f, err := ReadModFile(gomod, fixVersion(ctx, &fixed))
890 if inWorkspaceMode() {
891 if tooNew, ok := err.(*gover.TooNewError); ok && !strings.HasPrefix(cfg.CmdName, "work ") {
892 // Switching to a newer toolchain won't help - the go.work has the wrong version.
893 // Report this more specific error, unless we are a command like 'go work use'
894 // or 'go work sync', which will fix the problem after the caller sees the TooNewError
895 // and switches to a newer toolchain.
896 err = errWorkTooOld(gomod, workFile, tooNew.GoVersion)
898 err = fmt.Errorf("cannot load module %s listed in go.work file: %w",
899 base.ShortPath(filepath.Dir(gomod)), err)
902 errs = append(errs, err)
905 if inWorkspaceMode() && !strings.HasPrefix(cfg.CmdName, "work ") {
906 // Refuse to use workspace if its go version is too old.
907 // Disable this check if we are a workspace command like work use or work sync,
908 // which will fix the problem.
909 mv := gover.FromGoMod(f)
910 wv := gover.FromGoWork(workFile)
911 if gover.Compare(mv, wv) > 0 && gover.Compare(mv, gover.GoStrictVersion) >= 0 {
912 errs = append(errs, errWorkTooOld(gomod, workFile, mv))
917 modFiles = append(modFiles, f)
918 mainModule := f.Module.Mod
919 mainModules = append(mainModules, mainModule)
920 indices = append(indices, indexModFile(data, f, mainModule, fixed))
922 if err := module.CheckImportPath(f.Module.Mod.Path); err != nil {
923 if pathErr, ok := err.(*module.InvalidPathError); ok {
924 pathErr.Kind = "module"
926 errs = append(errs, err)
930 return nil, errors.Join(errs...)
933 MainModules = makeMainModules(mainModules, modRoots, modFiles, indices, workFile)
934 setDefaultBuildMod() // possibly enable automatic vendoring
935 rs := requirementsFromModFiles(ctx, workFile, modFiles, opts)
937 if cfg.BuildMod == "vendor" {
938 readVendorList(VendorDir())
939 var indexes []*modFileIndex
940 var modFiles []*modfile.File
941 var modRoots []string
942 for _, m := range MainModules.Versions() {
943 indexes = append(indexes, MainModules.Index(m))
944 modFiles = append(modFiles, MainModules.ModFile(m))
945 modRoots = append(modRoots, MainModules.ModRoot(m))
947 checkVendorConsistency(indexes, modFiles, modRoots)
948 rs.initVendor(vendorList)
951 if inWorkspaceMode() {
952 // We don't need to update the mod file so return early.
957 mainModule := MainModules.mustGetSingleMainModule()
959 if rs.hasRedundantRoot() {
960 // If any module path appears more than once in the roots, we know that the
961 // go.mod file needs to be updated even though we have not yet loaded any
962 // transitive dependencies.
964 rs, err = updateRoots(ctx, rs.direct, rs, nil, nil, false)
970 if MainModules.Index(mainModule).goVersion == "" && rs.pruning != workspace {
971 // TODO(#45551): Do something more principled instead of checking
972 // cfg.CmdName directly here.
973 if cfg.BuildMod == "mod" && cfg.CmdName != "mod graph" && cfg.CmdName != "mod why" {
974 // go line is missing from go.mod; add one there and add to derived requirements.
976 if opts != nil && opts.TidyGoVersion != "" {
977 v = opts.TidyGoVersion
979 addGoStmt(MainModules.ModFile(mainModule), mainModule, v)
980 rs = overrideRoots(ctx, rs, []module.Version{{Path: "go", Version: v}})
982 // We need to add a 'go' version to the go.mod file, but we must assume
983 // that its existing contents match something between Go 1.11 and 1.16.
984 // Go 1.11 through 1.16 do not support graph pruning, but the latest Go
985 // version uses a pruned module graph — so we need to convert the
986 // requirements to support pruning.
987 if gover.Compare(v, gover.ExplicitIndirectVersion) >= 0 {
989 rs, err = convertPruning(ctx, rs, pruned)
995 rawGoVersion.Store(mainModule, gover.DefaultGoModVersion)
1000 return requirements, nil
1003 func errWorkTooOld(gomod string, wf *modfile.WorkFile, goVers string) error {
1004 return fmt.Errorf("module %s listed in go.work file requires go >= %s, but go.work lists go %s; to update it:\n\tgo work use",
1005 base.ShortPath(filepath.Dir(gomod)), goVers, gover.FromGoWork(wf))
1008 // CreateModFile initializes a new module by creating a go.mod file.
1010 // If modPath is empty, CreateModFile will attempt to infer the path from the
1011 // directory location within GOPATH.
1013 // If a vendoring configuration file is present, CreateModFile will attempt to
1014 // translate it to go.mod directives. The resulting build list may not be
1015 // exactly the same as in the legacy configuration (for example, we can't get
1016 // packages at multiple versions from the same module).
1017 func CreateModFile(ctx context.Context, modPath string) {
1018 modRoot := base.Cwd()
1019 modRoots = []string{modRoot}
1021 modFilePath := modFilePath(modRoot)
1022 if _, err := fsys.Stat(modFilePath); err == nil {
1023 base.Fatalf("go: %s already exists", modFilePath)
1028 modPath, err = findModulePath(modRoot)
1032 } else if err := module.CheckImportPath(modPath); err != nil {
1033 if pathErr, ok := err.(*module.InvalidPathError); ok {
1034 pathErr.Kind = "module"
1035 // Same as build.IsLocalPath()
1036 if pathErr.Path == "." || pathErr.Path == ".." ||
1037 strings.HasPrefix(pathErr.Path, "./") || strings.HasPrefix(pathErr.Path, "../") {
1038 pathErr.Err = errors.New("is a local import path")
1042 } else if _, _, ok := module.SplitPathVersion(modPath); !ok {
1043 if strings.HasPrefix(modPath, "gopkg.in/") {
1044 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))
1045 base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg)
1047 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))
1048 base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg)
1051 fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", modPath)
1052 modFile := new(modfile.File)
1053 modFile.AddModuleStmt(modPath)
1054 MainModules = makeMainModules([]module.Version{modFile.Module.Mod}, []string{modRoot}, []*modfile.File{modFile}, []*modFileIndex{nil}, nil)
1055 addGoStmt(modFile, modFile.Module.Mod, gover.Local()) // Add the go directive before converted module requirements.
1057 rs := requirementsFromModFiles(ctx, nil, []*modfile.File{modFile}, nil)
1058 rs, err := updateRoots(ctx, rs.direct, rs, nil, nil, false)
1063 if err := commitRequirements(ctx, WriteOpts{}); err != nil {
1067 // Suggest running 'go mod tidy' unless the project is empty. Even if we
1068 // imported all the correct requirements above, we're probably missing
1069 // some sums, so the next build command in -mod=readonly will likely fail.
1071 // We look for non-hidden .go files or subdirectories to determine whether
1072 // this is an existing project. Walking the tree for packages would be more
1073 // accurate, but could take much longer.
1075 files, _ := os.ReadDir(modRoot)
1076 for _, f := range files {
1078 if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
1081 if strings.HasSuffix(name, ".go") || f.IsDir() {
1087 fmt.Fprintf(os.Stderr, "go: to add module requirements and sums:\n\tgo mod tidy\n")
1091 // fixVersion returns a modfile.VersionFixer implemented using the Query function.
1093 // It resolves commit hashes and branch names to versions,
1094 // canonicalizes versions that appeared in early vgo drafts,
1095 // and does nothing for versions that already appear to be canonical.
1097 // The VersionFixer sets 'fixed' if it ever returns a non-canonical version.
1098 func fixVersion(ctx context.Context, fixed *bool) modfile.VersionFixer {
1099 return func(path, vers string) (resolved string, err error) {
1101 if err == nil && resolved != vers {
1106 // Special case: remove the old -gopkgin- hack.
1107 if strings.HasPrefix(path, "gopkg.in/") && strings.Contains(vers, "-gopkgin-") {
1108 vers = vers[strings.Index(vers, "-gopkgin-")+len("-gopkgin-"):]
1111 // fixVersion is called speculatively on every
1112 // module, version pair from every go.mod file.
1113 // Avoid the query if it looks OK.
1114 _, pathMajor, ok := module.SplitPathVersion(path)
1116 return "", &module.ModuleError{
1118 Err: &module.InvalidVersionError{
1120 Err: fmt.Errorf("malformed module path %q", path),
1124 if vers != "" && module.CanonicalVersion(vers) == vers {
1125 if err := module.CheckPathMajor(vers, pathMajor); err != nil {
1126 return "", module.VersionError(module.Version{Path: path, Version: vers}, err)
1131 info, err := Query(ctx, path, vers, "", nil)
1135 return info.Version, nil
1139 // AllowMissingModuleImports allows import paths to be resolved to modules
1140 // when there is no module root. Normally, this is forbidden because it's slow
1141 // and there's no way to make the result reproducible, but some commands
1142 // like 'go get' are expected to do this.
1144 // This function affects the default cfg.BuildMod when outside of a module,
1145 // so it can only be called prior to Init.
1146 func AllowMissingModuleImports() {
1148 panic("AllowMissingModuleImports after Init")
1150 allowMissingModuleImports = true
1153 // makeMainModules creates a MainModuleSet and associated variables according to
1154 // the given main modules.
1155 func makeMainModules(ms []module.Version, rootDirs []string, modFiles []*modfile.File, indices []*modFileIndex, workFile *modfile.WorkFile) *MainModuleSet {
1156 for _, m := range ms {
1157 if m.Version != "" {
1158 panic("mainModulesCalled with module.Version with non empty Version field: " + fmt.Sprintf("%#v", m))
1161 modRootContainingCWD := findModuleRoot(base.Cwd())
1162 mainModules := &MainModuleSet{
1163 versions: slices.Clip(ms),
1164 inGorootSrc: map[module.Version]bool{},
1165 pathPrefix: map[module.Version]string{},
1166 modRoot: map[module.Version]string{},
1167 modFiles: map[module.Version]*modfile.File{},
1168 indices: map[module.Version]*modFileIndex{},
1169 highestReplaced: map[string]string{},
1172 var workFileReplaces []*modfile.Replace
1173 if workFile != nil {
1174 workFileReplaces = workFile.Replace
1175 mainModules.workFileReplaceMap = toReplaceMap(workFile.Replace)
1177 mainModulePaths := make(map[string]bool)
1178 for _, m := range ms {
1179 if mainModulePaths[m.Path] {
1180 base.Errorf("go: module %s appears multiple times in workspace", m.Path)
1182 mainModulePaths[m.Path] = true
1184 replacedByWorkFile := make(map[string]bool)
1185 replacements := make(map[module.Version]module.Version)
1186 for _, r := range workFileReplaces {
1187 if mainModulePaths[r.Old.Path] && r.Old.Version == "" {
1188 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)
1190 replacedByWorkFile[r.Old.Path] = true
1191 v, ok := mainModules.highestReplaced[r.Old.Path]
1192 if !ok || gover.ModCompare(r.Old.Path, r.Old.Version, v) > 0 {
1193 mainModules.highestReplaced[r.Old.Path] = r.Old.Version
1195 replacements[r.Old] = r.New
1197 for i, m := range ms {
1198 mainModules.pathPrefix[m] = m.Path
1199 mainModules.modRoot[m] = rootDirs[i]
1200 mainModules.modFiles[m] = modFiles[i]
1201 mainModules.indices[m] = indices[i]
1203 if mainModules.modRoot[m] == modRootContainingCWD {
1204 mainModules.modContainingCWD = m
1207 if rel := search.InDir(rootDirs[i], cfg.GOROOTsrc); rel != "" {
1208 mainModules.inGorootSrc[m] = true
1209 if m.Path == "std" {
1210 // The "std" module in GOROOT/src is the Go standard library. Unlike other
1211 // modules, the packages in the "std" module have no import-path prefix.
1213 // Modules named "std" outside of GOROOT/src do not receive this special
1214 // treatment, so it is possible to run 'go test .' in other GOROOTs to
1215 // test individual packages using a combination of the modified package
1216 // and the ordinary standard library.
1217 // (See https://golang.org/issue/30756.)
1218 mainModules.pathPrefix[m] = ""
1222 if modFiles[i] != nil {
1223 curModuleReplaces := make(map[module.Version]bool)
1224 for _, r := range modFiles[i].Replace {
1225 if replacedByWorkFile[r.Old.Path] {
1228 var newV module.Version = r.New
1229 if WorkFilePath() != "" && newV.Version == "" && !filepath.IsAbs(newV.Path) {
1230 // Since we are in a workspace, we may be loading replacements from
1231 // multiple go.mod files. Relative paths in those replacement are
1232 // relative to the go.mod file, not the workspace, so the same string
1233 // may refer to two different paths and different strings may refer to
1234 // the same path. Convert them all to be absolute instead.
1236 // (We could do this outside of a workspace too, but it would mean that
1237 // replacement paths in error strings needlessly differ from what's in
1238 // the go.mod file.)
1239 newV.Path = filepath.Join(rootDirs[i], newV.Path)
1241 if prev, ok := replacements[r.Old]; ok && !curModuleReplaces[r.Old] && prev != newV {
1242 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)
1244 curModuleReplaces[r.Old] = true
1245 replacements[r.Old] = newV
1247 v, ok := mainModules.highestReplaced[r.Old.Path]
1248 if !ok || gover.ModCompare(r.Old.Path, r.Old.Version, v) > 0 {
1249 mainModules.highestReplaced[r.Old.Path] = r.Old.Version
1257 // requirementsFromModFiles returns the set of non-excluded requirements from
1258 // the global modFile.
1259 func requirementsFromModFiles(ctx context.Context, workFile *modfile.WorkFile, modFiles []*modfile.File, opts *PackageOpts) *Requirements {
1260 var roots []module.Version
1261 direct := map[string]bool{}
1262 var pruning modPruning
1263 if inWorkspaceMode() {
1265 roots = make([]module.Version, len(MainModules.Versions()), 2+len(MainModules.Versions()))
1266 copy(roots, MainModules.Versions())
1267 goVersion := gover.FromGoWork(workFile)
1268 var toolchain string
1269 if workFile.Toolchain != nil {
1270 toolchain = workFile.Toolchain.Name
1272 roots = appendGoAndToolchainRoots(roots, goVersion, toolchain, direct)
1274 pruning = pruningForGoVersion(MainModules.GoVersion())
1275 if len(modFiles) != 1 {
1276 panic(fmt.Errorf("requirementsFromModFiles called with %v modfiles outside workspace mode", len(modFiles)))
1278 modFile := modFiles[0]
1279 roots, direct = rootsFromModFile(MainModules.mustGetSingleMainModule(), modFile, withToolchainRoot)
1282 gover.ModSort(roots)
1283 rs := newRequirements(pruning, roots, direct)
1287 type addToolchainRoot bool
1290 omitToolchainRoot addToolchainRoot = false
1291 withToolchainRoot = true
1294 func rootsFromModFile(m module.Version, modFile *modfile.File, addToolchainRoot addToolchainRoot) (roots []module.Version, direct map[string]bool) {
1295 direct = make(map[string]bool)
1296 padding := 2 // Add padding for the toolchain and go version, added upon return.
1297 if !addToolchainRoot {
1300 roots = make([]module.Version, 0, padding+len(modFile.Require))
1301 for _, r := range modFile.Require {
1302 if index := MainModules.Index(m); index != nil && index.exclude[r.Mod] {
1303 if cfg.BuildMod == "mod" {
1304 fmt.Fprintf(os.Stderr, "go: dropping requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
1306 fmt.Fprintf(os.Stderr, "go: ignoring requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
1311 roots = append(roots, r.Mod)
1313 direct[r.Mod.Path] = true
1316 goVersion := gover.FromGoMod(modFile)
1317 var toolchain string
1318 if addToolchainRoot && modFile.Toolchain != nil {
1319 toolchain = modFile.Toolchain.Name
1321 roots = appendGoAndToolchainRoots(roots, goVersion, toolchain, direct)
1322 return roots, direct
1325 func appendGoAndToolchainRoots(roots []module.Version, goVersion, toolchain string, direct map[string]bool) []module.Version {
1326 // Add explicit go and toolchain versions, inferring as needed.
1327 roots = append(roots, module.Version{Path: "go", Version: goVersion})
1328 direct["go"] = true // Every module directly uses the language and runtime.
1330 if toolchain != "" {
1331 roots = append(roots, module.Version{Path: "toolchain", Version: toolchain})
1332 // Leave the toolchain as indirect: nothing in the user's module directly
1333 // imports a package from the toolchain, and (like an indirect dependency in
1334 // a module without graph pruning) we may remove the toolchain line
1335 // automatically if the 'go' version is changed so that it implies the exact
1341 // setDefaultBuildMod sets a default value for cfg.BuildMod if the -mod flag
1342 // wasn't provided. setDefaultBuildMod may be called multiple times.
1343 func setDefaultBuildMod() {
1344 if cfg.BuildModExplicit {
1345 if inWorkspaceMode() && cfg.BuildMod != "readonly" && cfg.BuildMod != "vendor" {
1346 base.Fatalf("go: -mod may only be set to readonly or vendor when in workspace mode, but it is set to %q"+
1347 "\n\tRemove the -mod flag to use the default readonly value, "+
1348 "\n\tor set GOWORK=off to disable workspace mode.", cfg.BuildMod)
1350 // Don't override an explicit '-mod=' argument.
1354 // TODO(#40775): commands should pass in the module mode as an option
1355 // to modload functions instead of relying on an implicit setting
1356 // based on command name.
1357 switch cfg.CmdName {
1358 case "get", "mod download", "mod init", "mod tidy", "work sync":
1359 // These commands are intended to update go.mod and go.sum.
1360 cfg.BuildMod = "mod"
1362 case "mod graph", "mod verify", "mod why":
1363 // These commands should not update go.mod or go.sum, but they should be
1364 // able to fetch modules not in go.sum and should not report errors if
1365 // go.mod is inconsistent. They're useful for debugging, and they need
1366 // to work in buggy situations.
1367 cfg.BuildMod = "mod"
1369 case "mod vendor", "work vendor":
1370 cfg.BuildMod = "readonly"
1373 if modRoots == nil {
1374 if allowMissingModuleImports {
1375 cfg.BuildMod = "mod"
1377 cfg.BuildMod = "readonly"
1382 if len(modRoots) >= 1 {
1383 var goVersion string
1384 var versionSource string
1385 if inWorkspaceMode() {
1386 versionSource = "go.work"
1387 if wfg := MainModules.WorkFile().Go; wfg != nil {
1388 goVersion = wfg.Version
1391 versionSource = "go.mod"
1392 index := MainModules.GetSingleIndexOrNil()
1394 goVersion = index.goVersion
1398 if workFilePath != "" {
1399 vendorDir = filepath.Join(filepath.Dir(workFilePath), "vendor")
1401 if len(modRoots) != 1 {
1402 panic(fmt.Errorf("outside workspace mode, but have %v modRoots", modRoots))
1404 vendorDir = filepath.Join(modRoots[0], "vendor")
1406 if fi, err := fsys.Stat(vendorDir); err == nil && fi.IsDir() {
1407 modGo := "unspecified"
1408 if goVersion != "" {
1409 if gover.Compare(goVersion, "1.14") < 0 {
1410 // The go version is less than 1.14. Don't set -mod=vendor by default.
1411 // Since a vendor directory exists, we should record why we didn't use it.
1412 // This message won't normally be shown, but it may appear with import errors.
1413 cfg.BuildModReason = fmt.Sprintf("Go version in "+versionSource+" is %s, so vendor directory was not used.", modGo)
1415 vendoredWorkspace, err := modulesTextIsForWorkspace(vendorDir)
1417 base.Fatalf("go: reading modules.txt for vendor directory: %v", err)
1419 if vendoredWorkspace != (versionSource == "go.work") {
1420 if vendoredWorkspace {
1421 cfg.BuildModReason = "Outside workspace mode, but vendor directory is for a workspace."
1423 cfg.BuildModReason = "In workspace mode, but vendor directory is not for a workspace"
1426 // The Go version is at least 1.14, a vendor directory exists, and
1427 // the modules.txt was generated in the same mode the command is running in.
1428 // Set -mod=vendor by default.
1429 cfg.BuildMod = "vendor"
1430 cfg.BuildModReason = "Go version in " + versionSource + " is at least 1.14 and vendor directory exists."
1440 cfg.BuildMod = "readonly"
1443 func modulesTextIsForWorkspace(vendorDir string) (bool, error) {
1444 f, err := fsys.Open(filepath.Join(vendorDir, "modules.txt"))
1445 if errors.Is(err, os.ErrNotExist) {
1446 // Some vendor directories exist that don't contain modules.txt.
1447 // This mostly happens when converting to modules.
1448 // We want to preserve the behavior that mod=vendor is set (even though
1449 // readVendorList does nothing in that case).
1456 n, err := f.Read(buf[:])
1457 if err != nil && err != io.EOF {
1460 line, _, _ := strings.Cut(string(buf[:n]), "\n")
1461 if annotations, ok := strings.CutPrefix(line, "## "); ok {
1462 for _, entry := range strings.Split(annotations, ";") {
1463 entry = strings.TrimSpace(entry)
1464 if entry == "workspace" {
1472 func mustHaveCompleteRequirements() bool {
1473 return cfg.BuildMod != "mod" && !inWorkspaceMode()
1476 // addGoStmt adds a go directive to the go.mod file if it does not already
1477 // include one. The 'go' version added, if any, is the latest version supported
1478 // by this toolchain.
1479 func addGoStmt(modFile *modfile.File, mod module.Version, v string) {
1480 if modFile.Go != nil && modFile.Go.Version != "" {
1483 forceGoStmt(modFile, mod, v)
1486 func forceGoStmt(modFile *modfile.File, mod module.Version, v string) {
1487 if err := modFile.AddGoStmt(v); err != nil {
1488 base.Fatalf("go: internal error: %v", err)
1490 rawGoVersion.Store(mod, v)
1493 var altConfigs = []string{
1497 func findModuleRoot(dir string) (roots string) {
1499 panic("dir not set")
1501 dir = filepath.Clean(dir)
1503 // Look for enclosing go.mod.
1505 if fi, err := fsys.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {
1508 d := filepath.Dir(dir)
1517 func findWorkspaceFile(dir string) (root string) {
1519 panic("dir not set")
1521 dir = filepath.Clean(dir)
1523 // Look for enclosing go.mod.
1525 f := filepath.Join(dir, "go.work")
1526 if fi, err := fsys.Stat(f); err == nil && !fi.IsDir() {
1529 d := filepath.Dir(dir)
1533 if d == cfg.GOROOT {
1534 // As a special case, don't cross GOROOT to find a go.work file.
1535 // The standard library and commands built in go always use the vendored
1536 // dependencies, so avoid using a most likely irrelevant go.work file.
1544 func findAltConfig(dir string) (root, name string) {
1546 panic("dir not set")
1548 dir = filepath.Clean(dir)
1549 if rel := search.InDir(dir, cfg.BuildContext.GOROOT); rel != "" {
1550 // Don't suggest creating a module from $GOROOT/.git/config
1551 // or a config file found in any parent of $GOROOT (see #34191).
1555 for _, name := range altConfigs {
1556 if fi, err := fsys.Stat(filepath.Join(dir, name)); err == nil && !fi.IsDir() {
1560 d := filepath.Dir(dir)
1569 func findModulePath(dir string) (string, error) {
1570 // TODO(bcmills): once we have located a plausible module path, we should
1571 // query version control (if available) to verify that it matches the major
1572 // version of the most recent tag.
1573 // See https://golang.org/issue/29433, https://golang.org/issue/27009, and
1574 // https://golang.org/issue/31549.
1576 // Cast about for import comments,
1577 // first in top-level directory, then in subdirectories.
1578 list, _ := os.ReadDir(dir)
1579 for _, info := range list {
1580 if info.Type().IsRegular() && strings.HasSuffix(info.Name(), ".go") {
1581 if com := findImportComment(filepath.Join(dir, info.Name())); com != "" {
1586 for _, info1 := range list {
1588 files, _ := os.ReadDir(filepath.Join(dir, info1.Name()))
1589 for _, info2 := range files {
1590 if info2.Type().IsRegular() && strings.HasSuffix(info2.Name(), ".go") {
1591 if com := findImportComment(filepath.Join(dir, info1.Name(), info2.Name())); com != "" {
1592 return path.Dir(com), nil
1599 // Look for Godeps.json declaring import path.
1600 data, _ := os.ReadFile(filepath.Join(dir, "Godeps/Godeps.json"))
1601 var cfg1 struct{ ImportPath string }
1602 json.Unmarshal(data, &cfg1)
1603 if cfg1.ImportPath != "" {
1604 return cfg1.ImportPath, nil
1607 // Look for vendor.json declaring import path.
1608 data, _ = os.ReadFile(filepath.Join(dir, "vendor/vendor.json"))
1609 var cfg2 struct{ RootPath string }
1610 json.Unmarshal(data, &cfg2)
1611 if cfg2.RootPath != "" {
1612 return cfg2.RootPath, nil
1615 // Look for path in GOPATH.
1616 var badPathErr error
1617 for _, gpdir := range filepath.SplitList(cfg.BuildContext.GOPATH) {
1621 if rel := search.InDir(dir, filepath.Join(gpdir, "src")); rel != "" && rel != "." {
1622 path := filepath.ToSlash(rel)
1623 // gorelease will alert users publishing their modules to fix their paths.
1624 if err := module.CheckImportPath(path); err != nil {
1632 reason := "outside GOPATH, module path must be specified"
1633 if badPathErr != nil {
1634 // return a different error message if the module was in GOPATH, but
1635 // the module path determined above would be an invalid path.
1636 reason = fmt.Sprintf("bad module path inferred from directory in GOPATH: %v", badPathErr)
1638 msg := `cannot determine module path for source directory %s (%s)
1641 'go mod init example.com/m' to initialize a v0 or v1 module
1642 'go mod init example.com/m/v2' to initialize a v2 module
1644 Run 'go help mod init' for more information.
1646 return "", fmt.Errorf(msg, dir, reason)
1650 importCommentRE = lazyregexp.New(`(?m)^package[ \t]+[^ \t\r\n/]+[ \t]+//[ \t]+import[ \t]+(\"[^"]+\")[ \t]*\r?\n`)
1653 func findImportComment(file string) string {
1654 data, err := os.ReadFile(file)
1658 m := importCommentRE.FindSubmatch(data)
1662 path, err := strconv.Unquote(string(m[1]))
1669 // WriteOpts control the behavior of WriteGoMod.
1670 type WriteOpts struct {
1671 DropToolchain bool // go get toolchain@none
1672 ExplicitToolchain bool // go get has set explicit toolchain version
1674 // TODO(bcmills): Make 'go mod tidy' update the go version in the Requirements
1675 // instead of writing directly to the modfile.File
1676 TidyWroteGo bool // Go.Version field already updated by 'go mod tidy'
1679 // WriteGoMod writes the current build list back to go.mod.
1680 func WriteGoMod(ctx context.Context, opts WriteOpts) error {
1681 requirements = LoadModFile(ctx)
1682 return commitRequirements(ctx, opts)
1685 // commitRequirements ensures go.mod and go.sum are up to date with the current
1688 // In "mod" mode, commitRequirements writes changes to go.mod and go.sum.
1690 // In "readonly" and "vendor" modes, commitRequirements returns an error if
1691 // go.mod or go.sum are out of date in a semantically significant way.
1693 // In workspace mode, commitRequirements only writes changes to go.work.sum.
1694 func commitRequirements(ctx context.Context, opts WriteOpts) (err error) {
1695 if inWorkspaceMode() {
1696 // go.mod files aren't updated in workspace mode, but we still want to
1697 // update the go.work.sum file.
1698 return modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, requirements, addBuildListZipSums), mustHaveCompleteRequirements())
1700 if MainModules.Len() != 1 || MainModules.ModRoot(MainModules.Versions()[0]) == "" {
1701 // We aren't in a module, so we don't have anywhere to write a go.mod file.
1704 mainModule := MainModules.mustGetSingleMainModule()
1705 modFile := MainModules.ModFile(mainModule)
1707 // command-line-arguments has no .mod file to write.
1710 modFilePath := modFilePath(MainModules.ModRoot(mainModule))
1712 var list []*modfile.Require
1715 for _, m := range requirements.rootModules {
1717 goVersion = m.Version
1720 if m.Path == "toolchain" {
1721 toolchain = m.Version
1724 list = append(list, &modfile.Require{
1726 Indirect: !requirements.direct[m.Path],
1731 // Every MVS graph we consider should have go as a root,
1732 // and toolchain is either implied by the go line or explicitly a root.
1733 if goVersion == "" {
1734 base.Fatalf("go: internal error: missing go root module in WriteGoMod")
1736 if gover.Compare(goVersion, gover.Local()) > 0 {
1737 // We cannot assume that we know how to update a go.mod to a newer version.
1738 return &gover.TooNewError{What: "updating go.mod", GoVersion: goVersion}
1740 wroteGo := opts.TidyWroteGo
1741 if !wroteGo && modFile.Go == nil || modFile.Go.Version != goVersion {
1742 alwaysUpdate := cfg.BuildMod == "mod" || cfg.CmdName == "mod tidy" || cfg.CmdName == "get"
1743 if modFile.Go == nil && goVersion == gover.DefaultGoModVersion && !alwaysUpdate {
1744 // The go.mod has no go line, the implied default Go version matches
1745 // what we've computed for the graph, and we're not in one of the
1746 // traditional go.mod-updating programs, so leave it alone.
1749 forceGoStmt(modFile, mainModule, goVersion)
1752 if toolchain == "" {
1753 toolchain = "go" + goVersion
1756 // For reproducibility, if we are writing a new go line,
1757 // and we're not explicitly modifying the toolchain line with 'go get toolchain@something',
1758 // and the go version is one that supports switching toolchains,
1759 // and the toolchain running right now is newer than the current toolchain line,
1760 // then update the toolchain line to record the newer toolchain.
1762 // TODO(#57001): This condition feels too complicated. Can we simplify it?
1763 // TODO(#57001): Add more tests for toolchain lines.
1764 toolVers := gover.FromToolchain(toolchain)
1765 if wroteGo && !opts.DropToolchain && !opts.ExplicitToolchain &&
1766 gover.Compare(goVersion, gover.GoStrictVersion) >= 0 &&
1767 (gover.Compare(gover.Local(), toolVers) > 0 && !gover.IsLang(gover.Local())) {
1768 toolchain = "go" + gover.Local()
1769 toolVers = gover.FromToolchain(toolchain)
1772 if opts.DropToolchain || toolchain == "go"+goVersion || (gover.Compare(toolVers, gover.GoStrictVersion) < 0 && !opts.ExplicitToolchain) {
1773 // go get toolchain@none or toolchain matches go line or isn't valid; drop it.
1774 // TODO(#57001): 'go get' should reject explicit toolchains below GoStrictVersion.
1775 modFile.DropToolchainStmt()
1777 modFile.AddToolchainStmt(toolchain)
1780 // Update require blocks.
1781 if gover.Compare(goVersion, gover.SeparateIndirectVersion) < 0 {
1782 modFile.SetRequire(list)
1784 modFile.SetRequireSeparateIndirect(list)
1788 index := MainModules.GetSingleIndexOrNil()
1789 dirty := index.modFileIsDirty(modFile)
1790 if dirty && cfg.BuildMod != "mod" {
1791 // If we're about to fail due to -mod=readonly,
1792 // prefer to report a dirty go.mod over a dirty go.sum
1793 return errGoModDirty
1796 if !dirty && cfg.CmdName != "mod tidy" {
1797 // The go.mod file has the same semantic content that it had before
1798 // (but not necessarily the same exact bytes).
1799 // Don't write go.mod, but write go.sum in case we added or trimmed sums.
1800 // 'go mod init' shouldn't write go.sum, since it will be incomplete.
1801 if cfg.CmdName != "mod init" {
1802 if err := modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, requirements, addBuildListZipSums), mustHaveCompleteRequirements()); err != nil {
1808 if _, ok := fsys.OverlayPath(modFilePath); ok {
1810 return errors.New("updates to go.mod needed, but go.mod is part of the overlay specified with -overlay")
1815 new, err := modFile.Format()
1820 // At this point we have determined to make the go.mod file on disk equal to new.
1821 MainModules.SetIndex(mainModule, indexModFile(new, modFile, mainModule, false))
1823 // Update go.sum after releasing the side lock and refreshing the index.
1824 // 'go mod init' shouldn't write go.sum, since it will be incomplete.
1825 if cfg.CmdName != "mod init" {
1827 err = modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, requirements, addBuildListZipSums), mustHaveCompleteRequirements())
1832 // Make a best-effort attempt to acquire the side lock, only to exclude
1833 // previous versions of the 'go' command from making simultaneous edits.
1834 if unlock, err := modfetch.SideLock(ctx); err == nil {
1838 errNoChange := errors.New("no update needed")
1840 err = lockedfile.Transform(modFilePath, func(old []byte) ([]byte, error) {
1841 if bytes.Equal(old, new) {
1842 // The go.mod file is already equal to new, possibly as the result of some
1844 return nil, errNoChange
1847 if index != nil && !bytes.Equal(old, index.data) {
1848 // The contents of the go.mod file have changed. In theory we could add all
1849 // of the new modules to the build list, recompute, and check whether any
1850 // module in *our* build list got bumped to a different version, but that's
1851 // a lot of work for marginal benefit. Instead, fail the command: if users
1852 // want to run concurrent commands, they need to start with a complete,
1853 // consistent module definition.
1854 return nil, fmt.Errorf("existing contents have changed since last read")
1860 if err != nil && err != errNoChange {
1861 return fmt.Errorf("updating go.mod: %w", err)
1866 // keepSums returns the set of modules (and go.mod file entries) for which
1867 // checksums would be needed in order to reload the same set of packages
1868 // loaded by the most recent call to LoadPackages or ImportFromFiles,
1869 // including any go.mod files needed to reconstruct the MVS result
1870 // or identify go versions,
1871 // in addition to the checksums for every module in keepMods.
1872 func keepSums(ctx context.Context, ld *loader, rs *Requirements, which whichSums) map[module.Version]bool {
1873 // Every module in the full module graph contributes its requirements,
1874 // so in order to ensure that the build list itself is reproducible,
1875 // we need sums for every go.mod in the graph (regardless of whether
1876 // that version is selected).
1877 keep := make(map[module.Version]bool)
1879 // Add entries for modules in the build list with paths that are prefixes of
1880 // paths of loaded packages. We need to retain sums for all of these modules —
1881 // not just the modules containing the actual packages — in order to rule out
1882 // ambiguous import errors the next time we load the package.
1883 keepModSumsForZipSums := true
1885 if gover.Compare(MainModules.GoVersion(), gover.TidyGoModSumVersion) < 0 && cfg.BuildMod != "mod" {
1886 keepModSumsForZipSums = false
1889 keepPkgGoModSums := true
1890 if gover.Compare(ld.requirements.GoVersion(), gover.TidyGoModSumVersion) < 0 && (ld.Tidy || cfg.BuildMod != "mod") {
1891 keepPkgGoModSums = false
1892 keepModSumsForZipSums = false
1894 for _, pkg := range ld.pkgs {
1895 // We check pkg.mod.Path here instead of pkg.inStd because the
1896 // pseudo-package "C" is not in std, but not provided by any module (and
1897 // shouldn't force loading the whole module graph).
1898 if pkg.testOf != nil || (pkg.mod.Path == "" && pkg.err == nil) || module.CheckImportPath(pkg.path) != nil {
1902 // We need the checksum for the go.mod file for pkg.mod
1903 // so that we know what Go version to use to compile pkg.
1904 // However, we didn't do so before Go 1.21, and the bug is relatively
1905 // minor, so we maintain the previous (buggy) behavior in 'go mod tidy' to
1906 // avoid introducing unnecessary churn.
1907 if keepPkgGoModSums {
1908 r := resolveReplacement(pkg.mod)
1909 keep[modkey(r)] = true
1912 if rs.pruning == pruned && pkg.mod.Path != "" {
1913 if v, ok := rs.rootSelected(pkg.mod.Path); ok && v == pkg.mod.Version {
1914 // pkg was loaded from a root module, and because the main module has
1915 // a pruned module graph we do not check non-root modules for
1916 // conflicts for packages that can be found in roots. So we only need
1917 // the checksums for the root modules that may contain pkg, not all
1918 // possible modules.
1919 for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) {
1920 if v, ok := rs.rootSelected(prefix); ok && v != "none" {
1921 m := module.Version{Path: prefix, Version: v}
1922 r := resolveReplacement(m)
1930 mg, _ := rs.Graph(ctx)
1931 for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) {
1932 if v := mg.Selected(prefix); v != "none" {
1933 m := module.Version{Path: prefix, Version: v}
1934 r := resolveReplacement(m)
1941 if rs.graph.Load() == nil {
1942 // We haven't needed to load the module graph so far.
1943 // Save sums for the root modules (or their replacements), but don't
1944 // incur the cost of loading the graph just to find and retain the sums.
1945 for _, m := range rs.rootModules {
1946 r := resolveReplacement(m)
1947 keep[modkey(r)] = true
1948 if which == addBuildListZipSums {
1953 mg, _ := rs.Graph(ctx)
1954 mg.WalkBreadthFirst(func(m module.Version) {
1955 if _, ok := mg.RequiredBy(m); ok {
1956 // The requirements from m's go.mod file are present in the module graph,
1957 // so they are relevant to the MVS result regardless of whether m was
1958 // actually selected.
1959 r := resolveReplacement(m)
1960 keep[modkey(r)] = true
1964 if which == addBuildListZipSums {
1965 for _, m := range mg.BuildList() {
1966 r := resolveReplacement(m)
1967 if keepModSumsForZipSums {
1968 keep[modkey(r)] = true // we need the go version from the go.mod file to do anything useful with the zipfile
1981 loadedZipSumsOnly = whichSums(iota)
1985 // modkey returns the module.Version under which the checksum for m's go.mod
1986 // file is stored in the go.sum file.
1987 func modkey(m module.Version) module.Version {
1988 return module.Version{Path: m.Path, Version: m.Version + "/go.mod"}
1991 func suggestModulePath(path string) string {
1995 for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9' || path[i-1] == '.') {
1999 url = strings.TrimSuffix(url, "/v")
2000 url = strings.TrimSuffix(url, "/")
2002 f := func(c rune) bool {
2003 return c > '9' || c < '0'
2005 s := strings.FieldsFunc(path[i:], f)
2009 m = strings.TrimLeft(m, "0")
2010 if m == "" || m == "1" {
2014 return url + "/v" + m
2017 func suggestGopkgIn(path string) string {
2020 for i > 0 && (('0' <= path[i-1] && path[i-1] <= '9') || (path[i-1] == '.')) {
2024 url = strings.TrimSuffix(url, ".v")
2025 url = strings.TrimSuffix(url, "/v")
2026 url = strings.TrimSuffix(url, "/")
2028 f := func(c rune) bool {
2029 return c > '9' || c < '0'
2031 s := strings.FieldsFunc(path, f)
2036 m = strings.TrimLeft(m, "0")
2041 return url + ".v" + m