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