]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/go/internal/modload/init.go
cmd/go/internal/modload: ignore $GOPATH/go.mod
[gostls13.git] / src / cmd / go / internal / modload / init.go
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package modload
6
7 import (
8         "bytes"
9         "context"
10         "encoding/json"
11         "errors"
12         "fmt"
13         "internal/lazyregexp"
14         "io"
15         "os"
16         "path"
17         "path/filepath"
18         "slices"
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/gover"
27         "cmd/go/internal/lockedfile"
28         "cmd/go/internal/modfetch"
29         "cmd/go/internal/search"
30
31         "golang.org/x/mod/modfile"
32         "golang.org/x/mod/module"
33 )
34
35 // Variables set by other packages.
36 //
37 // TODO(#40775): See if these can be plumbed as explicit parameters.
38 var (
39         // RootMode determines whether a module root is needed.
40         RootMode Root
41
42         // ForceUseModules may be set to force modules to be enabled when
43         // GO111MODULE=auto or to report an error when GO111MODULE=off.
44         ForceUseModules bool
45
46         allowMissingModuleImports bool
47
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
57 )
58
59 // Variables set in Init.
60 var (
61         initialized bool
62
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
68         modRoots []string
69         gopath   string
70 )
71
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
75         requirements = nil
76         workFilePath = "" // Force module mode
77         modfetch.Reset()
78
79         modRoots = []string{enterModroot}
80         LoadModFile(ctx)
81 }
82
83 // Variable set in InitWorkfile
84 var (
85         // Set to the path to the go.work file, or "" if workspace mode is disabled.
86         workFilePath string
87 )
88
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
95
96         // modRoot maps each module in versions to its absolute filesystem path.
97         modRoot map[module.Version]string
98
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
103
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
107
108         modFiles map[module.Version]*modfile.File
109
110         modContainingCWD module.Version
111
112         workFile *modfile.WorkFile
113
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
117
118         indexMu sync.Mutex
119         indices map[module.Version]*modFileIndex
120 }
121
122 func (mms *MainModuleSet) PathPrefix(m module.Version) string {
123         return mms.pathPrefix[m]
124 }
125
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 {
131         if mms == nil {
132                 return nil
133         }
134         return mms.versions
135 }
136
137 func (mms *MainModuleSet) Contains(path string) bool {
138         if mms == nil {
139                 return false
140         }
141         for _, v := range mms.versions {
142                 if v.Path == path {
143                         return true
144                 }
145         }
146         return false
147 }
148
149 func (mms *MainModuleSet) ModRoot(m module.Version) string {
150         if mms == nil {
151                 return ""
152         }
153         return mms.modRoot[m]
154 }
155
156 func (mms *MainModuleSet) InGorootSrc(m module.Version) bool {
157         if mms == nil {
158                 return false
159         }
160         return mms.inGorootSrc[m]
161 }
162
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")
166         }
167         if len(mms.versions) != 1 {
168                 if inWorkspaceMode() {
169                         panic("internal error: mustGetSingleMainModule called in workspace mode")
170                 } else {
171                         panic("internal error: multiple main modules present outside of workspace mode")
172                 }
173         }
174         return mms.versions[0]
175 }
176
177 func (mms *MainModuleSet) GetSingleIndexOrNil() *modFileIndex {
178         if mms == nil {
179                 return nil
180         }
181         if len(mms.versions) == 0 {
182                 return nil
183         }
184         return mms.indices[mms.mustGetSingleMainModule()]
185 }
186
187 func (mms *MainModuleSet) Index(m module.Version) *modFileIndex {
188         mms.indexMu.Lock()
189         defer mms.indexMu.Unlock()
190         return mms.indices[m]
191 }
192
193 func (mms *MainModuleSet) SetIndex(m module.Version, index *modFileIndex) {
194         mms.indexMu.Lock()
195         defer mms.indexMu.Unlock()
196         mms.indices[m] = index
197 }
198
199 func (mms *MainModuleSet) ModFile(m module.Version) *modfile.File {
200         return mms.modFiles[m]
201 }
202
203 func (mms *MainModuleSet) WorkFile() *modfile.WorkFile {
204         return mms.workFile
205 }
206
207 func (mms *MainModuleSet) Len() int {
208         if mms == nil {
209                 return 0
210         }
211         return len(mms.versions)
212 }
213
214 // ModContainingCWD returns the main module containing the working directory,
215 // or module.Version{} if none of the main modules contain the working
216 // directory.
217 func (mms *MainModuleSet) ModContainingCWD() module.Version {
218         return mms.modContainingCWD
219 }
220
221 func (mms *MainModuleSet) HighestReplaced() map[string]string {
222         return mms.highestReplaced
223 }
224
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)
230         }
231         if mms != nil && len(mms.versions) == 1 {
232                 f := mms.ModFile(mms.mustGetSingleMainModule())
233                 if f == nil {
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.
237                         return gover.Local()
238                 }
239                 return gover.FromGoMod(f)
240         }
241         return gover.DefaultGoModVersion
242 }
243
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
250                 }
251                 return "go" + mms.GoVersion()
252         }
253         if mms != nil && len(mms.versions) == 1 {
254                 f := mms.ModFile(mms.mustGetSingleMainModule())
255                 if f == nil {
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()
260                 }
261                 if f.Toolchain != nil {
262                         return f.Toolchain.Name
263                 }
264         }
265         return "go" + mms.GoVersion()
266 }
267
268 func (mms *MainModuleSet) WorkFileReplaceMap() map[module.Version]module.Version {
269         return mms.workFileReplaceMap
270 }
271
272 var MainModules *MainModuleSet
273
274 type Root int
275
276 const (
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.
281         AutoRoot Root = iota
282
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.
285         NoRoot
286
287         // NeedRoot is used for commands that must run in module mode and don't
288         // make sense without a main module.
289         NeedRoot
290 )
291
292 // ModFile returns the parsed go.mod file.
293 //
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 {
301         Init()
302         modFile := MainModules.ModFile(MainModules.mustGetSingleMainModule())
303         if modFile == nil {
304                 die()
305         }
306         return modFile
307 }
308
309 func BinDir() string {
310         Init()
311         if cfg.GOBIN != "" {
312                 return cfg.GOBIN
313         }
314         if gopath == "" {
315                 return ""
316         }
317         return filepath.Join(gopath, "bin")
318 }
319
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())
325 }
326
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 {
334                 return ""
335         }
336
337         switch gowork := cfg.Getenv("GOWORK"); gowork {
338         case "off":
339                 return ""
340         case "", "auto":
341                 return findWorkspaceFile(wd)
342         default:
343                 if !filepath.IsAbs(gowork) {
344                         base.Fatalf("go: invalid GOWORK: not an absolute path")
345                 }
346                 return gowork
347         }
348 }
349
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 {
353         return workFilePath
354 }
355
356 // Reset clears all the initialized, cached state about the use of modules,
357 // so that we can start over.
358 func Reset() {
359         initialized = false
360         ForceUseModules = false
361         RootMode = 0
362         modRoots = nil
363         cfg.ModulesEnabled = false
364         MainModules = nil
365         requirements = nil
366         workFilePath = ""
367         modfetch.Reset()
368 }
369
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
373 // with modules.
374 func Init() {
375         if initialized {
376                 return
377         }
378         initialized = true
379
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")
385         switch env {
386         default:
387                 base.Fatalf("go: unknown environment setting GO111MODULE=%s", env)
388         case "auto":
389                 mustUseModules = ForceUseModules
390         case "on", "":
391                 mustUseModules = true
392         case "off":
393                 if ForceUseModules {
394                         base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
395                 }
396                 mustUseModules = false
397                 return
398         }
399
400         if err := fsys.Init(base.Cwd()); err != nil {
401                 base.Fatal(err)
402         }
403
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
408         // prompting.
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")
412         }
413
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")
429         }
430
431         if os.Getenv("GCM_INTERACTIVE") == "" {
432                 os.Setenv("GCM_INTERACTIVE", "never")
433         }
434         if modRoots != nil {
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")
440                 }
441                 modRoots = nil
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")
446                 }
447         } else {
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.")
451                         }
452                         if RootMode == NeedRoot {
453                                 base.Fatal(ErrNoModRoot)
454                         }
455                         if !mustUseModules {
456                                 // GO111MODULE is 'auto', and we can't find a module root.
457                                 // Stay in GOPATH mode.
458                                 return
459                         }
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)
469                         }
470                         if !mustUseModules {
471                                 return
472                         }
473                 } else {
474                         modRoots = []string{modRoot}
475                 }
476         }
477         if cfg.ModFile != "" && !strings.HasSuffix(cfg.ModFile, ".mod") {
478                 base.Fatalf("go: -modfile=%s: file does not have .mod extension", cfg.ModFile)
479         }
480
481         // We're in module mode. Set any global variables that need to be set.
482         cfg.ModulesEnabled = true
483         setDefaultBuildMod()
484         list := filepath.SplitList(cfg.BuildContext.GOPATH)
485         if len(list) > 0 && list[0] != "" {
486                 gopath = 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)
491                         }
492                         if !mustUseModules {
493                                 return
494                         }
495                 }
496         }
497 }
498
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.
502 //
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 {
510                 // Already enabled.
511                 return true
512         }
513         if initialized {
514                 // Initialized, not enabled.
515                 return false
516         }
517
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")
521         switch env {
522         case "on", "":
523                 return true
524         case "auto":
525                 break
526         default:
527                 return false
528         }
529
530         return FindGoMod(base.Cwd()) != ""
531 }
532
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)
540         if modRoot == "" {
541                 // GO111MODULE is 'auto', and we can't find a module root.
542                 // Stay in GOPATH mode.
543                 return ""
544         }
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.
551                 return ""
552         }
553         return filepath.Join(modRoot, "go.mod")
554 }
555
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 {
561         Init()
562         return modRoots != nil || cfg.ModulesEnabled
563 }
564
565 func VendorDir() string {
566         if inWorkspaceMode() {
567                 return filepath.Join(filepath.Dir(WorkFilePath()), "vendor")
568         }
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())
573         if modRoot == "" {
574                 panic("vendor directory does not exist when in single module mode outside of a module")
575         }
576         return filepath.Join(modRoot, "vendor")
577 }
578
579 func inWorkspaceMode() bool {
580         if !initialized {
581                 panic("inWorkspaceMode called before modload.Init called")
582         }
583         if !Enabled() {
584                 return false
585         }
586         return workFilePath != ""
587 }
588
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 {
593         Init()
594         return modRoots != nil
595 }
596
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() {
600         Init()
601         if !HasModRoot() {
602                 die()
603         }
604 }
605
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 {
610         MustHaveModRoot()
611         return modFilePath(findModuleRoot(base.Cwd()))
612 }
613
614 func modFilePath(modRoot string) string {
615         if cfg.ModFile != "" {
616                 return cfg.ModFile
617         }
618         return filepath.Join(modRoot, "go.mod")
619 }
620
621 func die() {
622         if cfg.Getenv("GO111MODULE") == "off" {
623                 base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
624         }
625         if inWorkspaceMode() {
626                 base.Fatalf("go: no modules were found in the current workspace; see 'go help work'")
627         }
628         if dir, name := findAltConfig(base.Cwd()); dir != "" {
629                 rel, err := filepath.Rel(base.Cwd(), dir)
630                 if err != nil {
631                         rel = dir
632                 }
633                 cdCmd := ""
634                 if rel != "." {
635                         cdCmd = fmt.Sprintf("cd %s && ", rel)
636                 }
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)
638         }
639         base.Fatal(ErrNoModRoot)
640 }
641
642 var ErrNoModRoot = errors.New("go.mod file not found in current directory or any parent directory; see 'go help modules'")
643
644 type goModDirtyError struct{}
645
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)
649         }
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)
652         }
653         return "updates to go.mod needed; to update it:\n\tgo mod tidy"
654 }
655
656 var errGoModDirty error = goModDirtyError{}
657
658 func loadWorkFile(path string) (workFile *modfile.WorkFile, modRoots []string, err error) {
659         workDir := filepath.Dir(path)
660         wf, err := ReadWorkFile(path)
661         if err != nil {
662                 return nil, nil, err
663         }
664         seen := map[string]bool{}
665         for _, d := range wf.Use {
666                 modRoot := d.Path
667                 if !filepath.IsAbs(modRoot) {
668                         modRoot = filepath.Join(workDir, modRoot)
669                 }
670
671                 if seen[modRoot] {
672                         return nil, nil, fmt.Errorf("path %s appears multiple times in workspace", modRoot)
673                 }
674                 seen[modRoot] = true
675                 modRoots = append(modRoots, modRoot)
676         }
677
678         return wf, modRoots, nil
679 }
680
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)
684         if err != nil {
685                 return nil, err
686         }
687
688         f, err := modfile.ParseWork(path, workData, nil)
689         if err != nil {
690                 return nil, err
691         }
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})
694         }
695         return f, nil
696 }
697
698 // WriteWorkFile cleans and writes out the go.work file to the given path.
699 func WriteWorkFile(path string, wf *modfile.WorkFile) error {
700         wf.SortBlocks()
701         wf.Cleanup()
702         out := modfile.Format(wf.Syntax)
703
704         return os.WriteFile(path, out, 0666)
705 }
706
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 {
712                 return false
713         }
714
715         wf.AddGoStmt(goVers)
716
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
722         // in a module.
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
730         }
731         if gover.IsLang(gover.Local()) {
732                 toolchain = gover.ToolchainMax(toolchain, "go"+goVers)
733         } else {
734                 toolchain = gover.ToolchainMax(toolchain, "go"+gover.Local())
735         }
736
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()
742         } else {
743                 wf.AddToolchainStmt(toolchain)
744         }
745         return true
746 }
747
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
752
753         for _, d := range wf.Use {
754                 if d.Path == "" {
755                         continue // d is marked for deletion.
756                 }
757                 modRoot := d.Path
758                 if d.ModulePath == "" {
759                         missingModulePaths[d.Path] = modRoot
760                 }
761         }
762
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)
767                 if err != nil {
768                         continue // Error will be reported if modules are loaded.
769                 }
770                 wf.AddUse(moddir, f.Module.Mod.Path)
771         }
772 }
773
774 // LoadModFile sets Target and, if there is a main module, parses the initial
775 // build list from its go.mod file.
776 //
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.
781 //
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.
784 //
785 // If LoadModFile or CreateModFile has already been called, LoadModFile returns
786 // the existing in-memory requirements (rather than re-reading them from disk).
787 //
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)
794         if err != nil {
795                 base.Fatal(err)
796         }
797         return rs
798 }
799
800 func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error) {
801         if requirements != nil {
802                 return requirements, nil
803         }
804
805         Init()
806         var workFile *modfile.WorkFile
807         if inWorkspaceMode() {
808                 var err error
809                 workFile, modRoots, err = loadWorkFile(workFilePath)
810                 if err != nil {
811                         return nil, fmt.Errorf("reading go.work: %w", err)
812                 }
813                 for _, modRoot := range modRoots {
814                         sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
815                         modfetch.WorkspaceGoSumFiles = append(modfetch.WorkspaceGoSumFiles, sumFile)
816                 }
817                 modfetch.GoSumFile = workFilePath + ".sum"
818         } else if len(modRoots) == 0 {
819                 // We're in module mode, but not inside a module.
820                 //
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.
826                 //
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
830                 // outside std.
831                 //
832                 // This can be overridden by calling AllowMissingModuleImports.
833                 // For example, 'go get' does this, since it is expected to resolve paths.
834                 //
835                 // See golang.org/issue/32027.
836         } else {
837                 modfetch.GoSumFile = strings.TrimSuffix(modFilePath(modRoots[0]), ".mod") + ".sum"
838         }
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
842                 // any module.
843                 mainModule := module.Version{Path: "command-line-arguments"}
844                 MainModules = makeMainModules([]module.Version{mainModule}, []string{""}, []*modfile.File{nil}, []*modFileIndex{nil}, nil)
845                 var (
846                         goVersion string
847                         pruning   modPruning
848                         roots     []module.Version
849                         direct    = map[string]bool{"go": true}
850                 )
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()
856                         pruning = workspace
857                         roots = []module.Version{
858                                 mainModule,
859                                 {Path: "go", Version: goVersion},
860                                 {Path: "toolchain", Version: gover.LocalToolchain()},
861                         }
862                 } else {
863                         goVersion = gover.Local()
864                         pruning = pruningForGoVersion(goVersion)
865                         roots = []module.Version{
866                                 {Path: "go", Version: goVersion},
867                                 {Path: "toolchain", Version: gover.LocalToolchain()},
868                         }
869                 }
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)
877                 }
878                 return requirements, nil
879         }
880
881         var modFiles []*modfile.File
882         var mainModules []module.Version
883         var indices []*modFileIndex
884         var errs []error
885         for _, modroot := range modRoots {
886                 gomod := modFilePath(modroot)
887                 var fixed bool
888                 data, f, err := ReadModFile(gomod, fixVersion(ctx, &fixed))
889                 if err != nil {
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)
897                                 } else {
898                                         err = fmt.Errorf("cannot load module %s listed in go.work file: %w",
899                                                 base.ShortPath(filepath.Dir(gomod)), err)
900                                 }
901                         }
902                         errs = append(errs, err)
903                         continue
904                 }
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))
913                                 continue
914                         }
915                 }
916
917                 modFiles = append(modFiles, f)
918                 mainModule := f.Module.Mod
919                 mainModules = append(mainModules, mainModule)
920                 indices = append(indices, indexModFile(data, f, mainModule, fixed))
921
922                 if err := module.CheckImportPath(f.Module.Mod.Path); err != nil {
923                         if pathErr, ok := err.(*module.InvalidPathError); ok {
924                                 pathErr.Kind = "module"
925                         }
926                         errs = append(errs, err)
927                 }
928         }
929         if len(errs) > 0 {
930                 return nil, errors.Join(errs...)
931         }
932
933         MainModules = makeMainModules(mainModules, modRoots, modFiles, indices, workFile)
934         setDefaultBuildMod() // possibly enable automatic vendoring
935         rs := requirementsFromModFiles(ctx, workFile, modFiles, opts)
936
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))
946                 }
947                 checkVendorConsistency(indexes, modFiles, modRoots)
948                 rs.initVendor(vendorList)
949         }
950
951         if inWorkspaceMode() {
952                 // We don't need to update the mod file so return early.
953                 requirements = rs
954                 return rs, nil
955         }
956
957         mainModule := MainModules.mustGetSingleMainModule()
958
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.
963                 var err error
964                 rs, err = updateRoots(ctx, rs.direct, rs, nil, nil, false)
965                 if err != nil {
966                         return nil, err
967                 }
968         }
969
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.
975                         v := gover.Local()
976                         if opts != nil && opts.TidyGoVersion != "" {
977                                 v = opts.TidyGoVersion
978                         }
979                         addGoStmt(MainModules.ModFile(mainModule), mainModule, v)
980                         rs = overrideRoots(ctx, rs, []module.Version{{Path: "go", Version: v}})
981
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 {
988                                 var err error
989                                 rs, err = convertPruning(ctx, rs, pruned)
990                                 if err != nil {
991                                         return nil, err
992                                 }
993                         }
994                 } else {
995                         rawGoVersion.Store(mainModule, gover.DefaultGoModVersion)
996                 }
997         }
998
999         requirements = rs
1000         return requirements, nil
1001 }
1002
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))
1006 }
1007
1008 // CreateModFile initializes a new module by creating a go.mod file.
1009 //
1010 // If modPath is empty, CreateModFile will attempt to infer the path from the
1011 // directory location within GOPATH.
1012 //
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}
1020         Init()
1021         modFilePath := modFilePath(modRoot)
1022         if _, err := fsys.Stat(modFilePath); err == nil {
1023                 base.Fatalf("go: %s already exists", modFilePath)
1024         }
1025
1026         if modPath == "" {
1027                 var err error
1028                 modPath, err = findModulePath(modRoot)
1029                 if err != nil {
1030                         base.Fatal(err)
1031                 }
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")
1039                         }
1040                 }
1041                 base.Fatal(err)
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)
1046                 }
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)
1049         }
1050
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.
1056
1057         rs := requirementsFromModFiles(ctx, nil, []*modfile.File{modFile}, nil)
1058         rs, err := updateRoots(ctx, rs.direct, rs, nil, nil, false)
1059         if err != nil {
1060                 base.Fatal(err)
1061         }
1062         requirements = rs
1063         if err := commitRequirements(ctx, WriteOpts{}); err != nil {
1064                 base.Fatal(err)
1065         }
1066
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.
1070         //
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.
1074         empty := true
1075         files, _ := os.ReadDir(modRoot)
1076         for _, f := range files {
1077                 name := f.Name()
1078                 if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
1079                         continue
1080                 }
1081                 if strings.HasSuffix(name, ".go") || f.IsDir() {
1082                         empty = false
1083                         break
1084                 }
1085         }
1086         if !empty {
1087                 fmt.Fprintf(os.Stderr, "go: to add module requirements and sums:\n\tgo mod tidy\n")
1088         }
1089 }
1090
1091 // fixVersion returns a modfile.VersionFixer implemented using the Query function.
1092 //
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.
1096 //
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) {
1100                 defer func() {
1101                         if err == nil && resolved != vers {
1102                                 *fixed = true
1103                         }
1104                 }()
1105
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-"):]
1109                 }
1110
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)
1115                 if !ok {
1116                         return "", &module.ModuleError{
1117                                 Path: path,
1118                                 Err: &module.InvalidVersionError{
1119                                         Version: vers,
1120                                         Err:     fmt.Errorf("malformed module path %q", path),
1121                                 },
1122                         }
1123                 }
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)
1127                         }
1128                         return vers, nil
1129                 }
1130
1131                 info, err := Query(ctx, path, vers, "", nil)
1132                 if err != nil {
1133                         return "", err
1134                 }
1135                 return info.Version, nil
1136         }
1137 }
1138
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.
1143 //
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() {
1147         if initialized {
1148                 panic("AllowMissingModuleImports after Init")
1149         }
1150         allowMissingModuleImports = true
1151 }
1152
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))
1159                 }
1160         }
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{},
1170                 workFile:        workFile,
1171         }
1172         var workFileReplaces []*modfile.Replace
1173         if workFile != nil {
1174                 workFileReplaces = workFile.Replace
1175                 mainModules.workFileReplaceMap = toReplaceMap(workFile.Replace)
1176         }
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)
1181                 }
1182                 mainModulePaths[m.Path] = true
1183         }
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)
1189                 }
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
1194                 }
1195                 replacements[r.Old] = r.New
1196         }
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]
1202
1203                 if mainModules.modRoot[m] == modRootContainingCWD {
1204                         mainModules.modContainingCWD = m
1205                 }
1206
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.
1212                                 //
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] = ""
1219                         }
1220                 }
1221
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] {
1226                                         continue
1227                                 }
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.
1235                                         //
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)
1240                                 }
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)
1243                                 }
1244                                 curModuleReplaces[r.Old] = true
1245                                 replacements[r.Old] = newV
1246
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
1250                                 }
1251                         }
1252                 }
1253         }
1254         return mainModules
1255 }
1256
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() {
1264                 pruning = workspace
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
1271                 }
1272                 roots = appendGoAndToolchainRoots(roots, goVersion, toolchain, direct)
1273         } else {
1274                 pruning = pruningForGoVersion(MainModules.GoVersion())
1275                 if len(modFiles) != 1 {
1276                         panic(fmt.Errorf("requirementsFromModFiles called with %v modfiles outside workspace mode", len(modFiles)))
1277                 }
1278                 modFile := modFiles[0]
1279                 roots, direct = rootsFromModFile(MainModules.mustGetSingleMainModule(), modFile, withToolchainRoot)
1280         }
1281
1282         gover.ModSort(roots)
1283         rs := newRequirements(pruning, roots, direct)
1284         return rs
1285 }
1286
1287 type addToolchainRoot bool
1288
1289 const (
1290         omitToolchainRoot addToolchainRoot = false
1291         withToolchainRoot                  = true
1292 )
1293
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 {
1298                 padding = 1
1299         }
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)
1305                         } else {
1306                                 fmt.Fprintf(os.Stderr, "go: ignoring requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
1307                         }
1308                         continue
1309                 }
1310
1311                 roots = append(roots, r.Mod)
1312                 if !r.Indirect {
1313                         direct[r.Mod.Path] = true
1314                 }
1315         }
1316         goVersion := gover.FromGoMod(modFile)
1317         var toolchain string
1318         if addToolchainRoot && modFile.Toolchain != nil {
1319                 toolchain = modFile.Toolchain.Name
1320         }
1321         roots = appendGoAndToolchainRoots(roots, goVersion, toolchain, direct)
1322         return roots, direct
1323 }
1324
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.
1329
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
1336                 // same toolchain.
1337         }
1338         return roots
1339 }
1340
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)
1349                 }
1350                 // Don't override an explicit '-mod=' argument.
1351                 return
1352         }
1353
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"
1361                 return
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"
1368                 return
1369         case "mod vendor", "work vendor":
1370                 cfg.BuildMod = "readonly"
1371                 return
1372         }
1373         if modRoots == nil {
1374                 if allowMissingModuleImports {
1375                         cfg.BuildMod = "mod"
1376                 } else {
1377                         cfg.BuildMod = "readonly"
1378                 }
1379                 return
1380         }
1381
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
1389                         }
1390                 } else {
1391                         versionSource = "go.mod"
1392                         index := MainModules.GetSingleIndexOrNil()
1393                         if index != nil {
1394                                 goVersion = index.goVersion
1395                         }
1396                 }
1397                 vendorDir := ""
1398                 if workFilePath != "" {
1399                         vendorDir = filepath.Join(filepath.Dir(workFilePath), "vendor")
1400                 } else {
1401                         if len(modRoots) != 1 {
1402                                 panic(fmt.Errorf("outside workspace mode, but have %v modRoots", modRoots))
1403                         }
1404                         vendorDir = filepath.Join(modRoots[0], "vendor")
1405                 }
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)
1414                                 } else {
1415                                         vendoredWorkspace, err := modulesTextIsForWorkspace(vendorDir)
1416                                         if err != nil {
1417                                                 base.Fatalf("go: reading modules.txt for vendor directory: %v", err)
1418                                         }
1419                                         if vendoredWorkspace != (versionSource == "go.work") {
1420                                                 if vendoredWorkspace {
1421                                                         cfg.BuildModReason = "Outside workspace mode, but vendor directory is for a workspace."
1422                                                 } else {
1423                                                         cfg.BuildModReason = "In workspace mode, but vendor directory is not for a workspace"
1424                                                 }
1425                                         } else {
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."
1431                                                 return
1432                                         }
1433                                 }
1434                                 modGo = goVersion
1435                         }
1436
1437                 }
1438         }
1439
1440         cfg.BuildMod = "readonly"
1441 }
1442
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).
1450                 return false, nil
1451         }
1452         if err != nil {
1453                 return false, err
1454         }
1455         var buf [512]byte
1456         n, err := f.Read(buf[:])
1457         if err != nil && err != io.EOF {
1458                 return false, err
1459         }
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" {
1465                                 return true, nil
1466                         }
1467                 }
1468         }
1469         return false, nil
1470 }
1471
1472 func mustHaveCompleteRequirements() bool {
1473         return cfg.BuildMod != "mod" && !inWorkspaceMode()
1474 }
1475
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 != "" {
1481                 return
1482         }
1483         forceGoStmt(modFile, mod, v)
1484 }
1485
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)
1489         }
1490         rawGoVersion.Store(mod, v)
1491 }
1492
1493 var altConfigs = []string{
1494         ".git/config",
1495 }
1496
1497 func findModuleRoot(dir string) (roots string) {
1498         if dir == "" {
1499                 panic("dir not set")
1500         }
1501         dir = filepath.Clean(dir)
1502
1503         // Look for enclosing go.mod.
1504         for {
1505                 if fi, err := fsys.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {
1506                         return dir
1507                 }
1508                 d := filepath.Dir(dir)
1509                 if d == dir {
1510                         break
1511                 }
1512                 dir = d
1513         }
1514         return ""
1515 }
1516
1517 func findWorkspaceFile(dir string) (root string) {
1518         if dir == "" {
1519                 panic("dir not set")
1520         }
1521         dir = filepath.Clean(dir)
1522
1523         // Look for enclosing go.mod.
1524         for {
1525                 f := filepath.Join(dir, "go.work")
1526                 if fi, err := fsys.Stat(f); err == nil && !fi.IsDir() {
1527                         return f
1528                 }
1529                 d := filepath.Dir(dir)
1530                 if d == dir {
1531                         break
1532                 }
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.
1537                         return ""
1538                 }
1539                 dir = d
1540         }
1541         return ""
1542 }
1543
1544 func findAltConfig(dir string) (root, name string) {
1545         if dir == "" {
1546                 panic("dir not set")
1547         }
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).
1552                 return "", ""
1553         }
1554         for {
1555                 for _, name := range altConfigs {
1556                         if fi, err := fsys.Stat(filepath.Join(dir, name)); err == nil && !fi.IsDir() {
1557                                 return dir, name
1558                         }
1559                 }
1560                 d := filepath.Dir(dir)
1561                 if d == dir {
1562                         break
1563                 }
1564                 dir = d
1565         }
1566         return "", ""
1567 }
1568
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.
1575
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 != "" {
1582                                 return com, nil
1583                         }
1584                 }
1585         }
1586         for _, info1 := range list {
1587                 if info1.IsDir() {
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
1593                                         }
1594                                 }
1595                         }
1596                 }
1597         }
1598
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
1605         }
1606
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
1613         }
1614
1615         // Look for path in GOPATH.
1616         var badPathErr error
1617         for _, gpdir := range filepath.SplitList(cfg.BuildContext.GOPATH) {
1618                 if gpdir == "" {
1619                         continue
1620                 }
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 {
1625                                 badPathErr = err
1626                                 break
1627                         }
1628                         return path, nil
1629                 }
1630         }
1631
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)
1637         }
1638         msg := `cannot determine module path for source directory %s (%s)
1639
1640 Example usage:
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
1643
1644 Run 'go help mod init' for more information.
1645 `
1646         return "", fmt.Errorf(msg, dir, reason)
1647 }
1648
1649 var (
1650         importCommentRE = lazyregexp.New(`(?m)^package[ \t]+[^ \t\r\n/]+[ \t]+//[ \t]+import[ \t]+(\"[^"]+\")[ \t]*\r?\n`)
1651 )
1652
1653 func findImportComment(file string) string {
1654         data, err := os.ReadFile(file)
1655         if err != nil {
1656                 return ""
1657         }
1658         m := importCommentRE.FindSubmatch(data)
1659         if m == nil {
1660                 return ""
1661         }
1662         path, err := strconv.Unquote(string(m[1]))
1663         if err != nil {
1664                 return ""
1665         }
1666         return path
1667 }
1668
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
1673
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'
1677 }
1678
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)
1683 }
1684
1685 // commitRequirements ensures go.mod and go.sum are up to date with the current
1686 // requirements.
1687 //
1688 // In "mod" mode, commitRequirements writes changes to go.mod and go.sum.
1689 //
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.
1692 //
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())
1699         }
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.
1702                 return nil
1703         }
1704         mainModule := MainModules.mustGetSingleMainModule()
1705         modFile := MainModules.ModFile(mainModule)
1706         if modFile == nil {
1707                 // command-line-arguments has no .mod file to write.
1708                 return nil
1709         }
1710         modFilePath := modFilePath(MainModules.ModRoot(mainModule))
1711
1712         var list []*modfile.Require
1713         toolchain := ""
1714         goVersion := ""
1715         for _, m := range requirements.rootModules {
1716                 if m.Path == "go" {
1717                         goVersion = m.Version
1718                         continue
1719                 }
1720                 if m.Path == "toolchain" {
1721                         toolchain = m.Version
1722                         continue
1723                 }
1724                 list = append(list, &modfile.Require{
1725                         Mod:      m,
1726                         Indirect: !requirements.direct[m.Path],
1727                 })
1728         }
1729
1730         // Update go line.
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")
1735         }
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}
1739         }
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.
1747                 } else {
1748                         wroteGo = true
1749                         forceGoStmt(modFile, mainModule, goVersion)
1750                 }
1751         }
1752         if toolchain == "" {
1753                 toolchain = "go" + goVersion
1754         }
1755
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.
1761         //
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)
1770         }
1771
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()
1776         } else {
1777                 modFile.AddToolchainStmt(toolchain)
1778         }
1779
1780         // Update require blocks.
1781         if gover.Compare(goVersion, gover.SeparateIndirectVersion) < 0 {
1782                 modFile.SetRequire(list)
1783         } else {
1784                 modFile.SetRequireSeparateIndirect(list)
1785         }
1786         modFile.Cleanup()
1787
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
1794         }
1795
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 {
1803                                 return err
1804                         }
1805                 }
1806                 return nil
1807         }
1808         if _, ok := fsys.OverlayPath(modFilePath); ok {
1809                 if dirty {
1810                         return errors.New("updates to go.mod needed, but go.mod is part of the overlay specified with -overlay")
1811                 }
1812                 return nil
1813         }
1814
1815         new, err := modFile.Format()
1816         if err != nil {
1817                 return err
1818         }
1819         defer func() {
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))
1822
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" {
1826                         if err == nil {
1827                                 err = modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, requirements, addBuildListZipSums), mustHaveCompleteRequirements())
1828                         }
1829                 }
1830         }()
1831
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 {
1835                 defer unlock()
1836         }
1837
1838         errNoChange := errors.New("no update needed")
1839
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
1843                         // other process.
1844                         return nil, errNoChange
1845                 }
1846
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")
1855                 }
1856
1857                 return new, nil
1858         })
1859
1860         if err != nil && err != errNoChange {
1861                 return fmt.Errorf("updating go.mod: %w", err)
1862         }
1863         return nil
1864 }
1865
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)
1878
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
1884         if ld == nil {
1885                 if gover.Compare(MainModules.GoVersion(), gover.TidyGoModSumVersion) < 0 && cfg.BuildMod != "mod" {
1886                         keepModSumsForZipSums = false
1887                 }
1888         } else {
1889                 keepPkgGoModSums := true
1890                 if gover.Compare(ld.requirements.GoVersion(), gover.TidyGoModSumVersion) < 0 && (ld.Tidy || cfg.BuildMod != "mod") {
1891                         keepPkgGoModSums = false
1892                         keepModSumsForZipSums = false
1893                 }
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 {
1899                                 continue
1900                         }
1901
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
1910                         }
1911
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)
1923                                                         keep[r] = true
1924                                                 }
1925                                         }
1926                                         continue
1927                                 }
1928                         }
1929
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)
1935                                         keep[r] = true
1936                                 }
1937                         }
1938                 }
1939         }
1940
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 {
1949                                 keep[r] = true
1950                         }
1951                 }
1952         } else {
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
1961                         }
1962                 })
1963
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
1969                                 }
1970                                 keep[r] = true
1971                         }
1972                 }
1973         }
1974
1975         return keep
1976 }
1977
1978 type whichSums int8
1979
1980 const (
1981         loadedZipSumsOnly = whichSums(iota)
1982         addBuildListZipSums
1983 )
1984
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"}
1989 }
1990
1991 func suggestModulePath(path string) string {
1992         var m string
1993
1994         i := len(path)
1995         for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9' || path[i-1] == '.') {
1996                 i--
1997         }
1998         url := path[:i]
1999         url = strings.TrimSuffix(url, "/v")
2000         url = strings.TrimSuffix(url, "/")
2001
2002         f := func(c rune) bool {
2003                 return c > '9' || c < '0'
2004         }
2005         s := strings.FieldsFunc(path[i:], f)
2006         if len(s) > 0 {
2007                 m = s[0]
2008         }
2009         m = strings.TrimLeft(m, "0")
2010         if m == "" || m == "1" {
2011                 return url + "/v2"
2012         }
2013
2014         return url + "/v" + m
2015 }
2016
2017 func suggestGopkgIn(path string) string {
2018         var m string
2019         i := len(path)
2020         for i > 0 && (('0' <= path[i-1] && path[i-1] <= '9') || (path[i-1] == '.')) {
2021                 i--
2022         }
2023         url := path[:i]
2024         url = strings.TrimSuffix(url, ".v")
2025         url = strings.TrimSuffix(url, "/v")
2026         url = strings.TrimSuffix(url, "/")
2027
2028         f := func(c rune) bool {
2029                 return c > '9' || c < '0'
2030         }
2031         s := strings.FieldsFunc(path, f)
2032         if len(s) > 0 {
2033                 m = s[0]
2034         }
2035
2036         m = strings.TrimLeft(m, "0")
2037
2038         if m == "" {
2039                 return url + ".v1"
2040         }
2041         return url + ".v" + m
2042 }