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