1
2
3
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
38
39
40 var (
41 allowMissingModuleImports bool
42
43
44
45
46
47
48
49
50
51 ExplicitWriteGoMod bool
52 )
53
54
55 var (
56 gopath string
57 )
58
59
60 func EnterModule(loaderstate *State, ctx context.Context, enterModroot string) {
61 loaderstate.MainModules = nil
62 loaderstate.requirements = nil
63 loaderstate.workFilePath = ""
64 modfetch.Reset()
65
66 loaderstate.modRoots = []string{enterModroot}
67 LoadModFile(loaderstate, ctx)
68 }
69
70
71
72
73
74 func EnterWorkspace(loaderstate *State, ctx context.Context) (exit func(), err error) {
75
76 mm := loaderstate.MainModules.mustGetSingleMainModule(loaderstate)
77
78 _, _, updatedmodfile, err := UpdateGoModFromReqs(loaderstate, ctx, WriteOpts{})
79 if err != nil {
80 return nil, err
81 }
82
83
84 oldstate := loaderstate.setState(State{})
85 loaderstate.ForceUseModules = true
86
87
88 InitWorkfile(loaderstate)
89 LoadModFile(loaderstate, ctx)
90
91
92 *loaderstate.MainModules.ModFile(mm) = *updatedmodfile
93 loaderstate.requirements = requirementsFromModFiles(loaderstate, ctx, loaderstate.MainModules.workFile, slices.Collect(maps.Values(loaderstate.MainModules.modFiles)), nil)
94
95 return func() {
96 loaderstate.setState(oldstate)
97 }, nil
98 }
99
100 type MainModuleSet struct {
101
102
103
104
105 versions []module.Version
106
107
108 modRoot map[module.Version]string
109
110
111
112
113 pathPrefix map[module.Version]string
114
115
116
117 inGorootSrc map[module.Version]bool
118
119 modFiles map[module.Version]*modfile.File
120
121 tools map[string]bool
122
123 modContainingCWD module.Version
124
125 workFile *modfile.WorkFile
126
127 workFileReplaceMap map[module.Version]module.Version
128
129 highestReplaced map[string]string
130
131 indexMu sync.RWMutex
132 indices map[module.Version]*modFileIndex
133 }
134
135 func (mms *MainModuleSet) PathPrefix(m module.Version) string {
136 return mms.pathPrefix[m]
137 }
138
139
140
141
142
143 func (mms *MainModuleSet) Versions() []module.Version {
144 if mms == nil {
145 return nil
146 }
147 return mms.versions
148 }
149
150
151
152 func (mms *MainModuleSet) Tools() map[string]bool {
153 if mms == nil {
154 return nil
155 }
156 return mms.tools
157 }
158
159 func (mms *MainModuleSet) Contains(path string) bool {
160 if mms == nil {
161 return false
162 }
163 for _, v := range mms.versions {
164 if v.Path == path {
165 return true
166 }
167 }
168 return false
169 }
170
171 func (mms *MainModuleSet) ModRoot(m module.Version) string {
172 if mms == nil {
173 return ""
174 }
175 return mms.modRoot[m]
176 }
177
178 func (mms *MainModuleSet) InGorootSrc(m module.Version) bool {
179 if mms == nil {
180 return false
181 }
182 return mms.inGorootSrc[m]
183 }
184
185 func (mms *MainModuleSet) mustGetSingleMainModule(loaderstate *State) module.Version {
186 mm, err := mms.getSingleMainModule(loaderstate)
187 if err != nil {
188 panic(err)
189 }
190 return mm
191 }
192
193 func (mms *MainModuleSet) getSingleMainModule(loaderstate *State) (module.Version, error) {
194 if mms == nil || len(mms.versions) == 0 {
195 return module.Version{}, errors.New("internal error: mustGetSingleMainModule called in context with no main modules")
196 }
197 if len(mms.versions) != 1 {
198 if inWorkspaceMode(loaderstate) {
199 return module.Version{}, errors.New("internal error: mustGetSingleMainModule called in workspace mode")
200 } else {
201 return module.Version{}, errors.New("internal error: multiple main modules present outside of workspace mode")
202 }
203 }
204 return mms.versions[0], nil
205 }
206
207 func (mms *MainModuleSet) GetSingleIndexOrNil(loaderstate *State) *modFileIndex {
208 if mms == nil {
209 return nil
210 }
211 if len(mms.versions) == 0 {
212 return nil
213 }
214 return mms.indices[mms.mustGetSingleMainModule(loaderstate)]
215 }
216
217 func (mms *MainModuleSet) Index(m module.Version) *modFileIndex {
218 mms.indexMu.RLock()
219 defer mms.indexMu.RUnlock()
220 return mms.indices[m]
221 }
222
223 func (mms *MainModuleSet) SetIndex(m module.Version, index *modFileIndex) {
224 mms.indexMu.Lock()
225 defer mms.indexMu.Unlock()
226 mms.indices[m] = index
227 }
228
229 func (mms *MainModuleSet) ModFile(m module.Version) *modfile.File {
230 return mms.modFiles[m]
231 }
232
233 func (mms *MainModuleSet) WorkFile() *modfile.WorkFile {
234 return mms.workFile
235 }
236
237 func (mms *MainModuleSet) Len() int {
238 if mms == nil {
239 return 0
240 }
241 return len(mms.versions)
242 }
243
244
245
246
247 func (mms *MainModuleSet) ModContainingCWD() module.Version {
248 return mms.modContainingCWD
249 }
250
251 func (mms *MainModuleSet) HighestReplaced() map[string]string {
252 return mms.highestReplaced
253 }
254
255
256
257 func (mms *MainModuleSet) GoVersion(loaderstate *State) string {
258 if inWorkspaceMode(loaderstate) {
259 return gover.FromGoWork(mms.workFile)
260 }
261 if mms != nil && len(mms.versions) == 1 {
262 f := mms.ModFile(mms.mustGetSingleMainModule(loaderstate))
263 if f == nil {
264
265
266
267 return gover.Local()
268 }
269 return gover.FromGoMod(f)
270 }
271 return gover.DefaultGoModVersion
272 }
273
274
275
276
277 func (mms *MainModuleSet) Godebugs(loaderstate *State) []*modfile.Godebug {
278 if inWorkspaceMode(loaderstate) {
279 if mms.workFile != nil {
280 return mms.workFile.Godebug
281 }
282 return nil
283 }
284 if mms != nil && len(mms.versions) == 1 {
285 f := mms.ModFile(mms.mustGetSingleMainModule(loaderstate))
286 if f == nil {
287
288 return nil
289 }
290 return f.Godebug
291 }
292 return nil
293 }
294
295 func (mms *MainModuleSet) WorkFileReplaceMap() map[module.Version]module.Version {
296 return mms.workFileReplaceMap
297 }
298
299 type Root int
300
301 const (
302
303
304
305
306 AutoRoot Root = iota
307
308
309
310 NoRoot
311
312
313
314 NeedRoot
315 )
316
317
318
319
320
321
322
323
324
325 func ModFile(loaderstate *State) *modfile.File {
326 Init(loaderstate)
327 modFile := loaderstate.MainModules.ModFile(loaderstate.MainModules.mustGetSingleMainModule(loaderstate))
328 if modFile == nil {
329 die(loaderstate)
330 }
331 return modFile
332 }
333
334 func BinDir(loaderstate *State) string {
335 Init(loaderstate)
336 if cfg.GOBIN != "" {
337 return cfg.GOBIN
338 }
339 if gopath == "" {
340 return ""
341 }
342 return filepath.Join(gopath, "bin")
343 }
344
345
346
347
348 func InitWorkfile(loaderstate *State) {
349
350 fips140.Init()
351 if err := fsys.Init(); err != nil {
352 base.Fatal(err)
353 }
354 loaderstate.workFilePath = FindGoWork(loaderstate, base.Cwd())
355 }
356
357
358
359
360
361
362 func FindGoWork(loaderstate *State, wd string) string {
363 if loaderstate.RootMode == NoRoot {
364 return ""
365 }
366
367 switch gowork := cfg.Getenv("GOWORK"); gowork {
368 case "off":
369 return ""
370 case "", "auto":
371 return findWorkspaceFile(wd)
372 default:
373 if !filepath.IsAbs(gowork) {
374 base.Fatalf("go: invalid GOWORK: not an absolute path")
375 }
376 return gowork
377 }
378 }
379
380
381
382 func WorkFilePath(loaderstate *State) string {
383 return loaderstate.workFilePath
384 }
385
386
387
388 func (s *State) Reset() {
389 s.setState(State{})
390 }
391
392 func (s *State) setState(new State) State {
393 oldState := State{
394 initialized: s.initialized,
395 ForceUseModules: s.ForceUseModules,
396 RootMode: s.RootMode,
397 modRoots: s.modRoots,
398 modulesEnabled: cfg.ModulesEnabled,
399 MainModules: s.MainModules,
400 requirements: s.requirements,
401 }
402 s.initialized = new.initialized
403 s.ForceUseModules = new.ForceUseModules
404 s.RootMode = new.RootMode
405 s.modRoots = new.modRoots
406 cfg.ModulesEnabled = new.modulesEnabled
407 s.MainModules = new.MainModules
408 s.requirements = new.requirements
409 s.workFilePath = new.workFilePath
410
411
412
413 oldState.modfetchState = modfetch.SetState(new.modfetchState)
414 return oldState
415 }
416
417 type State struct {
418 initialized bool
419
420
421
422 ForceUseModules bool
423
424
425 RootMode Root
426
427
428
429
430
431
432 modRoots []string
433 modulesEnabled bool
434 MainModules *MainModuleSet
435
436
437
438
439
440
441
442
443
444
445
446 requirements *Requirements
447
448
449
450 workFilePath string
451 modfetchState modfetch.State
452 }
453
454 func NewState() *State { return &State{} }
455
456
457
458
459
460 func Init(loaderstate *State) {
461 if loaderstate.initialized {
462 return
463 }
464 loaderstate.initialized = true
465
466 fips140.Init()
467
468
469
470
471 var mustUseModules bool
472 env := cfg.Getenv("GO111MODULE")
473 switch env {
474 default:
475 base.Fatalf("go: unknown environment setting GO111MODULE=%s", env)
476 case "auto":
477 mustUseModules = loaderstate.ForceUseModules
478 case "on", "":
479 mustUseModules = true
480 case "off":
481 if loaderstate.ForceUseModules {
482 base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
483 }
484 mustUseModules = false
485 return
486 }
487
488 if err := fsys.Init(); err != nil {
489 base.Fatal(err)
490 }
491
492
493
494
495
496
497
498 if os.Getenv("GIT_TERMINAL_PROMPT") == "" {
499 os.Setenv("GIT_TERMINAL_PROMPT", "0")
500 }
501
502 if os.Getenv("GCM_INTERACTIVE") == "" {
503 os.Setenv("GCM_INTERACTIVE", "never")
504 }
505 if loaderstate.modRoots != nil {
506
507
508 } else if loaderstate.RootMode == NoRoot {
509 if cfg.ModFile != "" && !base.InGOFLAGS("-modfile") {
510 base.Fatalf("go: -modfile cannot be used with commands that ignore the current module")
511 }
512 loaderstate.modRoots = nil
513 } else if loaderstate.workFilePath != "" {
514
515 if cfg.ModFile != "" {
516 base.Fatalf("go: -modfile cannot be used in workspace mode")
517 }
518 } else {
519 if modRoot := findModuleRoot(base.Cwd()); modRoot == "" {
520 if cfg.ModFile != "" {
521 base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.")
522 }
523 if loaderstate.RootMode == NeedRoot {
524 base.Fatal(NewNoMainModulesError(loaderstate))
525 }
526 if !mustUseModules {
527
528
529 return
530 }
531 } else if search.InDir(modRoot, os.TempDir()) == "." {
532
533
534
535
536
537 fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in system temp root %v\n", os.TempDir())
538 if loaderstate.RootMode == NeedRoot {
539 base.Fatal(NewNoMainModulesError(loaderstate))
540 }
541 if !mustUseModules {
542 return
543 }
544 } else {
545 loaderstate.modRoots = []string{modRoot}
546 }
547 }
548 if cfg.ModFile != "" && !strings.HasSuffix(cfg.ModFile, ".mod") {
549 base.Fatalf("go: -modfile=%s: file does not have .mod extension", cfg.ModFile)
550 }
551
552
553 cfg.ModulesEnabled = true
554 setDefaultBuildMod(loaderstate)
555 list := filepath.SplitList(cfg.BuildContext.GOPATH)
556 if len(list) > 0 && list[0] != "" {
557 gopath = list[0]
558 if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil {
559 fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in $GOPATH %v\n", gopath)
560 if loaderstate.RootMode == NeedRoot {
561 base.Fatal(NewNoMainModulesError(loaderstate))
562 }
563 if !mustUseModules {
564 return
565 }
566 }
567 }
568 }
569
570
571
572
573
574
575
576
577
578
579 func WillBeEnabled(loaderstate *State) bool {
580 if loaderstate.modRoots != nil || cfg.ModulesEnabled {
581
582 return true
583 }
584 if loaderstate.initialized {
585
586 return false
587 }
588
589
590
591 env := cfg.Getenv("GO111MODULE")
592 switch env {
593 case "on", "":
594 return true
595 case "auto":
596 break
597 default:
598 return false
599 }
600
601 return FindGoMod(base.Cwd()) != ""
602 }
603
604
605
606
607
608
609 func FindGoMod(wd string) string {
610 modRoot := findModuleRoot(wd)
611 if modRoot == "" {
612
613
614 return ""
615 }
616 if search.InDir(modRoot, os.TempDir()) == "." {
617
618
619
620
621
622 return ""
623 }
624 return filepath.Join(modRoot, "go.mod")
625 }
626
627
628
629
630
631 func Enabled(loaderstate *State) bool {
632 Init(loaderstate)
633 return loaderstate.modRoots != nil || cfg.ModulesEnabled
634 }
635
636 func (s *State) vendorDir() (string, error) {
637 if inWorkspaceMode(s) {
638 return filepath.Join(filepath.Dir(WorkFilePath(s)), "vendor"), nil
639 }
640 mainModule, err := s.MainModules.getSingleMainModule(s)
641 if err != nil {
642 return "", err
643 }
644
645
646
647 modRoot := s.MainModules.ModRoot(mainModule)
648 if modRoot == "" {
649 return "", errors.New("vendor directory does not exist when in single module mode outside of a module")
650 }
651 return filepath.Join(modRoot, "vendor"), nil
652 }
653
654 func (s *State) VendorDirOrEmpty() string {
655 dir, err := s.vendorDir()
656 if err != nil {
657 return ""
658 }
659 return dir
660 }
661
662 func VendorDir(loaderstate *State) string {
663 dir, err := loaderstate.vendorDir()
664 if err != nil {
665 panic(err)
666 }
667 return dir
668 }
669
670 func inWorkspaceMode(loaderstate *State) bool {
671 if !loaderstate.initialized {
672 panic("inWorkspaceMode called before modload.Init called")
673 }
674 if !Enabled(loaderstate) {
675 return false
676 }
677 return loaderstate.workFilePath != ""
678 }
679
680
681
682
683 func HasModRoot(loaderstate *State) bool {
684 Init(loaderstate)
685 return loaderstate.modRoots != nil
686 }
687
688
689
690 func MustHaveModRoot(loaderstate *State) {
691 Init(loaderstate)
692 if !HasModRoot(loaderstate) {
693 die(loaderstate)
694 }
695 }
696
697
698
699
700 func ModFilePath(loaderstate *State) string {
701 MustHaveModRoot(loaderstate)
702 return modFilePath(findModuleRoot(base.Cwd()))
703 }
704
705 func modFilePath(modRoot string) string {
706
707
708
709 if cfg.ModFile != "" {
710 return cfg.ModFile
711 }
712 return filepath.Join(modRoot, "go.mod")
713 }
714
715 func die(loaderstate *State) {
716 if cfg.Getenv("GO111MODULE") == "off" {
717 base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
718 }
719 if !inWorkspaceMode(loaderstate) {
720 if dir, name := findAltConfig(base.Cwd()); dir != "" {
721 rel, err := filepath.Rel(base.Cwd(), dir)
722 if err != nil {
723 rel = dir
724 }
725 cdCmd := ""
726 if rel != "." {
727 cdCmd = fmt.Sprintf("cd %s && ", rel)
728 }
729 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)
730 }
731 }
732 base.Fatal(NewNoMainModulesError(loaderstate))
733 }
734
735 var ErrNoModRoot = errors.New("no module root")
736
737
738
739 type noMainModulesError struct {
740 inWorkspaceMode bool
741 }
742
743 func (e noMainModulesError) Error() string {
744 if e.inWorkspaceMode {
745 return "no modules were found in the current workspace; see 'go help work'"
746 }
747 return "go.mod file not found in current directory or any parent directory; see 'go help modules'"
748 }
749
750 func (e noMainModulesError) Unwrap() error {
751 return ErrNoModRoot
752 }
753
754 func NewNoMainModulesError(s *State) noMainModulesError {
755 return noMainModulesError{
756 inWorkspaceMode: inWorkspaceMode(s),
757 }
758 }
759
760 type goModDirtyError struct{}
761
762 func (goModDirtyError) Error() string {
763 if cfg.BuildModExplicit {
764 return fmt.Sprintf("updates to go.mod needed, disabled by -mod=%v; to update it:\n\tgo mod tidy", cfg.BuildMod)
765 }
766 if cfg.BuildModReason != "" {
767 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)
768 }
769 return "updates to go.mod needed; to update it:\n\tgo mod tidy"
770 }
771
772 var errGoModDirty error = goModDirtyError{}
773
774
775
776
777 func LoadWorkFile(path string) (workFile *modfile.WorkFile, modRoots []string, err error) {
778 workDir := filepath.Dir(path)
779 wf, err := ReadWorkFile(path)
780 if err != nil {
781 return nil, nil, err
782 }
783 seen := map[string]bool{}
784 for _, d := range wf.Use {
785 modRoot := d.Path
786 if !filepath.IsAbs(modRoot) {
787 modRoot = filepath.Join(workDir, modRoot)
788 }
789
790 if seen[modRoot] {
791 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)
792 }
793 seen[modRoot] = true
794 modRoots = append(modRoots, modRoot)
795 }
796
797 for _, g := range wf.Godebug {
798 if err := CheckGodebug("godebug", g.Key, g.Value); err != nil {
799 return nil, nil, fmt.Errorf("error loading go.work:\n%s:%d: %w", base.ShortPath(path), g.Syntax.Start.Line, err)
800 }
801 }
802
803 return wf, modRoots, nil
804 }
805
806
807 func ReadWorkFile(path string) (*modfile.WorkFile, error) {
808 path = base.ShortPath(path)
809 workData, err := fsys.ReadFile(path)
810 if err != nil {
811 return nil, fmt.Errorf("reading go.work: %w", err)
812 }
813
814 f, err := modfile.ParseWork(path, workData, nil)
815 if err != nil {
816 return nil, fmt.Errorf("errors parsing go.work:\n%w", err)
817 }
818 if f.Go != nil && gover.Compare(f.Go.Version, gover.Local()) > 0 && cfg.CmdName != "work edit" {
819 base.Fatal(&gover.TooNewError{What: base.ShortPath(path), GoVersion: f.Go.Version})
820 }
821 return f, nil
822 }
823
824
825 func WriteWorkFile(path string, wf *modfile.WorkFile) error {
826 wf.SortBlocks()
827 wf.Cleanup()
828 out := modfile.Format(wf.Syntax)
829
830 return os.WriteFile(path, out, 0666)
831 }
832
833
834
835 func UpdateWorkGoVersion(wf *modfile.WorkFile, goVers string) (changed bool) {
836 old := gover.FromGoWork(wf)
837 if gover.Compare(old, goVers) >= 0 {
838 return false
839 }
840
841 wf.AddGoStmt(goVers)
842
843 if wf.Toolchain == nil {
844 return true
845 }
846
847
848
849
850
851
852
853
854
855
856 toolchain := wf.Toolchain.Name
857 toolVers := gover.FromToolchain(toolchain)
858 if toolchain == "go"+goVers || gover.Compare(toolVers, goVers) < 0 || gover.Compare(toolVers, gover.GoStrictVersion) < 0 {
859 wf.DropToolchainStmt()
860 }
861
862 return true
863 }
864
865
866
867 func UpdateWorkFile(wf *modfile.WorkFile) {
868 missingModulePaths := map[string]string{}
869
870 for _, d := range wf.Use {
871 if d.Path == "" {
872 continue
873 }
874 modRoot := d.Path
875 if d.ModulePath == "" {
876 missingModulePaths[d.Path] = modRoot
877 }
878 }
879
880
881
882 for moddir, absmodroot := range missingModulePaths {
883 _, f, err := ReadModFile(filepath.Join(absmodroot, "go.mod"), nil)
884 if err != nil {
885 continue
886 }
887 wf.AddUse(moddir, f.Module.Mod.Path)
888 }
889 }
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909 func LoadModFile(loaderstate *State, ctx context.Context) *Requirements {
910 rs, err := loadModFile(loaderstate, ctx, nil)
911 if err != nil {
912 base.Fatal(err)
913 }
914 return rs
915 }
916
917 func loadModFile(loaderstate *State, ctx context.Context, opts *PackageOpts) (*Requirements, error) {
918 if loaderstate.requirements != nil {
919 return loaderstate.requirements, nil
920 }
921
922 Init(loaderstate)
923 var workFile *modfile.WorkFile
924 if inWorkspaceMode(loaderstate) {
925 var err error
926 workFile, loaderstate.modRoots, err = LoadWorkFile(loaderstate.workFilePath)
927 if err != nil {
928 return nil, err
929 }
930 for _, modRoot := range loaderstate.modRoots {
931 sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
932 modfetch.WorkspaceGoSumFiles = append(modfetch.WorkspaceGoSumFiles, sumFile)
933 }
934 modfetch.GoSumFile = loaderstate.workFilePath + ".sum"
935 } else if len(loaderstate.modRoots) == 0 {
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953 } else {
954 modfetch.GoSumFile = strings.TrimSuffix(modFilePath(loaderstate.modRoots[0]), ".mod") + ".sum"
955 }
956 if len(loaderstate.modRoots) == 0 {
957
958
959
960 mainModule := module.Version{Path: "command-line-arguments"}
961 loaderstate.MainModules = makeMainModules(loaderstate, []module.Version{mainModule}, []string{""}, []*modfile.File{nil}, []*modFileIndex{nil}, nil)
962 var (
963 goVersion string
964 pruning modPruning
965 roots []module.Version
966 direct = map[string]bool{"go": true}
967 )
968 if inWorkspaceMode(loaderstate) {
969
970
971
972 goVersion = loaderstate.MainModules.GoVersion(loaderstate)
973 pruning = workspace
974 roots = []module.Version{
975 mainModule,
976 {Path: "go", Version: goVersion},
977 {Path: "toolchain", Version: gover.LocalToolchain()},
978 }
979 } else {
980 goVersion = gover.Local()
981 pruning = pruningForGoVersion(goVersion)
982 roots = []module.Version{
983 {Path: "go", Version: goVersion},
984 {Path: "toolchain", Version: gover.LocalToolchain()},
985 }
986 }
987 rawGoVersion.Store(mainModule, goVersion)
988 loaderstate.requirements = newRequirements(loaderstate, pruning, roots, direct)
989 if cfg.BuildMod == "vendor" {
990
991
992
993 loaderstate.requirements.initVendor(loaderstate, nil)
994 }
995 return loaderstate.requirements, nil
996 }
997
998 var modFiles []*modfile.File
999 var mainModules []module.Version
1000 var indices []*modFileIndex
1001 var errs []error
1002 for _, modroot := range loaderstate.modRoots {
1003 gomod := modFilePath(modroot)
1004 var fixed bool
1005 data, f, err := ReadModFile(gomod, fixVersion(loaderstate, ctx, &fixed))
1006 if err != nil {
1007 if inWorkspaceMode(loaderstate) {
1008 if tooNew, ok := err.(*gover.TooNewError); ok && !strings.HasPrefix(cfg.CmdName, "work ") {
1009
1010
1011
1012
1013 err = errWorkTooOld(gomod, workFile, tooNew.GoVersion)
1014 } else {
1015 err = fmt.Errorf("cannot load module %s listed in go.work file: %w",
1016 base.ShortPath(filepath.Dir(gomod)), base.ShortPathError(err))
1017 }
1018 }
1019 errs = append(errs, err)
1020 continue
1021 }
1022 if inWorkspaceMode(loaderstate) && !strings.HasPrefix(cfg.CmdName, "work ") {
1023
1024
1025
1026 mv := gover.FromGoMod(f)
1027 wv := gover.FromGoWork(workFile)
1028 if gover.Compare(mv, wv) > 0 && gover.Compare(mv, gover.GoStrictVersion) >= 0 {
1029 errs = append(errs, errWorkTooOld(gomod, workFile, mv))
1030 continue
1031 }
1032 }
1033
1034 if !inWorkspaceMode(loaderstate) {
1035 ok := true
1036 for _, g := range f.Godebug {
1037 if err := CheckGodebug("godebug", g.Key, g.Value); err != nil {
1038 errs = append(errs, fmt.Errorf("error loading go.mod:\n%s:%d: %v", base.ShortPath(gomod), g.Syntax.Start.Line, err))
1039 ok = false
1040 }
1041 }
1042 if !ok {
1043 continue
1044 }
1045 }
1046
1047 modFiles = append(modFiles, f)
1048 mainModule := f.Module.Mod
1049 mainModules = append(mainModules, mainModule)
1050 indices = append(indices, indexModFile(data, f, mainModule, fixed))
1051
1052 if err := module.CheckImportPath(f.Module.Mod.Path); err != nil {
1053 if pathErr, ok := err.(*module.InvalidPathError); ok {
1054 pathErr.Kind = "module"
1055 }
1056 errs = append(errs, err)
1057 }
1058 }
1059 if len(errs) > 0 {
1060 return nil, errors.Join(errs...)
1061 }
1062
1063 loaderstate.MainModules = makeMainModules(loaderstate, mainModules, loaderstate.modRoots, modFiles, indices, workFile)
1064 setDefaultBuildMod(loaderstate)
1065 rs := requirementsFromModFiles(loaderstate, ctx, workFile, modFiles, opts)
1066
1067 if cfg.BuildMod == "vendor" {
1068 readVendorList(VendorDir(loaderstate))
1069 versions := loaderstate.MainModules.Versions()
1070 indexes := make([]*modFileIndex, 0, len(versions))
1071 modFiles := make([]*modfile.File, 0, len(versions))
1072 modRoots := make([]string, 0, len(versions))
1073 for _, m := range versions {
1074 indexes = append(indexes, loaderstate.MainModules.Index(m))
1075 modFiles = append(modFiles, loaderstate.MainModules.ModFile(m))
1076 modRoots = append(modRoots, loaderstate.MainModules.ModRoot(m))
1077 }
1078 checkVendorConsistency(loaderstate, indexes, modFiles, modRoots)
1079 rs.initVendor(loaderstate, vendorList)
1080 }
1081
1082 if inWorkspaceMode(loaderstate) {
1083
1084 loaderstate.requirements = rs
1085 return rs, nil
1086 }
1087
1088 mainModule := loaderstate.MainModules.mustGetSingleMainModule(loaderstate)
1089
1090 if rs.hasRedundantRoot(loaderstate) {
1091
1092
1093
1094 var err error
1095 rs, err = updateRoots(loaderstate, ctx, rs.direct, rs, nil, nil, false)
1096 if err != nil {
1097 return nil, err
1098 }
1099 }
1100
1101 if loaderstate.MainModules.Index(mainModule).goVersion == "" && rs.pruning != workspace {
1102
1103
1104 if cfg.BuildMod == "mod" && cfg.CmdName != "mod graph" && cfg.CmdName != "mod why" {
1105
1106 v := gover.Local()
1107 if opts != nil && opts.TidyGoVersion != "" {
1108 v = opts.TidyGoVersion
1109 }
1110 addGoStmt(loaderstate.MainModules.ModFile(mainModule), mainModule, v)
1111 rs = overrideRoots(loaderstate, ctx, rs, []module.Version{{Path: "go", Version: v}})
1112
1113
1114
1115
1116
1117
1118 if gover.Compare(v, gover.ExplicitIndirectVersion) >= 0 {
1119 var err error
1120 rs, err = convertPruning(loaderstate, ctx, rs, pruned)
1121 if err != nil {
1122 return nil, err
1123 }
1124 }
1125 } else {
1126 rawGoVersion.Store(mainModule, gover.DefaultGoModVersion)
1127 }
1128 }
1129
1130 loaderstate.requirements = rs
1131 return loaderstate.requirements, nil
1132 }
1133
1134 func errWorkTooOld(gomod string, wf *modfile.WorkFile, goVers string) error {
1135 verb := "lists"
1136 if wf == nil || wf.Go == nil {
1137
1138
1139 verb = "implicitly requires"
1140 }
1141 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",
1142 base.ShortPath(filepath.Dir(gomod)), goVers, verb, gover.FromGoWork(wf))
1143 }
1144
1145
1146
1147 func CheckReservedModulePath(path string) error {
1148 if gover.IsToolchain(path) {
1149 return errors.New("module path is reserved")
1150 }
1151
1152 return nil
1153 }
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164 func CreateModFile(loaderstate *State, ctx context.Context, modPath string) {
1165 modRoot := base.Cwd()
1166 loaderstate.modRoots = []string{modRoot}
1167 Init(loaderstate)
1168 modFilePath := modFilePath(modRoot)
1169 if _, err := fsys.Stat(modFilePath); err == nil {
1170 base.Fatalf("go: %s already exists", modFilePath)
1171 }
1172
1173 if modPath == "" {
1174 var err error
1175 modPath, err = findModulePath(modRoot)
1176 if err != nil {
1177 base.Fatal(err)
1178 }
1179 } else if err := module.CheckImportPath(modPath); err != nil {
1180 if pathErr, ok := err.(*module.InvalidPathError); ok {
1181 pathErr.Kind = "module"
1182
1183 if pathErr.Path == "." || pathErr.Path == ".." ||
1184 strings.HasPrefix(pathErr.Path, "./") || strings.HasPrefix(pathErr.Path, "../") {
1185 pathErr.Err = errors.New("is a local import path")
1186 }
1187 }
1188 base.Fatal(err)
1189 } else if err := CheckReservedModulePath(modPath); err != nil {
1190 base.Fatalf(`go: invalid module path %q: `, modPath)
1191 } else if _, _, ok := module.SplitPathVersion(modPath); !ok {
1192 if strings.HasPrefix(modPath, "gopkg.in/") {
1193 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))
1194 base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg)
1195 }
1196 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))
1197 base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg)
1198 }
1199
1200 fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", modPath)
1201 modFile := new(modfile.File)
1202 modFile.AddModuleStmt(modPath)
1203 loaderstate.MainModules = makeMainModules(loaderstate, []module.Version{modFile.Module.Mod}, []string{modRoot}, []*modfile.File{modFile}, []*modFileIndex{nil}, nil)
1204 addGoStmt(modFile, modFile.Module.Mod, gover.Local())
1205
1206 rs := requirementsFromModFiles(loaderstate, ctx, nil, []*modfile.File{modFile}, nil)
1207 rs, err := updateRoots(loaderstate, ctx, rs.direct, rs, nil, nil, false)
1208 if err != nil {
1209 base.Fatal(err)
1210 }
1211 loaderstate.requirements = rs
1212 if err := commitRequirements(loaderstate, ctx, WriteOpts{}); err != nil {
1213 base.Fatal(err)
1214 }
1215
1216
1217
1218
1219
1220
1221
1222
1223 empty := true
1224 files, _ := os.ReadDir(modRoot)
1225 for _, f := range files {
1226 name := f.Name()
1227 if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
1228 continue
1229 }
1230 if strings.HasSuffix(name, ".go") || f.IsDir() {
1231 empty = false
1232 break
1233 }
1234 }
1235 if !empty {
1236 fmt.Fprintf(os.Stderr, "go: to add module requirements and sums:\n\tgo mod tidy\n")
1237 }
1238 }
1239
1240
1241
1242
1243
1244
1245
1246
1247 func fixVersion(loaderstate *State, ctx context.Context, fixed *bool) modfile.VersionFixer {
1248 return func(path, vers string) (resolved string, err error) {
1249 defer func() {
1250 if err == nil && resolved != vers {
1251 *fixed = true
1252 }
1253 }()
1254
1255
1256 if strings.HasPrefix(path, "gopkg.in/") && strings.Contains(vers, "-gopkgin-") {
1257 vers = vers[strings.Index(vers, "-gopkgin-")+len("-gopkgin-"):]
1258 }
1259
1260
1261
1262
1263 _, pathMajor, ok := module.SplitPathVersion(path)
1264 if !ok {
1265 return "", &module.ModuleError{
1266 Path: path,
1267 Err: &module.InvalidVersionError{
1268 Version: vers,
1269 Err: fmt.Errorf("malformed module path %q", path),
1270 },
1271 }
1272 }
1273 if vers != "" && module.CanonicalVersion(vers) == vers {
1274 if err := module.CheckPathMajor(vers, pathMajor); err != nil {
1275 return "", module.VersionError(module.Version{Path: path, Version: vers}, err)
1276 }
1277 return vers, nil
1278 }
1279
1280 info, err := Query(loaderstate, ctx, path, vers, "", nil)
1281 if err != nil {
1282 return "", err
1283 }
1284 return info.Version, nil
1285 }
1286 }
1287
1288
1289
1290
1291
1292
1293
1294
1295 func AllowMissingModuleImports(loaderstate *State) {
1296 if loaderstate.initialized {
1297 panic("AllowMissingModuleImports after Init")
1298 }
1299 allowMissingModuleImports = true
1300 }
1301
1302
1303
1304 func makeMainModules(loaderstate *State, ms []module.Version, rootDirs []string, modFiles []*modfile.File, indices []*modFileIndex, workFile *modfile.WorkFile) *MainModuleSet {
1305 for _, m := range ms {
1306 if m.Version != "" {
1307 panic("mainModulesCalled with module.Version with non empty Version field: " + fmt.Sprintf("%#v", m))
1308 }
1309 }
1310 modRootContainingCWD := findModuleRoot(base.Cwd())
1311 mainModules := &MainModuleSet{
1312 versions: slices.Clip(ms),
1313 inGorootSrc: map[module.Version]bool{},
1314 pathPrefix: map[module.Version]string{},
1315 modRoot: map[module.Version]string{},
1316 modFiles: map[module.Version]*modfile.File{},
1317 indices: map[module.Version]*modFileIndex{},
1318 highestReplaced: map[string]string{},
1319 tools: map[string]bool{},
1320 workFile: workFile,
1321 }
1322 var workFileReplaces []*modfile.Replace
1323 if workFile != nil {
1324 workFileReplaces = workFile.Replace
1325 mainModules.workFileReplaceMap = toReplaceMap(workFile.Replace)
1326 }
1327 mainModulePaths := make(map[string]bool)
1328 for _, m := range ms {
1329 if mainModulePaths[m.Path] {
1330 base.Errorf("go: module %s appears multiple times in workspace", m.Path)
1331 }
1332 mainModulePaths[m.Path] = true
1333 }
1334 replacedByWorkFile := make(map[string]bool)
1335 replacements := make(map[module.Version]module.Version)
1336 for _, r := range workFileReplaces {
1337 if mainModulePaths[r.Old.Path] && r.Old.Version == "" {
1338 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)
1339 }
1340 replacedByWorkFile[r.Old.Path] = true
1341 v, ok := mainModules.highestReplaced[r.Old.Path]
1342 if !ok || gover.ModCompare(r.Old.Path, r.Old.Version, v) > 0 {
1343 mainModules.highestReplaced[r.Old.Path] = r.Old.Version
1344 }
1345 replacements[r.Old] = r.New
1346 }
1347 for i, m := range ms {
1348 mainModules.pathPrefix[m] = m.Path
1349 mainModules.modRoot[m] = rootDirs[i]
1350 mainModules.modFiles[m] = modFiles[i]
1351 mainModules.indices[m] = indices[i]
1352
1353 if mainModules.modRoot[m] == modRootContainingCWD {
1354 mainModules.modContainingCWD = m
1355 }
1356
1357 if rel := search.InDir(rootDirs[i], cfg.GOROOTsrc); rel != "" {
1358 mainModules.inGorootSrc[m] = true
1359 if m.Path == "std" {
1360
1361
1362
1363
1364
1365
1366
1367
1368 mainModules.pathPrefix[m] = ""
1369 }
1370 }
1371
1372 if modFiles[i] != nil {
1373 curModuleReplaces := make(map[module.Version]bool)
1374 for _, r := range modFiles[i].Replace {
1375 if replacedByWorkFile[r.Old.Path] {
1376 continue
1377 }
1378 var newV module.Version = r.New
1379 if WorkFilePath(loaderstate) != "" && newV.Version == "" && !filepath.IsAbs(newV.Path) {
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389 newV.Path = filepath.Join(rootDirs[i], newV.Path)
1390 }
1391 if prev, ok := replacements[r.Old]; ok && !curModuleReplaces[r.Old] && prev != newV {
1392 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)
1393 }
1394 curModuleReplaces[r.Old] = true
1395 replacements[r.Old] = newV
1396
1397 v, ok := mainModules.highestReplaced[r.Old.Path]
1398 if !ok || gover.ModCompare(r.Old.Path, r.Old.Version, v) > 0 {
1399 mainModules.highestReplaced[r.Old.Path] = r.Old.Version
1400 }
1401 }
1402
1403 for _, t := range modFiles[i].Tool {
1404 if err := module.CheckImportPath(t.Path); err != nil {
1405 if e, ok := err.(*module.InvalidPathError); ok {
1406 e.Kind = "tool"
1407 }
1408 base.Fatal(err)
1409 }
1410
1411 mainModules.tools[t.Path] = true
1412 }
1413 }
1414 }
1415
1416 return mainModules
1417 }
1418
1419
1420
1421 func requirementsFromModFiles(loaderstate *State, ctx context.Context, workFile *modfile.WorkFile, modFiles []*modfile.File, opts *PackageOpts) *Requirements {
1422 var roots []module.Version
1423 direct := map[string]bool{}
1424 var pruning modPruning
1425 if inWorkspaceMode(loaderstate) {
1426 pruning = workspace
1427 roots = make([]module.Version, len(loaderstate.MainModules.Versions()), 2+len(loaderstate.MainModules.Versions()))
1428 copy(roots, loaderstate.MainModules.Versions())
1429 goVersion := gover.FromGoWork(workFile)
1430 var toolchain string
1431 if workFile.Toolchain != nil {
1432 toolchain = workFile.Toolchain.Name
1433 }
1434 roots = appendGoAndToolchainRoots(roots, goVersion, toolchain, direct)
1435 direct = directRequirements(modFiles)
1436 } else {
1437 pruning = pruningForGoVersion(loaderstate.MainModules.GoVersion(loaderstate))
1438 if len(modFiles) != 1 {
1439 panic(fmt.Errorf("requirementsFromModFiles called with %v modfiles outside workspace mode", len(modFiles)))
1440 }
1441 modFile := modFiles[0]
1442 roots, direct = rootsFromModFile(loaderstate, loaderstate.MainModules.mustGetSingleMainModule(loaderstate), modFile, withToolchainRoot)
1443 }
1444
1445 gover.ModSort(roots)
1446 rs := newRequirements(loaderstate, pruning, roots, direct)
1447 return rs
1448 }
1449
1450 type addToolchainRoot bool
1451
1452 const (
1453 omitToolchainRoot addToolchainRoot = false
1454 withToolchainRoot = true
1455 )
1456
1457 func directRequirements(modFiles []*modfile.File) map[string]bool {
1458 direct := make(map[string]bool)
1459 for _, modFile := range modFiles {
1460 for _, r := range modFile.Require {
1461 if !r.Indirect {
1462 direct[r.Mod.Path] = true
1463 }
1464 }
1465 }
1466 return direct
1467 }
1468
1469 func rootsFromModFile(loaderstate *State, m module.Version, modFile *modfile.File, addToolchainRoot addToolchainRoot) (roots []module.Version, direct map[string]bool) {
1470 direct = make(map[string]bool)
1471 padding := 2
1472 if !addToolchainRoot {
1473 padding = 1
1474 }
1475 roots = make([]module.Version, 0, padding+len(modFile.Require))
1476 for _, r := range modFile.Require {
1477 if index := loaderstate.MainModules.Index(m); index != nil && index.exclude[r.Mod] {
1478 if cfg.BuildMod == "mod" {
1479 fmt.Fprintf(os.Stderr, "go: dropping requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
1480 } else {
1481 fmt.Fprintf(os.Stderr, "go: ignoring requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
1482 }
1483 continue
1484 }
1485
1486 roots = append(roots, r.Mod)
1487 if !r.Indirect {
1488 direct[r.Mod.Path] = true
1489 }
1490 }
1491 goVersion := gover.FromGoMod(modFile)
1492 var toolchain string
1493 if addToolchainRoot && modFile.Toolchain != nil {
1494 toolchain = modFile.Toolchain.Name
1495 }
1496 roots = appendGoAndToolchainRoots(roots, goVersion, toolchain, direct)
1497 return roots, direct
1498 }
1499
1500 func appendGoAndToolchainRoots(roots []module.Version, goVersion, toolchain string, direct map[string]bool) []module.Version {
1501
1502 roots = append(roots, module.Version{Path: "go", Version: goVersion})
1503 direct["go"] = true
1504
1505 if toolchain != "" {
1506 roots = append(roots, module.Version{Path: "toolchain", Version: toolchain})
1507
1508
1509
1510
1511
1512 }
1513 return roots
1514 }
1515
1516
1517
1518 func setDefaultBuildMod(loaderstate *State) {
1519 if cfg.BuildModExplicit {
1520 if inWorkspaceMode(loaderstate) && cfg.BuildMod != "readonly" && cfg.BuildMod != "vendor" {
1521 switch cfg.CmdName {
1522 case "work sync", "mod graph", "mod verify", "mod why":
1523
1524
1525 panic("in workspace mode and -mod was set explicitly, but command doesn't support setting -mod")
1526 default:
1527 base.Fatalf("go: -mod may only be set to readonly or vendor when in workspace mode, but it is set to %q"+
1528 "\n\tRemove the -mod flag to use the default readonly value, "+
1529 "\n\tor set GOWORK=off to disable workspace mode.", cfg.BuildMod)
1530 }
1531 }
1532
1533 return
1534 }
1535
1536
1537
1538
1539 switch cfg.CmdName {
1540 case "get", "mod download", "mod init", "mod tidy", "work sync":
1541
1542 cfg.BuildMod = "mod"
1543 return
1544 case "mod graph", "mod verify", "mod why":
1545
1546
1547
1548
1549 cfg.BuildMod = "mod"
1550 return
1551 case "mod vendor", "work vendor":
1552 cfg.BuildMod = "readonly"
1553 return
1554 }
1555 if loaderstate.modRoots == nil {
1556 if allowMissingModuleImports {
1557 cfg.BuildMod = "mod"
1558 } else {
1559 cfg.BuildMod = "readonly"
1560 }
1561 return
1562 }
1563
1564 if len(loaderstate.modRoots) >= 1 {
1565 var goVersion string
1566 var versionSource string
1567 if inWorkspaceMode(loaderstate) {
1568 versionSource = "go.work"
1569 if wfg := loaderstate.MainModules.WorkFile().Go; wfg != nil {
1570 goVersion = wfg.Version
1571 }
1572 } else {
1573 versionSource = "go.mod"
1574 index := loaderstate.MainModules.GetSingleIndexOrNil(loaderstate)
1575 if index != nil {
1576 goVersion = index.goVersion
1577 }
1578 }
1579 vendorDir := ""
1580 if loaderstate.workFilePath != "" {
1581 vendorDir = filepath.Join(filepath.Dir(loaderstate.workFilePath), "vendor")
1582 } else {
1583 if len(loaderstate.modRoots) != 1 {
1584 panic(fmt.Errorf("outside workspace mode, but have %v modRoots", loaderstate.modRoots))
1585 }
1586 vendorDir = filepath.Join(loaderstate.modRoots[0], "vendor")
1587 }
1588 if fi, err := fsys.Stat(vendorDir); err == nil && fi.IsDir() {
1589 if goVersion != "" {
1590 if gover.Compare(goVersion, "1.14") < 0 {
1591
1592
1593
1594 cfg.BuildModReason = fmt.Sprintf("Go version in "+versionSource+" is %s, so vendor directory was not used.", goVersion)
1595 } else {
1596 vendoredWorkspace, err := modulesTextIsForWorkspace(vendorDir)
1597 if err != nil {
1598 base.Fatalf("go: reading modules.txt for vendor directory: %v", err)
1599 }
1600 if vendoredWorkspace != (versionSource == "go.work") {
1601 if vendoredWorkspace {
1602 cfg.BuildModReason = "Outside workspace mode, but vendor directory is for a workspace."
1603 } else {
1604 cfg.BuildModReason = "In workspace mode, but vendor directory is not for a workspace"
1605 }
1606 } else {
1607
1608
1609
1610 cfg.BuildMod = "vendor"
1611 cfg.BuildModReason = "Go version in " + versionSource + " is at least 1.14 and vendor directory exists."
1612 return
1613 }
1614 }
1615 } else {
1616 cfg.BuildModReason = fmt.Sprintf("Go version in %s is unspecified, so vendor directory was not used.", versionSource)
1617 }
1618 }
1619 }
1620
1621 cfg.BuildMod = "readonly"
1622 }
1623
1624 func modulesTextIsForWorkspace(vendorDir string) (bool, error) {
1625 f, err := fsys.Open(filepath.Join(vendorDir, "modules.txt"))
1626 if errors.Is(err, os.ErrNotExist) {
1627
1628
1629
1630
1631 return false, nil
1632 }
1633 if err != nil {
1634 return false, err
1635 }
1636 defer f.Close()
1637 var buf [512]byte
1638 n, err := f.Read(buf[:])
1639 if err != nil && err != io.EOF {
1640 return false, err
1641 }
1642 line, _, _ := strings.Cut(string(buf[:n]), "\n")
1643 if annotations, ok := strings.CutPrefix(line, "## "); ok {
1644 for entry := range strings.SplitSeq(annotations, ";") {
1645 entry = strings.TrimSpace(entry)
1646 if entry == "workspace" {
1647 return true, nil
1648 }
1649 }
1650 }
1651 return false, nil
1652 }
1653
1654 func mustHaveCompleteRequirements(loaderstate *State) bool {
1655 return cfg.BuildMod != "mod" && !inWorkspaceMode(loaderstate)
1656 }
1657
1658
1659
1660
1661 func addGoStmt(modFile *modfile.File, mod module.Version, v string) {
1662 if modFile.Go != nil && modFile.Go.Version != "" {
1663 return
1664 }
1665 forceGoStmt(modFile, mod, v)
1666 }
1667
1668 func forceGoStmt(modFile *modfile.File, mod module.Version, v string) {
1669 if err := modFile.AddGoStmt(v); err != nil {
1670 base.Fatalf("go: internal error: %v", err)
1671 }
1672 rawGoVersion.Store(mod, v)
1673 }
1674
1675 var altConfigs = []string{
1676 ".git/config",
1677 }
1678
1679 func findModuleRoot(dir string) (roots string) {
1680 if dir == "" {
1681 panic("dir not set")
1682 }
1683 dir = filepath.Clean(dir)
1684
1685
1686 for {
1687 if fi, err := fsys.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {
1688 return dir
1689 }
1690 d := filepath.Dir(dir)
1691 if d == dir {
1692 break
1693 }
1694 dir = d
1695 }
1696 return ""
1697 }
1698
1699 func findWorkspaceFile(dir string) (root string) {
1700 if dir == "" {
1701 panic("dir not set")
1702 }
1703 dir = filepath.Clean(dir)
1704
1705
1706 for {
1707 f := filepath.Join(dir, "go.work")
1708 if fi, err := fsys.Stat(f); err == nil && !fi.IsDir() {
1709 return f
1710 }
1711 d := filepath.Dir(dir)
1712 if d == dir {
1713 break
1714 }
1715 if d == cfg.GOROOT {
1716
1717
1718
1719 return ""
1720 }
1721 dir = d
1722 }
1723 return ""
1724 }
1725
1726 func findAltConfig(dir string) (root, name string) {
1727 if dir == "" {
1728 panic("dir not set")
1729 }
1730 dir = filepath.Clean(dir)
1731 if rel := search.InDir(dir, cfg.BuildContext.GOROOT); rel != "" {
1732
1733
1734 return "", ""
1735 }
1736 for {
1737 for _, name := range altConfigs {
1738 if fi, err := fsys.Stat(filepath.Join(dir, name)); err == nil && !fi.IsDir() {
1739 return dir, name
1740 }
1741 }
1742 d := filepath.Dir(dir)
1743 if d == dir {
1744 break
1745 }
1746 dir = d
1747 }
1748 return "", ""
1749 }
1750
1751 func findModulePath(dir string) (string, error) {
1752
1753
1754
1755
1756
1757
1758
1759
1760 list, _ := os.ReadDir(dir)
1761 for _, info := range list {
1762 if info.Type().IsRegular() && strings.HasSuffix(info.Name(), ".go") {
1763 if com := findImportComment(filepath.Join(dir, info.Name())); com != "" {
1764 return com, nil
1765 }
1766 }
1767 }
1768 for _, info1 := range list {
1769 if info1.IsDir() {
1770 files, _ := os.ReadDir(filepath.Join(dir, info1.Name()))
1771 for _, info2 := range files {
1772 if info2.Type().IsRegular() && strings.HasSuffix(info2.Name(), ".go") {
1773 if com := findImportComment(filepath.Join(dir, info1.Name(), info2.Name())); com != "" {
1774 return path.Dir(com), nil
1775 }
1776 }
1777 }
1778 }
1779 }
1780
1781
1782 var badPathErr error
1783 for _, gpdir := range filepath.SplitList(cfg.BuildContext.GOPATH) {
1784 if gpdir == "" {
1785 continue
1786 }
1787 if rel := search.InDir(dir, filepath.Join(gpdir, "src")); rel != "" && rel != "." {
1788 path := filepath.ToSlash(rel)
1789
1790 if err := module.CheckImportPath(path); err != nil {
1791 badPathErr = err
1792 break
1793 }
1794 return path, nil
1795 }
1796 }
1797
1798 reason := "outside GOPATH, module path must be specified"
1799 if badPathErr != nil {
1800
1801
1802 reason = fmt.Sprintf("bad module path inferred from directory in GOPATH: %v", badPathErr)
1803 }
1804 msg := `cannot determine module path for source directory %s (%s)
1805
1806 Example usage:
1807 'go mod init example.com/m' to initialize a v0 or v1 module
1808 'go mod init example.com/m/v2' to initialize a v2 module
1809
1810 Run 'go help mod init' for more information.
1811 `
1812 return "", fmt.Errorf(msg, dir, reason)
1813 }
1814
1815 var (
1816 importCommentRE = lazyregexp.New(`(?m)^package[ \t]+[^ \t\r\n/]+[ \t]+//[ \t]+import[ \t]+(\"[^"]+\")[ \t]*\r?\n`)
1817 )
1818
1819 func findImportComment(file string) string {
1820 data, err := os.ReadFile(file)
1821 if err != nil {
1822 return ""
1823 }
1824 m := importCommentRE.FindSubmatch(data)
1825 if m == nil {
1826 return ""
1827 }
1828 path, err := strconv.Unquote(string(m[1]))
1829 if err != nil {
1830 return ""
1831 }
1832 return path
1833 }
1834
1835
1836 type WriteOpts struct {
1837 DropToolchain bool
1838 ExplicitToolchain bool
1839
1840 AddTools []string
1841 DropTools []string
1842
1843
1844
1845 TidyWroteGo bool
1846 }
1847
1848
1849 func WriteGoMod(loaderstate *State, ctx context.Context, opts WriteOpts) error {
1850 loaderstate.requirements = LoadModFile(loaderstate, ctx)
1851 return commitRequirements(loaderstate, ctx, opts)
1852 }
1853
1854 var errNoChange = errors.New("no update needed")
1855
1856
1857
1858 func UpdateGoModFromReqs(loaderstate *State, ctx context.Context, opts WriteOpts) (before, after []byte, modFile *modfile.File, err error) {
1859 if loaderstate.MainModules.Len() != 1 || loaderstate.MainModules.ModRoot(loaderstate.MainModules.Versions()[0]) == "" {
1860
1861 return nil, nil, nil, errNoChange
1862 }
1863 mainModule := loaderstate.MainModules.mustGetSingleMainModule(loaderstate)
1864 modFile = loaderstate.MainModules.ModFile(mainModule)
1865 if modFile == nil {
1866
1867 return nil, nil, nil, errNoChange
1868 }
1869 before, err = modFile.Format()
1870 if err != nil {
1871 return nil, nil, nil, err
1872 }
1873
1874 var list []*modfile.Require
1875 toolchain := ""
1876 goVersion := ""
1877 for _, m := range loaderstate.requirements.rootModules {
1878 if m.Path == "go" {
1879 goVersion = m.Version
1880 continue
1881 }
1882 if m.Path == "toolchain" {
1883 toolchain = m.Version
1884 continue
1885 }
1886 list = append(list, &modfile.Require{
1887 Mod: m,
1888 Indirect: !loaderstate.requirements.direct[m.Path],
1889 })
1890 }
1891
1892
1893
1894
1895 if goVersion == "" {
1896 base.Fatalf("go: internal error: missing go root module in WriteGoMod")
1897 }
1898 if gover.Compare(goVersion, gover.Local()) > 0 {
1899
1900 return nil, nil, nil, &gover.TooNewError{What: "updating go.mod", GoVersion: goVersion}
1901 }
1902 wroteGo := opts.TidyWroteGo
1903 if !wroteGo && modFile.Go == nil || modFile.Go.Version != goVersion {
1904 alwaysUpdate := cfg.BuildMod == "mod" || cfg.CmdName == "mod tidy" || cfg.CmdName == "get"
1905 if modFile.Go == nil && goVersion == gover.DefaultGoModVersion && !alwaysUpdate {
1906
1907
1908
1909 } else {
1910 wroteGo = true
1911 forceGoStmt(modFile, mainModule, goVersion)
1912 }
1913 }
1914 if toolchain == "" {
1915 toolchain = "go" + goVersion
1916 }
1917
1918 toolVers := gover.FromToolchain(toolchain)
1919 if opts.DropToolchain || toolchain == "go"+goVersion || (gover.Compare(toolVers, gover.GoStrictVersion) < 0 && !opts.ExplicitToolchain) {
1920
1921
1922 modFile.DropToolchainStmt()
1923 } else {
1924 modFile.AddToolchainStmt(toolchain)
1925 }
1926
1927 for _, path := range opts.AddTools {
1928 modFile.AddTool(path)
1929 }
1930
1931 for _, path := range opts.DropTools {
1932 modFile.DropTool(path)
1933 }
1934
1935
1936 if gover.Compare(goVersion, gover.SeparateIndirectVersion) < 0 {
1937 modFile.SetRequire(list)
1938 } else {
1939 modFile.SetRequireSeparateIndirect(list)
1940 }
1941 modFile.Cleanup()
1942 after, err = modFile.Format()
1943 if err != nil {
1944 return nil, nil, nil, err
1945 }
1946 return before, after, modFile, nil
1947 }
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958 func commitRequirements(loaderstate *State, ctx context.Context, opts WriteOpts) (err error) {
1959 if inWorkspaceMode(loaderstate) {
1960
1961
1962 return modfetch.WriteGoSum(ctx, keepSums(loaderstate, ctx, loaded, loaderstate.requirements, addBuildListZipSums), mustHaveCompleteRequirements(loaderstate))
1963 }
1964 _, updatedGoMod, modFile, err := UpdateGoModFromReqs(loaderstate, ctx, opts)
1965 if err != nil {
1966 if errors.Is(err, errNoChange) {
1967 return nil
1968 }
1969 return err
1970 }
1971
1972 index := loaderstate.MainModules.GetSingleIndexOrNil(loaderstate)
1973 dirty := index.modFileIsDirty(modFile) || len(opts.DropTools) > 0 || len(opts.AddTools) > 0
1974 if dirty && cfg.BuildMod != "mod" {
1975
1976
1977 return errGoModDirty
1978 }
1979
1980 if !dirty && cfg.CmdName != "mod tidy" {
1981
1982
1983
1984
1985 if cfg.CmdName != "mod init" {
1986 if err := modfetch.WriteGoSum(ctx, keepSums(loaderstate, ctx, loaded, loaderstate.requirements, addBuildListZipSums), mustHaveCompleteRequirements(loaderstate)); err != nil {
1987 return err
1988 }
1989 }
1990 return nil
1991 }
1992
1993 mainModule := loaderstate.MainModules.mustGetSingleMainModule(loaderstate)
1994 modFilePath := modFilePath(loaderstate.MainModules.ModRoot(mainModule))
1995 if fsys.Replaced(modFilePath) {
1996 if dirty {
1997 return errors.New("updates to go.mod needed, but go.mod is part of the overlay specified with -overlay")
1998 }
1999 return nil
2000 }
2001 defer func() {
2002
2003 loaderstate.MainModules.SetIndex(mainModule, indexModFile(updatedGoMod, modFile, mainModule, false))
2004
2005
2006
2007 if cfg.CmdName != "mod init" {
2008 if err == nil {
2009 err = modfetch.WriteGoSum(ctx, keepSums(loaderstate, ctx, loaded, loaderstate.requirements, addBuildListZipSums), mustHaveCompleteRequirements(loaderstate))
2010 }
2011 }
2012 }()
2013
2014
2015
2016 if unlock, err := modfetch.SideLock(ctx); err == nil {
2017 defer unlock()
2018 }
2019
2020 err = lockedfile.Transform(modFilePath, func(old []byte) ([]byte, error) {
2021 if bytes.Equal(old, updatedGoMod) {
2022
2023
2024 return nil, errNoChange
2025 }
2026
2027 if index != nil && !bytes.Equal(old, index.data) {
2028
2029
2030
2031
2032
2033
2034 return nil, fmt.Errorf("existing contents have changed since last read")
2035 }
2036
2037 return updatedGoMod, nil
2038 })
2039
2040 if err != nil && err != errNoChange {
2041 return fmt.Errorf("updating go.mod: %w", err)
2042 }
2043 return nil
2044 }
2045
2046
2047
2048
2049
2050
2051
2052 func keepSums(loaderstate *State, ctx context.Context, ld *loader, rs *Requirements, which whichSums) map[module.Version]bool {
2053
2054
2055
2056
2057 keep := make(map[module.Version]bool)
2058
2059
2060
2061
2062
2063 keepModSumsForZipSums := true
2064 if ld == nil {
2065 if gover.Compare(loaderstate.MainModules.GoVersion(loaderstate), gover.TidyGoModSumVersion) < 0 && cfg.BuildMod != "mod" {
2066 keepModSumsForZipSums = false
2067 }
2068 } else {
2069 keepPkgGoModSums := true
2070 if gover.Compare(ld.requirements.GoVersion(loaderstate), gover.TidyGoModSumVersion) < 0 && (ld.Tidy || cfg.BuildMod != "mod") {
2071 keepPkgGoModSums = false
2072 keepModSumsForZipSums = false
2073 }
2074 for _, pkg := range ld.pkgs {
2075
2076
2077
2078 if pkg.testOf != nil || (pkg.mod.Path == "" && pkg.err == nil) || module.CheckImportPath(pkg.path) != nil {
2079 continue
2080 }
2081
2082
2083
2084
2085
2086
2087 if keepPkgGoModSums {
2088 r := resolveReplacement(loaderstate, pkg.mod)
2089 keep[modkey(r)] = true
2090 }
2091
2092 if rs.pruning == pruned && pkg.mod.Path != "" {
2093 if v, ok := rs.rootSelected(loaderstate, pkg.mod.Path); ok && v == pkg.mod.Version {
2094
2095
2096
2097
2098
2099 for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) {
2100 if v, ok := rs.rootSelected(loaderstate, prefix); ok && v != "none" {
2101 m := module.Version{Path: prefix, Version: v}
2102 r := resolveReplacement(loaderstate, m)
2103 keep[r] = true
2104 }
2105 }
2106 continue
2107 }
2108 }
2109
2110 mg, _ := rs.Graph(loaderstate, ctx)
2111 for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) {
2112 if v := mg.Selected(prefix); v != "none" {
2113 m := module.Version{Path: prefix, Version: v}
2114 r := resolveReplacement(loaderstate, m)
2115 keep[r] = true
2116 }
2117 }
2118 }
2119 }
2120
2121 if rs.graph.Load() == nil {
2122
2123
2124
2125 for _, m := range rs.rootModules {
2126 r := resolveReplacement(loaderstate, m)
2127 keep[modkey(r)] = true
2128 if which == addBuildListZipSums {
2129 keep[r] = true
2130 }
2131 }
2132 } else {
2133 mg, _ := rs.Graph(loaderstate, ctx)
2134 mg.WalkBreadthFirst(func(m module.Version) {
2135 if _, ok := mg.RequiredBy(m); ok {
2136
2137
2138
2139 r := resolveReplacement(loaderstate, m)
2140 keep[modkey(r)] = true
2141 }
2142 })
2143
2144 if which == addBuildListZipSums {
2145 for _, m := range mg.BuildList() {
2146 r := resolveReplacement(loaderstate, m)
2147 if keepModSumsForZipSums {
2148 keep[modkey(r)] = true
2149 }
2150 keep[r] = true
2151 }
2152 }
2153 }
2154
2155 return keep
2156 }
2157
2158 type whichSums int8
2159
2160 const (
2161 loadedZipSumsOnly = whichSums(iota)
2162 addBuildListZipSums
2163 )
2164
2165
2166
2167 func modkey(m module.Version) module.Version {
2168 return module.Version{Path: m.Path, Version: m.Version + "/go.mod"}
2169 }
2170
2171 func suggestModulePath(path string) string {
2172 var m string
2173
2174 i := len(path)
2175 for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9' || path[i-1] == '.') {
2176 i--
2177 }
2178 url := path[:i]
2179 url = strings.TrimSuffix(url, "/v")
2180 url = strings.TrimSuffix(url, "/")
2181
2182 f := func(c rune) bool {
2183 return c > '9' || c < '0'
2184 }
2185 s := strings.FieldsFunc(path[i:], f)
2186 if len(s) > 0 {
2187 m = s[0]
2188 }
2189 m = strings.TrimLeft(m, "0")
2190 if m == "" || m == "1" {
2191 return url + "/v2"
2192 }
2193
2194 return url + "/v" + m
2195 }
2196
2197 func suggestGopkgIn(path string) string {
2198 var m string
2199 i := len(path)
2200 for i > 0 && (('0' <= path[i-1] && path[i-1] <= '9') || (path[i-1] == '.')) {
2201 i--
2202 }
2203 url := path[:i]
2204 url = strings.TrimSuffix(url, ".v")
2205 url = strings.TrimSuffix(url, "/v")
2206 url = strings.TrimSuffix(url, "/")
2207
2208 f := func(c rune) bool {
2209 return c > '9' || c < '0'
2210 }
2211 s := strings.FieldsFunc(path, f)
2212 if len(s) > 0 {
2213 m = s[0]
2214 }
2215
2216 m = strings.TrimLeft(m, "0")
2217
2218 if m == "" {
2219 return url + ".v1"
2220 }
2221 return url + ".v" + m
2222 }
2223
2224 func CheckGodebug(verb, k, v string) error {
2225 if strings.ContainsAny(k, " \t") {
2226 return fmt.Errorf("key contains space")
2227 }
2228 if strings.ContainsAny(v, " \t") {
2229 return fmt.Errorf("value contains space")
2230 }
2231 if strings.ContainsAny(k, ",") {
2232 return fmt.Errorf("key contains comma")
2233 }
2234 if strings.ContainsAny(v, ",") {
2235 return fmt.Errorf("value contains comma")
2236 }
2237 if k == "default" {
2238 if !strings.HasPrefix(v, "go") || !gover.IsValid(v[len("go"):]) {
2239 return fmt.Errorf("value for default= must be goVERSION")
2240 }
2241 if gover.Compare(v[len("go"):], gover.Local()) > 0 {
2242 return fmt.Errorf("default=%s too new (toolchain is go%s)", v, gover.Local())
2243 }
2244 return nil
2245 }
2246 for _, info := range godebugs.All {
2247 if k == info.Name {
2248 return nil
2249 }
2250 }
2251 return fmt.Errorf("unknown %s %q", verb, k)
2252 }
2253
View as plain text