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