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