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