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