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