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