1
2
3
4
5 package imports
6
7 import (
8 "bytes"
9 "context"
10 "encoding/json"
11 "fmt"
12 "os"
13 "path"
14 "path/filepath"
15 "regexp"
16 "sort"
17 "strconv"
18 "strings"
19
20 "golang.org/x/mod/module"
21 "golang.org/x/tools/internal/event"
22 "golang.org/x/tools/internal/gocommand"
23 "golang.org/x/tools/internal/gopathwalk"
24 "golang.org/x/tools/internal/stdlib"
25 )
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 type ModuleResolver struct {
59 env *ProcessEnv
60
61
62 dummyVendorMod *gocommand.ModuleJSON
63 moduleCacheDir string
64 roots []gopathwalk.Root
65 mains []*gocommand.ModuleJSON
66 mainByDir map[string]*gocommand.ModuleJSON
67 modsByModPath []*gocommand.ModuleJSON
68 modsByDir []*gocommand.ModuleJSON
69
70
71
72
73
74
75 scanSema chan struct{}
76 scannedRoots map[gopathwalk.Root]bool
77
78
79
80
81
82
83
84
85 moduleCacheCache *DirInfoCache
86 otherCache *DirInfoCache
87 }
88
89
90
91
92
93 func newModuleResolver(e *ProcessEnv, moduleCacheCache *DirInfoCache) (*ModuleResolver, error) {
94 r := &ModuleResolver{
95 env: e,
96 scanSema: make(chan struct{}, 1),
97 }
98 r.scanSema <- struct{}{}
99
100 goenv, err := r.env.goEnv()
101 if err != nil {
102 return nil, err
103 }
104
105
106 inv := gocommand.Invocation{
107 BuildFlags: r.env.BuildFlags,
108 ModFlag: r.env.ModFlag,
109 Env: r.env.env(),
110 Logf: r.env.Logf,
111 WorkingDir: r.env.WorkingDir,
112 }
113
114 vendorEnabled := false
115 var mainModVendor *gocommand.ModuleJSON
116 var mainModsVendor []*gocommand.ModuleJSON
117
118 goWork := r.env.Env["GOWORK"]
119 if len(goWork) == 0 {
120
121
122
123 vendorEnabled, mainModVendor, err = gocommand.VendorEnabled(context.TODO(), inv, r.env.GocmdRunner)
124 if err != nil {
125 return nil, err
126 }
127 } else {
128 vendorEnabled, mainModsVendor, err = gocommand.WorkspaceVendorEnabled(context.Background(), inv, r.env.GocmdRunner)
129 if err != nil {
130 return nil, err
131 }
132 }
133
134 if vendorEnabled {
135 if mainModVendor != nil {
136
137
138 r.mains = []*gocommand.ModuleJSON{mainModVendor}
139 r.dummyVendorMod = &gocommand.ModuleJSON{
140 Path: "",
141 Dir: filepath.Join(mainModVendor.Dir, "vendor"),
142 }
143 r.modsByModPath = []*gocommand.ModuleJSON{mainModVendor, r.dummyVendorMod}
144 r.modsByDir = []*gocommand.ModuleJSON{mainModVendor, r.dummyVendorMod}
145 } else {
146
147
148 r.mains = mainModsVendor
149 r.dummyVendorMod = &gocommand.ModuleJSON{
150 Path: "",
151 Dir: filepath.Join(filepath.Dir(goWork), "vendor"),
152 }
153 r.modsByModPath = append(append([]*gocommand.ModuleJSON{}, mainModsVendor...), r.dummyVendorMod)
154 r.modsByDir = append(append([]*gocommand.ModuleJSON{}, mainModsVendor...), r.dummyVendorMod)
155 }
156 } else {
157
158 err := r.initAllMods()
159
160
161 if err != nil {
162 if errMsg := err.Error(); !strings.Contains(errMsg, "working directory is not part of a module") && !strings.Contains(errMsg, "go.mod file not found") {
163 return nil, err
164 }
165 }
166 }
167
168 r.moduleCacheDir = gomodcacheForEnv(goenv)
169 if r.moduleCacheDir == "" {
170 return nil, fmt.Errorf("cannot resolve GOMODCACHE")
171 }
172
173 sort.Slice(r.modsByModPath, func(i, j int) bool {
174 count := func(x int) int {
175 return strings.Count(r.modsByModPath[x].Path, "/")
176 }
177 return count(j) < count(i)
178 })
179 sort.Slice(r.modsByDir, func(i, j int) bool {
180 count := func(x int) int {
181 return strings.Count(r.modsByDir[x].Dir, string(filepath.Separator))
182 }
183 return count(j) < count(i)
184 })
185
186 r.roots = []gopathwalk.Root{}
187 if goenv["GOROOT"] != "" {
188 r.roots = append(r.roots, gopathwalk.Root{Path: filepath.Join(goenv["GOROOT"], "/src"), Type: gopathwalk.RootGOROOT})
189 }
190 r.mainByDir = make(map[string]*gocommand.ModuleJSON)
191 for _, main := range r.mains {
192 r.roots = append(r.roots, gopathwalk.Root{Path: main.Dir, Type: gopathwalk.RootCurrentModule})
193 r.mainByDir[main.Dir] = main
194 }
195 if vendorEnabled {
196 r.roots = append(r.roots, gopathwalk.Root{Path: r.dummyVendorMod.Dir, Type: gopathwalk.RootOther})
197 } else {
198 addDep := func(mod *gocommand.ModuleJSON) {
199 if mod.Replace == nil {
200
201
202
203
204
205 r.roots = append(r.roots, gopathwalk.Root{Path: mod.Dir, Type: gopathwalk.RootModuleCache})
206 } else {
207 r.roots = append(r.roots, gopathwalk.Root{Path: mod.Dir, Type: gopathwalk.RootOther})
208 }
209 }
210
211 for _, mod := range r.modsByModPath {
212 if !mod.Indirect && !mod.Main {
213 addDep(mod)
214 }
215 }
216 for _, mod := range r.modsByModPath {
217 if mod.Indirect && !mod.Main {
218 addDep(mod)
219 }
220 }
221
222
223
224
225
226
227
228 r.moduleCacheCache = moduleCacheCache
229 r.roots = append(r.roots, gopathwalk.Root{Path: r.moduleCacheDir, Type: gopathwalk.RootModuleCache})
230 }
231
232 r.scannedRoots = map[gopathwalk.Root]bool{}
233 if r.moduleCacheCache == nil {
234 r.moduleCacheCache = NewDirInfoCache()
235 }
236 r.otherCache = NewDirInfoCache()
237 return r, nil
238 }
239
240
241
242
243
244
245
246 func gomodcacheForEnv(goenv map[string]string) string {
247 if gmc := goenv["GOMODCACHE"]; gmc != "" {
248 return gmc
249 }
250 gopaths := filepath.SplitList(goenv["GOPATH"])
251 if len(gopaths) == 0 {
252 return ""
253 }
254 return filepath.Join(gopaths[0], "/pkg/mod")
255 }
256
257 func (r *ModuleResolver) initAllMods() error {
258 stdout, err := r.env.invokeGo(context.TODO(), "list", "-m", "-e", "-json", "...")
259 if err != nil {
260 return err
261 }
262 for dec := json.NewDecoder(stdout); dec.More(); {
263 mod := &gocommand.ModuleJSON{}
264 if err := dec.Decode(mod); err != nil {
265 return err
266 }
267 if mod.Dir == "" {
268 if r.env.Logf != nil {
269 r.env.Logf("module %v has not been downloaded and will be ignored", mod.Path)
270 }
271
272 continue
273 }
274
275 mod.Dir = filepath.Clean(mod.Dir)
276 r.modsByModPath = append(r.modsByModPath, mod)
277 r.modsByDir = append(r.modsByDir, mod)
278 if mod.Main {
279 r.mains = append(r.mains, mod)
280 }
281 }
282 return nil
283 }
284
285
286
287
288
289
290 func (r *ModuleResolver) ClearForNewScan() Resolver {
291 <-r.scanSema
292 r2 := &ModuleResolver{
293 env: r.env,
294 dummyVendorMod: r.dummyVendorMod,
295 moduleCacheDir: r.moduleCacheDir,
296 roots: r.roots,
297 mains: r.mains,
298 mainByDir: r.mainByDir,
299 modsByModPath: r.modsByModPath,
300
301 scanSema: make(chan struct{}, 1),
302 scannedRoots: make(map[gopathwalk.Root]bool),
303 otherCache: NewDirInfoCache(),
304 moduleCacheCache: r.moduleCacheCache,
305 }
306 r2.scanSema <- struct{}{}
307
308
309
310
311
312
313
314
315 for _, root := range r.roots {
316 if root.Type == gopathwalk.RootModuleCache && r.scannedRoots[root] {
317 r2.scannedRoots[root] = true
318 }
319 }
320 r.scanSema <- struct{}{}
321 return r2
322 }
323
324
325
326
327
328
329
330
331
332
333 func (e *ProcessEnv) ClearModuleInfo() {
334 if r, ok := e.resolver.(*ModuleResolver); ok {
335 resolver, err := newModuleResolver(e, e.ModCache)
336 if err != nil {
337 e.resolver = nil
338 e.resolverErr = err
339 return
340 }
341
342 <-r.scanSema
343 resolver.moduleCacheCache = r.moduleCacheCache
344 resolver.otherCache = r.otherCache
345 r.scanSema <- struct{}{}
346
347 e.UpdateResolver(resolver)
348 }
349 }
350
351
352
353
354
355
356
357 func (e *ProcessEnv) UpdateResolver(r Resolver) {
358 e.resolver = r
359 e.resolverErr = nil
360 }
361
362
363
364
365 func (r *ModuleResolver) findPackage(importPath string) (*gocommand.ModuleJSON, string) {
366
367
368 for _, m := range r.modsByModPath {
369 if !strings.HasPrefix(importPath, m.Path) {
370 continue
371 }
372 pathInModule := importPath[len(m.Path):]
373 pkgDir := filepath.Join(m.Dir, pathInModule)
374 if r.dirIsNestedModule(pkgDir, m) {
375 continue
376 }
377
378 if info, ok := r.cacheLoad(pkgDir); ok {
379 if loaded, err := info.reachedStatus(nameLoaded); loaded {
380 if err != nil {
381 continue
382 }
383 return m, pkgDir
384 }
385 if scanned, err := info.reachedStatus(directoryScanned); scanned && err != nil {
386 continue
387 }
388
389
390
391
392
393 if _, err := r.cachePackageName(info); err == nil {
394 return m, pkgDir
395 }
396 }
397
398
399 pkgFiles, err := os.ReadDir(pkgDir)
400 if err != nil {
401 continue
402 }
403
404
405
406 for _, fi := range pkgFiles {
407 if ok, _ := r.env.matchFile(pkgDir, fi.Name()); ok {
408 return m, pkgDir
409 }
410 }
411 }
412 return nil, ""
413 }
414
415 func (r *ModuleResolver) cacheLoad(dir string) (directoryPackageInfo, bool) {
416 if info, ok := r.moduleCacheCache.Load(dir); ok {
417 return info, ok
418 }
419 return r.otherCache.Load(dir)
420 }
421
422 func (r *ModuleResolver) cacheStore(info directoryPackageInfo) {
423 if info.rootType == gopathwalk.RootModuleCache {
424 r.moduleCacheCache.Store(info.dir, info)
425 } else {
426 r.otherCache.Store(info.dir, info)
427 }
428 }
429
430
431 func (r *ModuleResolver) cachePackageName(info directoryPackageInfo) (string, error) {
432 if info.rootType == gopathwalk.RootModuleCache {
433 return r.moduleCacheCache.CachePackageName(info)
434 }
435 return r.otherCache.CachePackageName(info)
436 }
437
438 func (r *ModuleResolver) cacheExports(ctx context.Context, env *ProcessEnv, info directoryPackageInfo) (string, []stdlib.Symbol, error) {
439 if info.rootType == gopathwalk.RootModuleCache {
440 return r.moduleCacheCache.CacheExports(ctx, env, info)
441 }
442 return r.otherCache.CacheExports(ctx, env, info)
443 }
444
445
446
447 func (r *ModuleResolver) findModuleByDir(dir string) *gocommand.ModuleJSON {
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462 for _, m := range r.modsByDir {
463 if !strings.HasPrefix(dir, m.Dir) {
464 continue
465 }
466
467 if r.dirIsNestedModule(dir, m) {
468 continue
469 }
470
471 return m
472 }
473 return nil
474 }
475
476
477
478 func (r *ModuleResolver) dirIsNestedModule(dir string, mod *gocommand.ModuleJSON) bool {
479 if !strings.HasPrefix(dir, mod.Dir) {
480 return false
481 }
482 if r.dirInModuleCache(dir) {
483
484
485 return false
486 }
487 if mod != nil && mod == r.dummyVendorMod {
488
489 return false
490 }
491 modDir, _ := r.modInfo(dir)
492 if modDir == "" {
493 return false
494 }
495 return modDir != mod.Dir
496 }
497
498 func readModName(modFile string) string {
499 modBytes, err := os.ReadFile(modFile)
500 if err != nil {
501 return ""
502 }
503 return modulePath(modBytes)
504 }
505
506 func (r *ModuleResolver) modInfo(dir string) (modDir, modName string) {
507 if r.dirInModuleCache(dir) {
508 if matches := modCacheRegexp.FindStringSubmatch(dir); len(matches) == 3 {
509 index := strings.Index(dir, matches[1]+"@"+matches[2])
510 modDir := filepath.Join(dir[:index], matches[1]+"@"+matches[2])
511 return modDir, readModName(filepath.Join(modDir, "go.mod"))
512 }
513 }
514 for {
515 if info, ok := r.cacheLoad(dir); ok {
516 return info.moduleDir, info.moduleName
517 }
518 f := filepath.Join(dir, "go.mod")
519 info, err := os.Stat(f)
520 if err == nil && !info.IsDir() {
521 return dir, readModName(f)
522 }
523
524 d := filepath.Dir(dir)
525 if len(d) >= len(dir) {
526 return "", ""
527 }
528 dir = d
529 }
530 }
531
532 func (r *ModuleResolver) dirInModuleCache(dir string) bool {
533 if r.moduleCacheDir == "" {
534 return false
535 }
536 return strings.HasPrefix(dir, r.moduleCacheDir)
537 }
538
539 func (r *ModuleResolver) loadPackageNames(importPaths []string, srcDir string) (map[string]string, error) {
540 names := map[string]string{}
541 for _, path := range importPaths {
542
543 _, packageDir := r.findPackage(path)
544 if packageDir == "" {
545 continue
546 }
547 name, err := packageDirToName(packageDir)
548 if err != nil {
549 continue
550 }
551 names[path] = name
552 }
553 return names, nil
554 }
555
556 func (r *ModuleResolver) scan(ctx context.Context, callback *scanCallback) error {
557 ctx, done := event.Start(ctx, "imports.ModuleResolver.scan")
558 defer done()
559
560 processDir := func(info directoryPackageInfo) {
561
562 if scanned, err := info.reachedStatus(directoryScanned); !scanned || err != nil {
563 return
564 }
565 pkg, err := r.canonicalize(info)
566 if err != nil {
567 return
568 }
569 if !callback.dirFound(pkg) {
570 return
571 }
572
573 pkg.packageName, err = r.cachePackageName(info)
574 if err != nil {
575 return
576 }
577 if !callback.packageNameLoaded(pkg) {
578 return
579 }
580
581 _, exports, err := r.loadExports(ctx, pkg, false)
582 if err != nil {
583 return
584 }
585 callback.exportsLoaded(pkg, exports)
586 }
587
588
589
590 stop1 := r.moduleCacheCache.ScanAndListen(ctx, processDir)
591 defer stop1()
592 stop2 := r.otherCache.ScanAndListen(ctx, processDir)
593 defer stop2()
594
595
596
597 skip := func(root gopathwalk.Root, dir string) bool {
598 if r.env.SkipPathInScan != nil && root.Type == gopathwalk.RootCurrentModule {
599 if root.Path == dir {
600 return false
601 }
602
603 if r.env.SkipPathInScan(filepath.Clean(dir)) {
604 return true
605 }
606 }
607
608 info, ok := r.cacheLoad(dir)
609 if !ok {
610 return false
611 }
612
613
614
615 packageScanned, _ := info.reachedStatus(directoryScanned)
616 return packageScanned
617 }
618
619 add := func(root gopathwalk.Root, dir string) {
620 r.cacheStore(r.scanDirForPackage(root, dir))
621 }
622
623
624
625 roots := filterRoots(r.roots, callback.rootFound)
626
627
628 scanDone := make(chan struct{})
629 go func() {
630 select {
631 case <-ctx.Done():
632 return
633 case <-r.scanSema:
634 }
635 defer func() { r.scanSema <- struct{}{} }()
636
637 for _, root := range roots {
638 if ctx.Err() != nil {
639 return
640 }
641
642 if r.scannedRoots[root] {
643 continue
644 }
645 gopathwalk.WalkSkip([]gopathwalk.Root{root}, add, skip, gopathwalk.Options{Logf: r.env.Logf, ModulesEnabled: true})
646 r.scannedRoots[root] = true
647 }
648 close(scanDone)
649 }()
650 select {
651 case <-ctx.Done():
652 case <-scanDone:
653 }
654 return nil
655 }
656
657 func (r *ModuleResolver) scoreImportPath(ctx context.Context, path string) float64 {
658 if stdlib.HasPackage(path) {
659 return MaxRelevance
660 }
661 mod, _ := r.findPackage(path)
662 return modRelevance(mod)
663 }
664
665 func modRelevance(mod *gocommand.ModuleJSON) float64 {
666 var relevance float64
667 switch {
668 case mod == nil:
669 return MaxRelevance - 4
670 case mod.Indirect:
671 relevance = MaxRelevance - 3
672 case !mod.Main:
673 relevance = MaxRelevance - 2
674 default:
675 relevance = MaxRelevance - 1
676 }
677
678 _, versionString, ok := module.SplitPathVersion(mod.Path)
679 if ok {
680 index := strings.Index(versionString, "v")
681 if index == -1 {
682 return relevance
683 }
684 if versionNumber, err := strconv.ParseFloat(versionString[index+1:], 64); err == nil {
685 relevance += versionNumber / 1000
686 }
687 }
688
689 return relevance
690 }
691
692
693
694 func (r *ModuleResolver) canonicalize(info directoryPackageInfo) (*pkg, error) {
695
696 if info.rootType == gopathwalk.RootGOROOT {
697 return &pkg{
698 importPathShort: info.nonCanonicalImportPath,
699 dir: info.dir,
700 packageName: path.Base(info.nonCanonicalImportPath),
701 relevance: MaxRelevance,
702 }, nil
703 }
704
705 importPath := info.nonCanonicalImportPath
706 mod := r.findModuleByDir(info.dir)
707
708 if mod != nil {
709
710
711 if mod.Dir == info.dir {
712 importPath = mod.Path
713 } else {
714 dirInMod := info.dir[len(mod.Dir)+len("/"):]
715 importPath = path.Join(mod.Path, filepath.ToSlash(dirInMod))
716 }
717 } else if !strings.HasPrefix(importPath, info.moduleName) {
718
719
720 return nil, fmt.Errorf("package in %q is not valid without a replace statement", info.dir)
721 }
722
723 res := &pkg{
724 importPathShort: importPath,
725 dir: info.dir,
726 relevance: modRelevance(mod),
727 }
728
729
730 if _, canonicalDir := r.findPackage(importPath); canonicalDir != "" {
731 res.dir = canonicalDir
732 }
733 return res, nil
734 }
735
736 func (r *ModuleResolver) loadExports(ctx context.Context, pkg *pkg, includeTest bool) (string, []stdlib.Symbol, error) {
737 if info, ok := r.cacheLoad(pkg.dir); ok && !includeTest {
738 return r.cacheExports(ctx, r.env, info)
739 }
740 return loadExportsFromFiles(ctx, r.env, pkg.dir, includeTest)
741 }
742
743 func (r *ModuleResolver) scanDirForPackage(root gopathwalk.Root, dir string) directoryPackageInfo {
744 subdir := ""
745 if dir != root.Path {
746 subdir = dir[len(root.Path)+len("/"):]
747 }
748 importPath := filepath.ToSlash(subdir)
749 if strings.HasPrefix(importPath, "vendor/") {
750
751 return directoryPackageInfo{
752 status: directoryScanned,
753 err: fmt.Errorf("unwanted vendor directory"),
754 }
755 }
756 switch root.Type {
757 case gopathwalk.RootCurrentModule:
758 importPath = path.Join(r.mainByDir[root.Path].Path, filepath.ToSlash(subdir))
759 case gopathwalk.RootModuleCache:
760 matches := modCacheRegexp.FindStringSubmatch(subdir)
761 if len(matches) == 0 {
762 return directoryPackageInfo{
763 status: directoryScanned,
764 err: fmt.Errorf("invalid module cache path: %v", subdir),
765 }
766 }
767 modPath, err := module.UnescapePath(filepath.ToSlash(matches[1]))
768 if err != nil {
769 if r.env.Logf != nil {
770 r.env.Logf("decoding module cache path %q: %v", subdir, err)
771 }
772 return directoryPackageInfo{
773 status: directoryScanned,
774 err: fmt.Errorf("decoding module cache path %q: %v", subdir, err),
775 }
776 }
777 importPath = path.Join(modPath, filepath.ToSlash(matches[3]))
778 }
779
780 modDir, modName := r.modInfo(dir)
781 result := directoryPackageInfo{
782 status: directoryScanned,
783 dir: dir,
784 rootType: root.Type,
785 nonCanonicalImportPath: importPath,
786 moduleDir: modDir,
787 moduleName: modName,
788 }
789 if root.Type == gopathwalk.RootGOROOT {
790
791 return result
792 }
793 return result
794 }
795
796
797 var modCacheRegexp = regexp.MustCompile(`(.*)@([^/\\]*)(.*)`)
798
799 var (
800 slashSlash = []byte("//")
801 moduleStr = []byte("module")
802 )
803
804
805
806
807
808
809 func modulePath(mod []byte) string {
810 for len(mod) > 0 {
811 line := mod
812 mod = nil
813 if i := bytes.IndexByte(line, '\n'); i >= 0 {
814 line, mod = line[:i], line[i+1:]
815 }
816 if i := bytes.Index(line, slashSlash); i >= 0 {
817 line = line[:i]
818 }
819 line = bytes.TrimSpace(line)
820 if !bytes.HasPrefix(line, moduleStr) {
821 continue
822 }
823 line = line[len(moduleStr):]
824 n := len(line)
825 line = bytes.TrimSpace(line)
826 if len(line) == n || len(line) == 0 {
827 continue
828 }
829
830 if line[0] == '"' || line[0] == '`' {
831 p, err := strconv.Unquote(string(line))
832 if err != nil {
833 return ""
834 }
835 return p
836 }
837
838 return string(line)
839 }
840 return ""
841 }
842
View as plain text