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 if cfg.ModFile != "" {
1207 fmt.Fprintf(os.Stderr, "go: creating new go.mod (using -modfile path %s): module %s\n", base.ShortPath(modFilePath), modPath)
1208 } else {
1209 fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", modPath)
1210 }
1211 modFile := new(modfile.File)
1212 modFile.AddModuleStmt(modPath)
1213 ld.MainModules = makeMainModules(ld, []module.Version{modFile.Module.Mod}, []string{modRoot}, []*modfile.File{modFile}, []*modFileIndex{nil}, nil)
1214 addGoStmt(modFile, modFile.Module.Mod, gover.Local())
1215
1216 rs := requirementsFromModFiles(ld, ctx, nil, []*modfile.File{modFile}, nil)
1217 rs, err := updateRoots(ld, ctx, rs.direct, rs, nil, nil, false)
1218 if err != nil {
1219 base.Fatal(err)
1220 }
1221 ld.requirements = rs
1222 if err := commitRequirements(ld, ctx, WriteOpts{}); err != nil {
1223 base.Fatal(err)
1224 }
1225
1226
1227
1228
1229
1230
1231
1232
1233 empty := true
1234 files, _ := os.ReadDir(modRoot)
1235 for _, f := range files {
1236 name := f.Name()
1237 if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
1238 continue
1239 }
1240 if strings.HasSuffix(name, ".go") || f.IsDir() {
1241 empty = false
1242 break
1243 }
1244 }
1245 if !empty {
1246 fmt.Fprintf(os.Stderr, "go: to add module requirements and sums:\n\tgo mod tidy\n")
1247 }
1248 }
1249
1250 func checkModulePath(modPath string) {
1251 if err := module.CheckImportPath(modPath); err != nil {
1252 if pathErr, ok := err.(*module.InvalidPathError); ok {
1253 pathErr.Kind = "module"
1254
1255 if pathErr.Path == "." || pathErr.Path == ".." ||
1256 strings.HasPrefix(pathErr.Path, "./") || strings.HasPrefix(pathErr.Path, "../") {
1257 pathErr.Err = errors.New("is a local import path")
1258 }
1259 }
1260 base.Fatal(err)
1261 }
1262 if err := CheckReservedModulePath(modPath); err != nil {
1263 base.Fatalf(`go: invalid module path %q: `, modPath)
1264 }
1265 if _, _, ok := module.SplitPathVersion(modPath); !ok {
1266 if strings.HasPrefix(modPath, "gopkg.in/") {
1267 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))
1268 base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg)
1269 }
1270 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))
1271 base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg)
1272 }
1273 }
1274
1275
1276
1277
1278
1279
1280
1281
1282 func fixVersion(ld *Loader, ctx context.Context, fixed *bool) modfile.VersionFixer {
1283 return func(path, vers string) (resolved string, err error) {
1284 defer func() {
1285 if err == nil && resolved != vers {
1286 *fixed = true
1287 }
1288 }()
1289
1290
1291 if strings.HasPrefix(path, "gopkg.in/") && strings.Contains(vers, "-gopkgin-") {
1292 vers = vers[strings.Index(vers, "-gopkgin-")+len("-gopkgin-"):]
1293 }
1294
1295
1296
1297
1298 _, pathMajor, ok := module.SplitPathVersion(path)
1299 if !ok {
1300 return "", &module.ModuleError{
1301 Path: path,
1302 Err: &module.InvalidVersionError{
1303 Version: vers,
1304 Err: fmt.Errorf("malformed module path %q", path),
1305 },
1306 }
1307 }
1308 if vers != "" && module.CanonicalVersion(vers) == vers {
1309 if err := module.CheckPathMajor(vers, pathMajor); err != nil {
1310 return "", module.VersionError(module.Version{Path: path, Version: vers}, err)
1311 }
1312 return vers, nil
1313 }
1314
1315 info, err := Query(ld, ctx, path, vers, "", nil)
1316 if err != nil {
1317 return "", err
1318 }
1319 return info.Version, nil
1320 }
1321 }
1322
1323
1324
1325
1326
1327
1328
1329
1330 func (ld *Loader) AllowMissingModuleImports() {
1331 if ld.initialized {
1332 panic("AllowMissingModuleImports after Init")
1333 }
1334 ld.allowMissingModuleImports = true
1335 }
1336
1337
1338
1339 func makeMainModules(ld *Loader, ms []module.Version, rootDirs []string, modFiles []*modfile.File, indices []*modFileIndex, workFile *modfile.WorkFile) *MainModuleSet {
1340 for _, m := range ms {
1341 if m.Version != "" {
1342 panic("mainModulesCalled with module.Version with non empty Version field: " + fmt.Sprintf("%#v", m))
1343 }
1344 }
1345 modRootContainingCWD := findModuleRoot(base.Cwd())
1346 mainModules := &MainModuleSet{
1347 versions: slices.Clip(ms),
1348 inGorootSrc: map[module.Version]bool{},
1349 pathPrefix: map[module.Version]string{},
1350 modRoot: map[module.Version]string{},
1351 modFiles: map[module.Version]*modfile.File{},
1352 indices: map[module.Version]*modFileIndex{},
1353 highestReplaced: map[string]string{},
1354 tools: map[string]bool{},
1355 workFile: workFile,
1356 }
1357 var workFileReplaces []*modfile.Replace
1358 if workFile != nil {
1359 workFileReplaces = workFile.Replace
1360 mainModules.workFileReplaceMap = toReplaceMap(workFile.Replace)
1361 }
1362 mainModulePaths := make(map[string]bool)
1363 for _, m := range ms {
1364 if mainModulePaths[m.Path] {
1365 base.Errorf("go: module %s appears multiple times in workspace", m.Path)
1366 }
1367 mainModulePaths[m.Path] = true
1368 }
1369 replacedByWorkFile := make(map[string]bool)
1370 replacements := make(map[module.Version]module.Version)
1371 for _, r := range workFileReplaces {
1372 if mainModulePaths[r.Old.Path] && r.Old.Version == "" {
1373 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)
1374 }
1375 replacedByWorkFile[r.Old.Path] = true
1376 v, ok := mainModules.highestReplaced[r.Old.Path]
1377 if !ok || gover.ModCompare(r.Old.Path, r.Old.Version, v) > 0 {
1378 mainModules.highestReplaced[r.Old.Path] = r.Old.Version
1379 }
1380 replacements[r.Old] = r.New
1381 }
1382 for i, m := range ms {
1383 mainModules.pathPrefix[m] = m.Path
1384 mainModules.modRoot[m] = rootDirs[i]
1385 mainModules.modFiles[m] = modFiles[i]
1386 mainModules.indices[m] = indices[i]
1387
1388 if mainModules.modRoot[m] == modRootContainingCWD {
1389 mainModules.modContainingCWD = m
1390 }
1391
1392 if rel := search.InDir(rootDirs[i], cfg.GOROOTsrc); rel != "" {
1393 mainModules.inGorootSrc[m] = true
1394 if m.Path == "std" {
1395
1396
1397
1398
1399
1400
1401
1402
1403 mainModules.pathPrefix[m] = ""
1404 }
1405 }
1406
1407 if modFiles[i] != nil {
1408 curModuleReplaces := make(map[module.Version]bool)
1409 for _, r := range modFiles[i].Replace {
1410 if replacedByWorkFile[r.Old.Path] {
1411 continue
1412 }
1413 var newV module.Version = r.New
1414 if WorkFilePath(ld) != "" && newV.Version == "" && !filepath.IsAbs(newV.Path) {
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424 newV.Path = filepath.Join(rootDirs[i], newV.Path)
1425 }
1426 if prev, ok := replacements[r.Old]; ok && !curModuleReplaces[r.Old] && prev != newV {
1427 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)
1428 }
1429 curModuleReplaces[r.Old] = true
1430 replacements[r.Old] = newV
1431
1432 v, ok := mainModules.highestReplaced[r.Old.Path]
1433 if !ok || gover.ModCompare(r.Old.Path, r.Old.Version, v) > 0 {
1434 mainModules.highestReplaced[r.Old.Path] = r.Old.Version
1435 }
1436 }
1437
1438 for _, t := range modFiles[i].Tool {
1439 if err := module.CheckImportPath(t.Path); err != nil {
1440 if e, ok := err.(*module.InvalidPathError); ok {
1441 e.Kind = "tool"
1442 }
1443 base.Fatal(err)
1444 }
1445
1446 mainModules.tools[t.Path] = true
1447 }
1448 }
1449 }
1450
1451 return mainModules
1452 }
1453
1454
1455
1456 func requirementsFromModFiles(ld *Loader, ctx context.Context, workFile *modfile.WorkFile, modFiles []*modfile.File, opts *PackageOpts) *Requirements {
1457 var roots []module.Version
1458 direct := map[string]bool{}
1459 var pruning modPruning
1460 if ld.inWorkspaceMode() {
1461 pruning = workspace
1462 roots = make([]module.Version, len(ld.MainModules.Versions()), 2+len(ld.MainModules.Versions()))
1463 copy(roots, ld.MainModules.Versions())
1464 goVersion := gover.FromGoWork(workFile)
1465 var toolchain string
1466 if workFile.Toolchain != nil {
1467 toolchain = workFile.Toolchain.Name
1468 }
1469 roots = appendGoAndToolchainRoots(roots, goVersion, toolchain, direct)
1470 direct = directRequirements(modFiles)
1471 } else {
1472 pruning = pruningForGoVersion(ld.MainModules.GoVersion(ld))
1473 if len(modFiles) != 1 {
1474 panic(fmt.Errorf("requirementsFromModFiles called with %v modfiles outside workspace mode", len(modFiles)))
1475 }
1476 modFile := modFiles[0]
1477 roots, direct = rootsFromModFile(ld, ld.MainModules.mustGetSingleMainModule(ld), modFile, withToolchainRoot)
1478 }
1479
1480 gover.ModSort(roots)
1481 rs := newRequirements(ld, pruning, roots, direct)
1482 return rs
1483 }
1484
1485 type addToolchainRoot bool
1486
1487 const (
1488 omitToolchainRoot addToolchainRoot = false
1489 withToolchainRoot = true
1490 )
1491
1492 func directRequirements(modFiles []*modfile.File) map[string]bool {
1493 direct := make(map[string]bool)
1494 for _, modFile := range modFiles {
1495 for _, r := range modFile.Require {
1496 if !r.Indirect {
1497 direct[r.Mod.Path] = true
1498 }
1499 }
1500 }
1501 return direct
1502 }
1503
1504 func rootsFromModFile(ld *Loader, m module.Version, modFile *modfile.File, addToolchainRoot addToolchainRoot) (roots []module.Version, direct map[string]bool) {
1505 direct = make(map[string]bool)
1506 padding := 2
1507 if !addToolchainRoot {
1508 padding = 1
1509 }
1510 roots = make([]module.Version, 0, padding+len(modFile.Require))
1511 for _, r := range modFile.Require {
1512 if index := ld.MainModules.Index(m); index != nil && index.exclude[r.Mod] {
1513 if cfg.BuildMod == "mod" {
1514 fmt.Fprintf(os.Stderr, "go: dropping requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
1515 } else {
1516 fmt.Fprintf(os.Stderr, "go: ignoring requirement on excluded version %s %s\n", r.Mod.Path, r.Mod.Version)
1517 }
1518 continue
1519 }
1520
1521 roots = append(roots, r.Mod)
1522 if !r.Indirect {
1523 direct[r.Mod.Path] = true
1524 }
1525 }
1526 goVersion := gover.FromGoMod(modFile)
1527 var toolchain string
1528 if addToolchainRoot && modFile.Toolchain != nil {
1529 toolchain = modFile.Toolchain.Name
1530 }
1531 roots = appendGoAndToolchainRoots(roots, goVersion, toolchain, direct)
1532 return roots, direct
1533 }
1534
1535 func appendGoAndToolchainRoots(roots []module.Version, goVersion, toolchain string, direct map[string]bool) []module.Version {
1536
1537 roots = append(roots, module.Version{Path: "go", Version: goVersion})
1538 direct["go"] = true
1539
1540 if toolchain != "" {
1541 roots = append(roots, module.Version{Path: "toolchain", Version: toolchain})
1542
1543
1544
1545
1546
1547 }
1548 return roots
1549 }
1550
1551
1552
1553 func setDefaultBuildMod(ld *Loader) {
1554 if cfg.BuildModExplicit {
1555 if ld.inWorkspaceMode() && cfg.BuildMod != "readonly" && cfg.BuildMod != "vendor" {
1556 switch cfg.CmdName {
1557 case "work sync", "mod graph", "mod verify", "mod why":
1558
1559
1560 panic("in workspace mode and -mod was set explicitly, but command doesn't support setting -mod")
1561 default:
1562 base.Fatalf("go: -mod may only be set to readonly or vendor when in workspace mode, but it is set to %q"+
1563 "\n\tRemove the -mod flag to use the default readonly value, "+
1564 "\n\tor set GOWORK=off to disable workspace mode.", cfg.BuildMod)
1565 }
1566 }
1567
1568 return
1569 }
1570
1571
1572
1573
1574 switch cfg.CmdName {
1575 case "get", "mod download", "mod init", "mod tidy", "work sync":
1576
1577 cfg.BuildMod = "mod"
1578 return
1579 case "mod graph", "mod verify", "mod why":
1580
1581
1582
1583
1584 cfg.BuildMod = "mod"
1585 return
1586 case "mod vendor", "work vendor":
1587 cfg.BuildMod = "readonly"
1588 return
1589 }
1590 if ld.modRoots == nil {
1591 if ld.allowMissingModuleImports {
1592 cfg.BuildMod = "mod"
1593 } else {
1594 cfg.BuildMod = "readonly"
1595 }
1596 return
1597 }
1598
1599 if len(ld.modRoots) >= 1 {
1600 var goVersion string
1601 var versionSource string
1602 if ld.inWorkspaceMode() {
1603 versionSource = "go.work"
1604 if wfg := ld.MainModules.WorkFile().Go; wfg != nil {
1605 goVersion = wfg.Version
1606 }
1607 } else {
1608 versionSource = "go.mod"
1609 index := ld.MainModules.GetSingleIndexOrNil(ld)
1610 if index != nil {
1611 goVersion = index.goVersion
1612 }
1613 }
1614 vendorDir := ""
1615 if ld.workFilePath != "" {
1616 vendorDir = filepath.Join(filepath.Dir(ld.workFilePath), "vendor")
1617 } else {
1618 if len(ld.modRoots) != 1 {
1619 panic(fmt.Errorf("outside workspace mode, but have %v modRoots", ld.modRoots))
1620 }
1621 vendorDir = filepath.Join(ld.modRoots[0], "vendor")
1622 }
1623 if fi, err := fsys.Stat(vendorDir); err == nil && fi.IsDir() {
1624 if goVersion != "" {
1625 if gover.Compare(goVersion, "1.14") < 0 {
1626
1627
1628
1629 cfg.BuildModReason = fmt.Sprintf("Go version in "+versionSource+" is %s, so vendor directory was not used.", goVersion)
1630 } else {
1631 vendoredWorkspace, err := modulesTextIsForWorkspace(vendorDir)
1632 if err != nil {
1633 base.Fatalf("go: reading modules.txt for vendor directory: %v", err)
1634 }
1635 if vendoredWorkspace != (versionSource == "go.work") {
1636 if vendoredWorkspace {
1637 cfg.BuildModReason = "Outside workspace mode, but vendor directory is for a workspace."
1638 } else {
1639 cfg.BuildModReason = "In workspace mode, but vendor directory is not for a workspace"
1640 }
1641 } else {
1642
1643
1644
1645 cfg.BuildMod = "vendor"
1646 cfg.BuildModReason = "Go version in " + versionSource + " is at least 1.14 and vendor directory exists."
1647 return
1648 }
1649 }
1650 } else {
1651 cfg.BuildModReason = fmt.Sprintf("Go version in %s is unspecified, so vendor directory was not used.", versionSource)
1652 }
1653 }
1654 }
1655
1656 cfg.BuildMod = "readonly"
1657 }
1658
1659 func modulesTextIsForWorkspace(vendorDir string) (bool, error) {
1660 f, err := fsys.Open(filepath.Join(vendorDir, "modules.txt"))
1661 if errors.Is(err, os.ErrNotExist) {
1662
1663
1664
1665
1666 return false, nil
1667 }
1668 if err != nil {
1669 return false, err
1670 }
1671 defer f.Close()
1672 var buf [512]byte
1673 n, err := f.Read(buf[:])
1674 if err != nil && err != io.EOF {
1675 return false, err
1676 }
1677 line, _, _ := strings.Cut(string(buf[:n]), "\n")
1678 if annotations, ok := strings.CutPrefix(line, "## "); ok {
1679 for entry := range strings.SplitSeq(annotations, ";") {
1680 entry = strings.TrimSpace(entry)
1681 if entry == "workspace" {
1682 return true, nil
1683 }
1684 }
1685 }
1686 return false, nil
1687 }
1688
1689 func mustHaveCompleteRequirements(ld *Loader) bool {
1690 return cfg.BuildMod != "mod" && !ld.inWorkspaceMode()
1691 }
1692
1693
1694
1695
1696 func addGoStmt(modFile *modfile.File, mod module.Version, v string) {
1697 if modFile.Go != nil && modFile.Go.Version != "" {
1698 return
1699 }
1700 forceGoStmt(modFile, mod, v)
1701 }
1702
1703 func forceGoStmt(modFile *modfile.File, mod module.Version, v string) {
1704 if err := modFile.AddGoStmt(v); err != nil {
1705 base.Fatalf("go: internal error: %v", err)
1706 }
1707 rawGoVersion.Store(mod, v)
1708 }
1709
1710 var altConfigs = []string{
1711 ".git/config",
1712 }
1713
1714 func findModuleRoot(dir string) (roots string) {
1715 if dir == "" {
1716 panic("dir not set")
1717 }
1718 dir = filepath.Clean(dir)
1719
1720
1721 for {
1722 if fi, err := fsys.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {
1723 return dir
1724 }
1725 d := filepath.Dir(dir)
1726 if d == dir {
1727 break
1728 }
1729 dir = d
1730 }
1731 return ""
1732 }
1733
1734 func findWorkspaceFile(dir string) (root string) {
1735 if dir == "" {
1736 panic("dir not set")
1737 }
1738 dir = filepath.Clean(dir)
1739
1740
1741 for {
1742 f := filepath.Join(dir, "go.work")
1743 if fi, err := fsys.Stat(f); err == nil && !fi.IsDir() {
1744 return f
1745 }
1746 d := filepath.Dir(dir)
1747 if d == dir {
1748 break
1749 }
1750 if d == cfg.GOROOT {
1751
1752
1753
1754 return ""
1755 }
1756 dir = d
1757 }
1758 return ""
1759 }
1760
1761 func findAltConfig(dir string) (root, name string) {
1762 if dir == "" {
1763 panic("dir not set")
1764 }
1765 dir = filepath.Clean(dir)
1766 if rel := search.InDir(dir, cfg.BuildContext.GOROOT); rel != "" {
1767
1768
1769 return "", ""
1770 }
1771 for {
1772 for _, name := range altConfigs {
1773 if fi, err := fsys.Stat(filepath.Join(dir, name)); err == nil && !fi.IsDir() {
1774 return dir, name
1775 }
1776 }
1777 d := filepath.Dir(dir)
1778 if d == dir {
1779 break
1780 }
1781 dir = d
1782 }
1783 return "", ""
1784 }
1785
1786 func findModulePath(dir string) (string, error) {
1787
1788
1789
1790
1791
1792
1793
1794
1795 list, _ := os.ReadDir(dir)
1796 for _, info := range list {
1797 if info.Type().IsRegular() && strings.HasSuffix(info.Name(), ".go") {
1798 if com := findImportComment(filepath.Join(dir, info.Name())); com != "" {
1799 return com, nil
1800 }
1801 }
1802 }
1803 for _, info1 := range list {
1804 if info1.IsDir() {
1805 files, _ := os.ReadDir(filepath.Join(dir, info1.Name()))
1806 for _, info2 := range files {
1807 if info2.Type().IsRegular() && strings.HasSuffix(info2.Name(), ".go") {
1808 if com := findImportComment(filepath.Join(dir, info1.Name(), info2.Name())); com != "" {
1809 return path.Dir(com), nil
1810 }
1811 }
1812 }
1813 }
1814 }
1815
1816
1817 var badPathErr error
1818 for _, gpdir := range filepath.SplitList(cfg.BuildContext.GOPATH) {
1819 if gpdir == "" {
1820 continue
1821 }
1822 if rel := search.InDir(dir, filepath.Join(gpdir, "src")); rel != "" && rel != "." {
1823 path := filepath.ToSlash(rel)
1824
1825 if err := module.CheckImportPath(path); err != nil {
1826 badPathErr = err
1827 break
1828 }
1829 return path, nil
1830 }
1831 }
1832
1833 reason := "outside GOPATH, module path must be specified"
1834 if badPathErr != nil {
1835
1836
1837 reason = fmt.Sprintf("bad module path inferred from directory in GOPATH: %v", badPathErr)
1838 }
1839 msg := `cannot determine module path for source directory %s (%s)
1840
1841 Example usage:
1842 'go mod init example.com/m' to initialize a v0 or v1 module
1843 'go mod init example.com/m/v2' to initialize a v2 module
1844
1845 Run 'go help mod init' for more information.
1846 `
1847 return "", fmt.Errorf(msg, dir, reason)
1848 }
1849
1850 var importCommentRE = lazyregexp.New(`(?m)^package[ \t]+[^ \t\r\n/]+[ \t]+//[ \t]+import[ \t]+(\"[^"]+\")[ \t]*\r?\n`)
1851
1852 func findImportComment(file string) string {
1853 data, err := os.ReadFile(file)
1854 if err != nil {
1855 return ""
1856 }
1857 m := importCommentRE.FindSubmatch(data)
1858 if m == nil {
1859 return ""
1860 }
1861 path, err := strconv.Unquote(string(m[1]))
1862 if err != nil {
1863 return ""
1864 }
1865 return path
1866 }
1867
1868
1869 type WriteOpts struct {
1870 DropToolchain bool
1871 ExplicitToolchain bool
1872
1873 AddTools []string
1874 DropTools []string
1875
1876
1877
1878 TidyWroteGo bool
1879 }
1880
1881
1882 func WriteGoMod(ld *Loader, ctx context.Context, opts WriteOpts) error {
1883 ld.requirements = LoadModFile(ld, ctx)
1884 return commitRequirements(ld, ctx, opts)
1885 }
1886
1887 var errNoChange = errors.New("no update needed")
1888
1889
1890
1891 func UpdateGoModFromReqs(ld *Loader, ctx context.Context, opts WriteOpts) (before, after []byte, modFile *modfile.File, err error) {
1892 if ld.MainModules.Len() != 1 || ld.MainModules.ModRoot(ld.MainModules.Versions()[0]) == "" {
1893
1894 return nil, nil, nil, errNoChange
1895 }
1896 mainModule := ld.MainModules.mustGetSingleMainModule(ld)
1897 modFile = ld.MainModules.ModFile(mainModule)
1898 if modFile == nil {
1899
1900 return nil, nil, nil, errNoChange
1901 }
1902 before, err = modFile.Format()
1903 if err != nil {
1904 return nil, nil, nil, err
1905 }
1906
1907 var list []*modfile.Require
1908 toolchain := ""
1909 goVersion := ""
1910 for _, m := range ld.requirements.rootModules {
1911 if m.Path == "go" {
1912 goVersion = m.Version
1913 continue
1914 }
1915 if m.Path == "toolchain" {
1916 toolchain = m.Version
1917 continue
1918 }
1919 list = append(list, &modfile.Require{
1920 Mod: m,
1921 Indirect: !ld.requirements.direct[m.Path],
1922 })
1923 }
1924
1925
1926
1927
1928 if goVersion == "" {
1929 base.Fatalf("go: internal error: missing go root module in WriteGoMod")
1930 }
1931 if gover.Compare(goVersion, gover.Local()) > 0 {
1932
1933 return nil, nil, nil, &gover.TooNewError{What: "updating go.mod", GoVersion: goVersion}
1934 }
1935 wroteGo := opts.TidyWroteGo
1936 if !wroteGo && modFile.Go == nil || modFile.Go.Version != goVersion {
1937 alwaysUpdate := cfg.BuildMod == "mod" || cfg.CmdName == "mod tidy" || cfg.CmdName == "get"
1938 if modFile.Go == nil && goVersion == gover.DefaultGoModVersion && !alwaysUpdate {
1939
1940
1941
1942 } else {
1943 wroteGo = true
1944 forceGoStmt(modFile, mainModule, goVersion)
1945 }
1946 }
1947 if toolchain == "" {
1948 toolchain = "go" + goVersion
1949 }
1950
1951 toolVers := gover.FromToolchain(toolchain)
1952 if opts.DropToolchain || toolchain == "go"+goVersion || (gover.Compare(toolVers, gover.GoStrictVersion) < 0 && !opts.ExplicitToolchain) {
1953
1954
1955 modFile.DropToolchainStmt()
1956 } else {
1957 modFile.AddToolchainStmt(toolchain)
1958 }
1959
1960 for _, path := range opts.AddTools {
1961 modFile.AddTool(path)
1962 }
1963
1964 for _, path := range opts.DropTools {
1965 modFile.DropTool(path)
1966 }
1967
1968
1969 if gover.Compare(goVersion, gover.SeparateIndirectVersion) < 0 {
1970 modFile.SetRequire(list)
1971 } else {
1972 modFile.SetRequireSeparateIndirect(list)
1973 }
1974 modFile.Cleanup()
1975 after, err = modFile.Format()
1976 if err != nil {
1977 return nil, nil, nil, err
1978 }
1979 return before, after, modFile, nil
1980 }
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991 func commitRequirements(ld *Loader, ctx context.Context, opts WriteOpts) (err error) {
1992 if ld.inWorkspaceMode() {
1993
1994
1995 return ld.Fetcher().WriteGoSum(ctx, keepSums(ld, ctx, ld.pkgLoader, ld.requirements, addBuildListZipSums), mustHaveCompleteRequirements(ld))
1996 }
1997 _, updatedGoMod, modFile, err := UpdateGoModFromReqs(ld, ctx, opts)
1998 if err != nil {
1999 if errors.Is(err, errNoChange) {
2000 return nil
2001 }
2002 return err
2003 }
2004
2005 index := ld.MainModules.GetSingleIndexOrNil(ld)
2006 dirty := index.modFileIsDirty(modFile) || len(opts.DropTools) > 0 || len(opts.AddTools) > 0
2007 if dirty && cfg.BuildMod != "mod" {
2008
2009
2010 return errGoModDirty
2011 }
2012
2013 if !dirty && cfg.CmdName != "mod tidy" {
2014
2015
2016
2017
2018 if cfg.CmdName != "mod init" {
2019 if err := ld.Fetcher().WriteGoSum(ctx, keepSums(ld, ctx, ld.pkgLoader, ld.requirements, addBuildListZipSums), mustHaveCompleteRequirements(ld)); err != nil {
2020 return err
2021 }
2022 }
2023 return nil
2024 }
2025
2026 mainModule := ld.MainModules.mustGetSingleMainModule(ld)
2027 modFilePath := modFilePath(ld.MainModules.ModRoot(mainModule))
2028 if fsys.Replaced(modFilePath) {
2029 if dirty {
2030 return errors.New("updates to go.mod needed, but go.mod is part of the overlay specified with -overlay")
2031 }
2032 return nil
2033 }
2034 defer func() {
2035
2036 ld.MainModules.SetIndex(mainModule, indexModFile(updatedGoMod, modFile, mainModule, false))
2037
2038
2039
2040 if cfg.CmdName != "mod init" {
2041 if err == nil {
2042 err = ld.Fetcher().WriteGoSum(ctx, keepSums(ld, ctx, ld.pkgLoader, ld.requirements, addBuildListZipSums), mustHaveCompleteRequirements(ld))
2043 }
2044 }
2045 }()
2046
2047
2048
2049 if unlock, err := modfetch.SideLock(ctx); err == nil {
2050 defer unlock()
2051 }
2052
2053 err = lockedfile.Transform(modFilePath, func(old []byte) ([]byte, error) {
2054 if bytes.Equal(old, updatedGoMod) {
2055
2056
2057 return nil, errNoChange
2058 }
2059
2060 if index != nil && !bytes.Equal(old, index.data) {
2061
2062
2063
2064
2065
2066
2067 return nil, fmt.Errorf("existing contents have changed since last read")
2068 }
2069
2070 return updatedGoMod, nil
2071 })
2072
2073 if err != nil && err != errNoChange {
2074 return fmt.Errorf("updating go.mod: %w", err)
2075 }
2076 return nil
2077 }
2078
2079
2080
2081
2082
2083
2084
2085 func keepSums(ld *Loader, ctx context.Context, pld *packageLoader, rs *Requirements, which whichSums) map[module.Version]bool {
2086
2087
2088
2089
2090 keep := make(map[module.Version]bool)
2091
2092
2093
2094
2095
2096 keepModSumsForZipSums := true
2097 if pld == nil {
2098 if gover.Compare(ld.MainModules.GoVersion(ld), gover.TidyGoModSumVersion) < 0 && cfg.BuildMod != "mod" {
2099 keepModSumsForZipSums = false
2100 }
2101 } else {
2102 keepPkgGoModSums := true
2103 if gover.Compare(pld.requirements.GoVersion(ld), gover.TidyGoModSumVersion) < 0 && (pld.Tidy || cfg.BuildMod != "mod") {
2104 keepPkgGoModSums = false
2105 keepModSumsForZipSums = false
2106 }
2107 for _, pkg := range pld.pkgs {
2108
2109
2110
2111 if pkg.testOf != nil || (pkg.mod.Path == "" && pkg.err == nil) || module.CheckImportPath(pkg.path) != nil {
2112 continue
2113 }
2114
2115
2116
2117
2118
2119
2120 if keepPkgGoModSums {
2121 r := resolveReplacement(ld, pkg.mod)
2122 keep[modkey(r)] = true
2123 }
2124
2125 if rs.pruning == pruned && pkg.mod.Path != "" {
2126 if v, ok := rs.rootSelected(ld, pkg.mod.Path); ok && v == pkg.mod.Version {
2127
2128
2129
2130
2131
2132 for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) {
2133 if v, ok := rs.rootSelected(ld, prefix); ok && v != "none" {
2134 m := module.Version{Path: prefix, Version: v}
2135 r := resolveReplacement(ld, m)
2136 keep[r] = true
2137 }
2138 }
2139 continue
2140 }
2141 }
2142
2143 mg, _ := rs.Graph(ld, ctx)
2144 for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) {
2145 if v := mg.Selected(prefix); v != "none" {
2146 m := module.Version{Path: prefix, Version: v}
2147 r := resolveReplacement(ld, m)
2148 keep[r] = true
2149 }
2150 }
2151 }
2152 }
2153
2154 if rs.graph.Load() == nil {
2155
2156
2157
2158 for _, m := range rs.rootModules {
2159 r := resolveReplacement(ld, m)
2160 keep[modkey(r)] = true
2161 if which == addBuildListZipSums {
2162 keep[r] = true
2163 }
2164 }
2165 } else {
2166 mg, _ := rs.Graph(ld, ctx)
2167 mg.WalkBreadthFirst(func(m module.Version) {
2168 if _, ok := mg.RequiredBy(m); ok {
2169
2170
2171
2172 r := resolveReplacement(ld, m)
2173 keep[modkey(r)] = true
2174 }
2175 })
2176
2177 if which == addBuildListZipSums {
2178 for _, m := range mg.BuildList() {
2179 r := resolveReplacement(ld, m)
2180 if keepModSumsForZipSums {
2181 keep[modkey(r)] = true
2182 }
2183 keep[r] = true
2184 }
2185 }
2186 }
2187
2188 return keep
2189 }
2190
2191 type whichSums int8
2192
2193 const (
2194 loadedZipSumsOnly = whichSums(iota)
2195 addBuildListZipSums
2196 )
2197
2198
2199
2200 func modkey(m module.Version) module.Version {
2201 return module.Version{Path: m.Path, Version: m.Version + "/go.mod"}
2202 }
2203
2204 func suggestModulePath(path string) string {
2205 var m string
2206
2207 i := len(path)
2208 for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9' || path[i-1] == '.') {
2209 i--
2210 }
2211 url := path[:i]
2212 url = strings.TrimSuffix(url, "/v")
2213 url = strings.TrimSuffix(url, "/")
2214
2215 f := func(c rune) bool {
2216 return c > '9' || c < '0'
2217 }
2218 s := strings.FieldsFunc(path[i:], f)
2219 if len(s) > 0 {
2220 m = s[0]
2221 }
2222 m = strings.TrimLeft(m, "0")
2223 if m == "" || m == "1" {
2224 return url + "/v2"
2225 }
2226
2227 return url + "/v" + m
2228 }
2229
2230 func suggestGopkgIn(path string) string {
2231 var m string
2232 i := len(path)
2233 for i > 0 && (('0' <= path[i-1] && path[i-1] <= '9') || (path[i-1] == '.')) {
2234 i--
2235 }
2236 url := path[:i]
2237 url = strings.TrimSuffix(url, ".v")
2238 url = strings.TrimSuffix(url, "/v")
2239 url = strings.TrimSuffix(url, "/")
2240
2241 f := func(c rune) bool {
2242 return c > '9' || c < '0'
2243 }
2244 s := strings.FieldsFunc(path, f)
2245 if len(s) > 0 {
2246 m = s[0]
2247 }
2248
2249 m = strings.TrimLeft(m, "0")
2250
2251 if m == "" {
2252 return url + ".v1"
2253 }
2254 return url + ".v" + m
2255 }
2256
2257 func CheckGodebug(verb, k, v string) error {
2258 if strings.ContainsAny(k, " \t") {
2259 return fmt.Errorf("key contains space")
2260 }
2261 if strings.ContainsAny(v, " \t") {
2262 return fmt.Errorf("value contains space")
2263 }
2264 if strings.ContainsAny(k, ",") {
2265 return fmt.Errorf("key contains comma")
2266 }
2267 if strings.ContainsAny(v, ",") {
2268 return fmt.Errorf("value contains comma")
2269 }
2270 if k == "default" {
2271 if !strings.HasPrefix(v, "go") || !gover.IsValid(v[len("go"):]) {
2272 return fmt.Errorf("value for default= must be goVERSION")
2273 }
2274 if gover.Compare(v[len("go"):], gover.Local()) > 0 {
2275 return fmt.Errorf("default=%s too new (toolchain is go%s)", v, gover.Local())
2276 }
2277 return nil
2278 }
2279 if godebugs.Lookup(k) != nil {
2280 return nil
2281 }
2282 for _, info := range godebugs.Removed {
2283 if info.Name == k {
2284 return fmt.Errorf("use of removed %s %q, see https://go.dev/doc/godebug#go-1%v", verb, k, info.Removed)
2285 }
2286 }
2287 return fmt.Errorf("unknown %s %q", verb, k)
2288 }
2289
View as plain text