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