Debian Codegraph @

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

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