github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/imports/mod.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package imports
     6  
     7  import (
     8  	"bytes"
     9  	"context"
    10  	"encoding/json"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"os"
    14  	"path"
    15  	"path/filepath"
    16  	"regexp"
    17  	"sort"
    18  	"strconv"
    19  	"strings"
    20  
    21  	"golang.org/x/mod/module"
    22  	"github.com/powerman/golang-tools/internal/gocommand"
    23  	"github.com/powerman/golang-tools/internal/gopathwalk"
    24  )
    25  
    26  // ModuleResolver implements resolver for modules using the go command as little
    27  // as feasible.
    28  type ModuleResolver struct {
    29  	env            *ProcessEnv
    30  	moduleCacheDir string
    31  	dummyVendorMod *gocommand.ModuleJSON // If vendoring is enabled, the pseudo-module that represents the /vendor directory.
    32  	roots          []gopathwalk.Root
    33  	scanSema       chan struct{} // scanSema prevents concurrent scans and guards scannedRoots.
    34  	scannedRoots   map[gopathwalk.Root]bool
    35  
    36  	initialized   bool
    37  	mains         []*gocommand.ModuleJSON
    38  	mainByDir     map[string]*gocommand.ModuleJSON
    39  	modsByModPath []*gocommand.ModuleJSON // All modules, ordered by # of path components in module Path...
    40  	modsByDir     []*gocommand.ModuleJSON // ...or Dir.
    41  
    42  	// moduleCacheCache stores information about the module cache.
    43  	moduleCacheCache *dirInfoCache
    44  	otherCache       *dirInfoCache
    45  }
    46  
    47  func newModuleResolver(e *ProcessEnv) *ModuleResolver {
    48  	r := &ModuleResolver{
    49  		env:      e,
    50  		scanSema: make(chan struct{}, 1),
    51  	}
    52  	r.scanSema <- struct{}{}
    53  	return r
    54  }
    55  
    56  func (r *ModuleResolver) init() error {
    57  	if r.initialized {
    58  		return nil
    59  	}
    60  
    61  	goenv, err := r.env.goEnv()
    62  	if err != nil {
    63  		return err
    64  	}
    65  	inv := gocommand.Invocation{
    66  		BuildFlags: r.env.BuildFlags,
    67  		ModFlag:    r.env.ModFlag,
    68  		ModFile:    r.env.ModFile,
    69  		Env:        r.env.env(),
    70  		Logf:       r.env.Logf,
    71  		WorkingDir: r.env.WorkingDir,
    72  	}
    73  	vendorEnabled, mainModVendor, err := gocommand.VendorEnabled(context.TODO(), inv, r.env.GocmdRunner)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	if mainModVendor != nil && vendorEnabled {
    79  		// Vendor mode is on, so all the non-Main modules are irrelevant,
    80  		// and we need to search /vendor for everything.
    81  		r.mains = []*gocommand.ModuleJSON{mainModVendor}
    82  		r.dummyVendorMod = &gocommand.ModuleJSON{
    83  			Path: "",
    84  			Dir:  filepath.Join(mainModVendor.Dir, "vendor"),
    85  		}
    86  		r.modsByModPath = []*gocommand.ModuleJSON{mainModVendor, r.dummyVendorMod}
    87  		r.modsByDir = []*gocommand.ModuleJSON{mainModVendor, r.dummyVendorMod}
    88  	} else {
    89  		// Vendor mode is off, so run go list -m ... to find everything.
    90  		err := r.initAllMods()
    91  		// We expect an error when running outside of a module with
    92  		// GO111MODULE=on. Other errors are fatal.
    93  		if err != nil {
    94  			if errMsg := err.Error(); !strings.Contains(errMsg, "working directory is not part of a module") && !strings.Contains(errMsg, "go.mod file not found") {
    95  				return err
    96  			}
    97  		}
    98  	}
    99  
   100  	if gmc := r.env.Env["GOMODCACHE"]; gmc != "" {
   101  		r.moduleCacheDir = gmc
   102  	} else {
   103  		gopaths := filepath.SplitList(goenv["GOPATH"])
   104  		if len(gopaths) == 0 {
   105  			return fmt.Errorf("empty GOPATH")
   106  		}
   107  		r.moduleCacheDir = filepath.Join(gopaths[0], "/pkg/mod")
   108  	}
   109  
   110  	sort.Slice(r.modsByModPath, func(i, j int) bool {
   111  		count := func(x int) int {
   112  			return strings.Count(r.modsByModPath[x].Path, "/")
   113  		}
   114  		return count(j) < count(i) // descending order
   115  	})
   116  	sort.Slice(r.modsByDir, func(i, j int) bool {
   117  		count := func(x int) int {
   118  			return strings.Count(r.modsByDir[x].Dir, "/")
   119  		}
   120  		return count(j) < count(i) // descending order
   121  	})
   122  
   123  	r.roots = []gopathwalk.Root{
   124  		{filepath.Join(goenv["GOROOT"], "/src"), gopathwalk.RootGOROOT},
   125  	}
   126  	r.mainByDir = make(map[string]*gocommand.ModuleJSON)
   127  	for _, main := range r.mains {
   128  		r.roots = append(r.roots, gopathwalk.Root{main.Dir, gopathwalk.RootCurrentModule})
   129  		r.mainByDir[main.Dir] = main
   130  	}
   131  	if vendorEnabled {
   132  		r.roots = append(r.roots, gopathwalk.Root{r.dummyVendorMod.Dir, gopathwalk.RootOther})
   133  	} else {
   134  		addDep := func(mod *gocommand.ModuleJSON) {
   135  			if mod.Replace == nil {
   136  				// This is redundant with the cache, but we'll skip it cheaply enough.
   137  				r.roots = append(r.roots, gopathwalk.Root{mod.Dir, gopathwalk.RootModuleCache})
   138  			} else {
   139  				r.roots = append(r.roots, gopathwalk.Root{mod.Dir, gopathwalk.RootOther})
   140  			}
   141  		}
   142  		// Walk dependent modules before scanning the full mod cache, direct deps first.
   143  		for _, mod := range r.modsByModPath {
   144  			if !mod.Indirect && !mod.Main {
   145  				addDep(mod)
   146  			}
   147  		}
   148  		for _, mod := range r.modsByModPath {
   149  			if mod.Indirect && !mod.Main {
   150  				addDep(mod)
   151  			}
   152  		}
   153  		r.roots = append(r.roots, gopathwalk.Root{r.moduleCacheDir, gopathwalk.RootModuleCache})
   154  	}
   155  
   156  	r.scannedRoots = map[gopathwalk.Root]bool{}
   157  	if r.moduleCacheCache == nil {
   158  		r.moduleCacheCache = &dirInfoCache{
   159  			dirs:      map[string]*directoryPackageInfo{},
   160  			listeners: map[*int]cacheListener{},
   161  		}
   162  	}
   163  	if r.otherCache == nil {
   164  		r.otherCache = &dirInfoCache{
   165  			dirs:      map[string]*directoryPackageInfo{},
   166  			listeners: map[*int]cacheListener{},
   167  		}
   168  	}
   169  	r.initialized = true
   170  	return nil
   171  }
   172  
   173  func (r *ModuleResolver) initAllMods() error {
   174  	stdout, err := r.env.invokeGo(context.TODO(), "list", "-m", "-e", "-json", "...")
   175  	if err != nil {
   176  		return err
   177  	}
   178  	for dec := json.NewDecoder(stdout); dec.More(); {
   179  		mod := &gocommand.ModuleJSON{}
   180  		if err := dec.Decode(mod); err != nil {
   181  			return err
   182  		}
   183  		if mod.Dir == "" {
   184  			if r.env.Logf != nil {
   185  				r.env.Logf("module %v has not been downloaded and will be ignored", mod.Path)
   186  			}
   187  			// Can't do anything with a module that's not downloaded.
   188  			continue
   189  		}
   190  		// golang/go#36193: the go command doesn't always clean paths.
   191  		mod.Dir = filepath.Clean(mod.Dir)
   192  		r.modsByModPath = append(r.modsByModPath, mod)
   193  		r.modsByDir = append(r.modsByDir, mod)
   194  		if mod.Main {
   195  			r.mains = append(r.mains, mod)
   196  		}
   197  	}
   198  	return nil
   199  }
   200  
   201  func (r *ModuleResolver) ClearForNewScan() {
   202  	<-r.scanSema
   203  	r.scannedRoots = map[gopathwalk.Root]bool{}
   204  	r.otherCache = &dirInfoCache{
   205  		dirs:      map[string]*directoryPackageInfo{},
   206  		listeners: map[*int]cacheListener{},
   207  	}
   208  	r.scanSema <- struct{}{}
   209  }
   210  
   211  func (r *ModuleResolver) ClearForNewMod() {
   212  	<-r.scanSema
   213  	*r = ModuleResolver{
   214  		env:              r.env,
   215  		moduleCacheCache: r.moduleCacheCache,
   216  		otherCache:       r.otherCache,
   217  		scanSema:         r.scanSema,
   218  	}
   219  	r.init()
   220  	r.scanSema <- struct{}{}
   221  }
   222  
   223  // findPackage returns the module and directory that contains the package at
   224  // the given import path, or returns nil, "" if no module is in scope.
   225  func (r *ModuleResolver) findPackage(importPath string) (*gocommand.ModuleJSON, string) {
   226  	// This can't find packages in the stdlib, but that's harmless for all
   227  	// the existing code paths.
   228  	for _, m := range r.modsByModPath {
   229  		if !strings.HasPrefix(importPath, m.Path) {
   230  			continue
   231  		}
   232  		pathInModule := importPath[len(m.Path):]
   233  		pkgDir := filepath.Join(m.Dir, pathInModule)
   234  		if r.dirIsNestedModule(pkgDir, m) {
   235  			continue
   236  		}
   237  
   238  		if info, ok := r.cacheLoad(pkgDir); ok {
   239  			if loaded, err := info.reachedStatus(nameLoaded); loaded {
   240  				if err != nil {
   241  					continue // No package in this dir.
   242  				}
   243  				return m, pkgDir
   244  			}
   245  			if scanned, err := info.reachedStatus(directoryScanned); scanned && err != nil {
   246  				continue // Dir is unreadable, etc.
   247  			}
   248  			// This is slightly wrong: a directory doesn't have to have an
   249  			// importable package to count as a package for package-to-module
   250  			// resolution. package main or _test files should count but
   251  			// don't.
   252  			// TODO(heschi): fix this.
   253  			if _, err := r.cachePackageName(info); err == nil {
   254  				return m, pkgDir
   255  			}
   256  		}
   257  
   258  		// Not cached. Read the filesystem.
   259  		pkgFiles, err := ioutil.ReadDir(pkgDir)
   260  		if err != nil {
   261  			continue
   262  		}
   263  		// A module only contains a package if it has buildable go
   264  		// files in that directory. If not, it could be provided by an
   265  		// outer module. See #29736.
   266  		for _, fi := range pkgFiles {
   267  			if ok, _ := r.env.matchFile(pkgDir, fi.Name()); ok {
   268  				return m, pkgDir
   269  			}
   270  		}
   271  	}
   272  	return nil, ""
   273  }
   274  
   275  func (r *ModuleResolver) cacheLoad(dir string) (directoryPackageInfo, bool) {
   276  	if info, ok := r.moduleCacheCache.Load(dir); ok {
   277  		return info, ok
   278  	}
   279  	return r.otherCache.Load(dir)
   280  }
   281  
   282  func (r *ModuleResolver) cacheStore(info directoryPackageInfo) {
   283  	if info.rootType == gopathwalk.RootModuleCache {
   284  		r.moduleCacheCache.Store(info.dir, info)
   285  	} else {
   286  		r.otherCache.Store(info.dir, info)
   287  	}
   288  }
   289  
   290  func (r *ModuleResolver) cacheKeys() []string {
   291  	return append(r.moduleCacheCache.Keys(), r.otherCache.Keys()...)
   292  }
   293  
   294  // cachePackageName caches the package name for a dir already in the cache.
   295  func (r *ModuleResolver) cachePackageName(info directoryPackageInfo) (string, error) {
   296  	if info.rootType == gopathwalk.RootModuleCache {
   297  		return r.moduleCacheCache.CachePackageName(info)
   298  	}
   299  	return r.otherCache.CachePackageName(info)
   300  }
   301  
   302  func (r *ModuleResolver) cacheExports(ctx context.Context, env *ProcessEnv, info directoryPackageInfo) (string, []string, error) {
   303  	if info.rootType == gopathwalk.RootModuleCache {
   304  		return r.moduleCacheCache.CacheExports(ctx, env, info)
   305  	}
   306  	return r.otherCache.CacheExports(ctx, env, info)
   307  }
   308  
   309  // findModuleByDir returns the module that contains dir, or nil if no such
   310  // module is in scope.
   311  func (r *ModuleResolver) findModuleByDir(dir string) *gocommand.ModuleJSON {
   312  	// This is quite tricky and may not be correct. dir could be:
   313  	// - a package in the main module.
   314  	// - a replace target underneath the main module's directory.
   315  	//    - a nested module in the above.
   316  	// - a replace target somewhere totally random.
   317  	//    - a nested module in the above.
   318  	// - in the mod cache.
   319  	// - in /vendor/ in -mod=vendor mode.
   320  	//    - nested module? Dunno.
   321  	// Rumor has it that replace targets cannot contain other replace targets.
   322  	for _, m := range r.modsByDir {
   323  		if !strings.HasPrefix(dir, m.Dir) {
   324  			continue
   325  		}
   326  
   327  		if r.dirIsNestedModule(dir, m) {
   328  			continue
   329  		}
   330  
   331  		return m
   332  	}
   333  	return nil
   334  }
   335  
   336  // dirIsNestedModule reports if dir is contained in a nested module underneath
   337  // mod, not actually in mod.
   338  func (r *ModuleResolver) dirIsNestedModule(dir string, mod *gocommand.ModuleJSON) bool {
   339  	if !strings.HasPrefix(dir, mod.Dir) {
   340  		return false
   341  	}
   342  	if r.dirInModuleCache(dir) {
   343  		// Nested modules in the module cache are pruned,
   344  		// so it cannot be a nested module.
   345  		return false
   346  	}
   347  	if mod != nil && mod == r.dummyVendorMod {
   348  		// The /vendor pseudomodule is flattened and doesn't actually count.
   349  		return false
   350  	}
   351  	modDir, _ := r.modInfo(dir)
   352  	if modDir == "" {
   353  		return false
   354  	}
   355  	return modDir != mod.Dir
   356  }
   357  
   358  func (r *ModuleResolver) modInfo(dir string) (modDir string, modName string) {
   359  	readModName := func(modFile string) string {
   360  		modBytes, err := ioutil.ReadFile(modFile)
   361  		if err != nil {
   362  			return ""
   363  		}
   364  		return modulePath(modBytes)
   365  	}
   366  
   367  	if r.dirInModuleCache(dir) {
   368  		if matches := modCacheRegexp.FindStringSubmatch(dir); len(matches) == 3 {
   369  			index := strings.Index(dir, matches[1]+"@"+matches[2])
   370  			modDir := filepath.Join(dir[:index], matches[1]+"@"+matches[2])
   371  			return modDir, readModName(filepath.Join(modDir, "go.mod"))
   372  		}
   373  	}
   374  	for {
   375  		if info, ok := r.cacheLoad(dir); ok {
   376  			return info.moduleDir, info.moduleName
   377  		}
   378  		f := filepath.Join(dir, "go.mod")
   379  		info, err := os.Stat(f)
   380  		if err == nil && !info.IsDir() {
   381  			return dir, readModName(f)
   382  		}
   383  
   384  		d := filepath.Dir(dir)
   385  		if len(d) >= len(dir) {
   386  			return "", "" // reached top of file system, no go.mod
   387  		}
   388  		dir = d
   389  	}
   390  }
   391  
   392  func (r *ModuleResolver) dirInModuleCache(dir string) bool {
   393  	if r.moduleCacheDir == "" {
   394  		return false
   395  	}
   396  	return strings.HasPrefix(dir, r.moduleCacheDir)
   397  }
   398  
   399  func (r *ModuleResolver) loadPackageNames(importPaths []string, srcDir string) (map[string]string, error) {
   400  	if err := r.init(); err != nil {
   401  		return nil, err
   402  	}
   403  	names := map[string]string{}
   404  	for _, path := range importPaths {
   405  		_, packageDir := r.findPackage(path)
   406  		if packageDir == "" {
   407  			continue
   408  		}
   409  		name, err := packageDirToName(packageDir)
   410  		if err != nil {
   411  			continue
   412  		}
   413  		names[path] = name
   414  	}
   415  	return names, nil
   416  }
   417  
   418  func (r *ModuleResolver) scan(ctx context.Context, callback *scanCallback) error {
   419  	if err := r.init(); err != nil {
   420  		return err
   421  	}
   422  
   423  	processDir := func(info directoryPackageInfo) {
   424  		// Skip this directory if we were not able to get the package information successfully.
   425  		if scanned, err := info.reachedStatus(directoryScanned); !scanned || err != nil {
   426  			return
   427  		}
   428  		pkg, err := r.canonicalize(info)
   429  		if err != nil {
   430  			return
   431  		}
   432  
   433  		if !callback.dirFound(pkg) {
   434  			return
   435  		}
   436  		pkg.packageName, err = r.cachePackageName(info)
   437  		if err != nil {
   438  			return
   439  		}
   440  
   441  		if !callback.packageNameLoaded(pkg) {
   442  			return
   443  		}
   444  		_, exports, err := r.loadExports(ctx, pkg, false)
   445  		if err != nil {
   446  			return
   447  		}
   448  		callback.exportsLoaded(pkg, exports)
   449  	}
   450  
   451  	// Start processing everything in the cache, and listen for the new stuff
   452  	// we discover in the walk below.
   453  	stop1 := r.moduleCacheCache.ScanAndListen(ctx, processDir)
   454  	defer stop1()
   455  	stop2 := r.otherCache.ScanAndListen(ctx, processDir)
   456  	defer stop2()
   457  
   458  	// We assume cached directories are fully cached, including all their
   459  	// children, and have not changed. We can skip them.
   460  	skip := func(root gopathwalk.Root, dir string) bool {
   461  		info, ok := r.cacheLoad(dir)
   462  		if !ok {
   463  			return false
   464  		}
   465  		// This directory can be skipped as long as we have already scanned it.
   466  		// Packages with errors will continue to have errors, so there is no need
   467  		// to rescan them.
   468  		packageScanned, _ := info.reachedStatus(directoryScanned)
   469  		return packageScanned
   470  	}
   471  
   472  	// Add anything new to the cache, and process it if we're still listening.
   473  	add := func(root gopathwalk.Root, dir string) {
   474  		r.cacheStore(r.scanDirForPackage(root, dir))
   475  	}
   476  
   477  	// r.roots and the callback are not necessarily safe to use in the
   478  	// goroutine below. Process them eagerly.
   479  	roots := filterRoots(r.roots, callback.rootFound)
   480  	// We can't cancel walks, because we need them to finish to have a usable
   481  	// cache. Instead, run them in a separate goroutine and detach.
   482  	scanDone := make(chan struct{})
   483  	go func() {
   484  		select {
   485  		case <-ctx.Done():
   486  			return
   487  		case <-r.scanSema:
   488  		}
   489  		defer func() { r.scanSema <- struct{}{} }()
   490  		// We have the lock on r.scannedRoots, and no other scans can run.
   491  		for _, root := range roots {
   492  			if ctx.Err() != nil {
   493  				return
   494  			}
   495  
   496  			if r.scannedRoots[root] {
   497  				continue
   498  			}
   499  			gopathwalk.WalkSkip([]gopathwalk.Root{root}, add, skip, gopathwalk.Options{Logf: r.env.Logf, ModulesEnabled: true})
   500  			r.scannedRoots[root] = true
   501  		}
   502  		close(scanDone)
   503  	}()
   504  	select {
   505  	case <-ctx.Done():
   506  	case <-scanDone:
   507  	}
   508  	return nil
   509  }
   510  
   511  func (r *ModuleResolver) scoreImportPath(ctx context.Context, path string) float64 {
   512  	if _, ok := stdlib[path]; ok {
   513  		return MaxRelevance
   514  	}
   515  	mod, _ := r.findPackage(path)
   516  	return modRelevance(mod)
   517  }
   518  
   519  func modRelevance(mod *gocommand.ModuleJSON) float64 {
   520  	var relevance float64
   521  	switch {
   522  	case mod == nil: // out of scope
   523  		return MaxRelevance - 4
   524  	case mod.Indirect:
   525  		relevance = MaxRelevance - 3
   526  	case !mod.Main:
   527  		relevance = MaxRelevance - 2
   528  	default:
   529  		relevance = MaxRelevance - 1 // main module ties with stdlib
   530  	}
   531  
   532  	_, versionString, ok := module.SplitPathVersion(mod.Path)
   533  	if ok {
   534  		index := strings.Index(versionString, "v")
   535  		if index == -1 {
   536  			return relevance
   537  		}
   538  		if versionNumber, err := strconv.ParseFloat(versionString[index+1:], 64); err == nil {
   539  			relevance += versionNumber / 1000
   540  		}
   541  	}
   542  
   543  	return relevance
   544  }
   545  
   546  // canonicalize gets the result of canonicalizing the packages using the results
   547  // of initializing the resolver from 'go list -m'.
   548  func (r *ModuleResolver) canonicalize(info directoryPackageInfo) (*pkg, error) {
   549  	// Packages in GOROOT are already canonical, regardless of the std/cmd modules.
   550  	if info.rootType == gopathwalk.RootGOROOT {
   551  		return &pkg{
   552  			importPathShort: info.nonCanonicalImportPath,
   553  			dir:             info.dir,
   554  			packageName:     path.Base(info.nonCanonicalImportPath),
   555  			relevance:       MaxRelevance,
   556  		}, nil
   557  	}
   558  
   559  	importPath := info.nonCanonicalImportPath
   560  	mod := r.findModuleByDir(info.dir)
   561  	// Check if the directory is underneath a module that's in scope.
   562  	if mod != nil {
   563  		// It is. If dir is the target of a replace directive,
   564  		// our guessed import path is wrong. Use the real one.
   565  		if mod.Dir == info.dir {
   566  			importPath = mod.Path
   567  		} else {
   568  			dirInMod := info.dir[len(mod.Dir)+len("/"):]
   569  			importPath = path.Join(mod.Path, filepath.ToSlash(dirInMod))
   570  		}
   571  	} else if !strings.HasPrefix(importPath, info.moduleName) {
   572  		// The module's name doesn't match the package's import path. It
   573  		// probably needs a replace directive we don't have.
   574  		return nil, fmt.Errorf("package in %q is not valid without a replace statement", info.dir)
   575  	}
   576  
   577  	res := &pkg{
   578  		importPathShort: importPath,
   579  		dir:             info.dir,
   580  		relevance:       modRelevance(mod),
   581  	}
   582  	// We may have discovered a package that has a different version
   583  	// in scope already. Canonicalize to that one if possible.
   584  	if _, canonicalDir := r.findPackage(importPath); canonicalDir != "" {
   585  		res.dir = canonicalDir
   586  	}
   587  	return res, nil
   588  }
   589  
   590  func (r *ModuleResolver) loadExports(ctx context.Context, pkg *pkg, includeTest bool) (string, []string, error) {
   591  	if err := r.init(); err != nil {
   592  		return "", nil, err
   593  	}
   594  	if info, ok := r.cacheLoad(pkg.dir); ok && !includeTest {
   595  		return r.cacheExports(ctx, r.env, info)
   596  	}
   597  	return loadExportsFromFiles(ctx, r.env, pkg.dir, includeTest)
   598  }
   599  
   600  func (r *ModuleResolver) scanDirForPackage(root gopathwalk.Root, dir string) directoryPackageInfo {
   601  	subdir := ""
   602  	if dir != root.Path {
   603  		subdir = dir[len(root.Path)+len("/"):]
   604  	}
   605  	importPath := filepath.ToSlash(subdir)
   606  	if strings.HasPrefix(importPath, "vendor/") {
   607  		// Only enter vendor directories if they're explicitly requested as a root.
   608  		return directoryPackageInfo{
   609  			status: directoryScanned,
   610  			err:    fmt.Errorf("unwanted vendor directory"),
   611  		}
   612  	}
   613  	switch root.Type {
   614  	case gopathwalk.RootCurrentModule:
   615  		importPath = path.Join(r.mainByDir[root.Path].Path, filepath.ToSlash(subdir))
   616  	case gopathwalk.RootModuleCache:
   617  		matches := modCacheRegexp.FindStringSubmatch(subdir)
   618  		if len(matches) == 0 {
   619  			return directoryPackageInfo{
   620  				status: directoryScanned,
   621  				err:    fmt.Errorf("invalid module cache path: %v", subdir),
   622  			}
   623  		}
   624  		modPath, err := module.UnescapePath(filepath.ToSlash(matches[1]))
   625  		if err != nil {
   626  			if r.env.Logf != nil {
   627  				r.env.Logf("decoding module cache path %q: %v", subdir, err)
   628  			}
   629  			return directoryPackageInfo{
   630  				status: directoryScanned,
   631  				err:    fmt.Errorf("decoding module cache path %q: %v", subdir, err),
   632  			}
   633  		}
   634  		importPath = path.Join(modPath, filepath.ToSlash(matches[3]))
   635  	}
   636  
   637  	modDir, modName := r.modInfo(dir)
   638  	result := directoryPackageInfo{
   639  		status:                 directoryScanned,
   640  		dir:                    dir,
   641  		rootType:               root.Type,
   642  		nonCanonicalImportPath: importPath,
   643  		moduleDir:              modDir,
   644  		moduleName:             modName,
   645  	}
   646  	if root.Type == gopathwalk.RootGOROOT {
   647  		// stdlib packages are always in scope, despite the confusing go.mod
   648  		return result
   649  	}
   650  	return result
   651  }
   652  
   653  // modCacheRegexp splits a path in a module cache into module, module version, and package.
   654  var modCacheRegexp = regexp.MustCompile(`(.*)@([^/\\]*)(.*)`)
   655  
   656  var (
   657  	slashSlash = []byte("//")
   658  	moduleStr  = []byte("module")
   659  )
   660  
   661  // modulePath returns the module path from the gomod file text.
   662  // If it cannot find a module path, it returns an empty string.
   663  // It is tolerant of unrelated problems in the go.mod file.
   664  //
   665  // Copied from cmd/go/internal/modfile.
   666  func modulePath(mod []byte) string {
   667  	for len(mod) > 0 {
   668  		line := mod
   669  		mod = nil
   670  		if i := bytes.IndexByte(line, '\n'); i >= 0 {
   671  			line, mod = line[:i], line[i+1:]
   672  		}
   673  		if i := bytes.Index(line, slashSlash); i >= 0 {
   674  			line = line[:i]
   675  		}
   676  		line = bytes.TrimSpace(line)
   677  		if !bytes.HasPrefix(line, moduleStr) {
   678  			continue
   679  		}
   680  		line = line[len(moduleStr):]
   681  		n := len(line)
   682  		line = bytes.TrimSpace(line)
   683  		if len(line) == n || len(line) == 0 {
   684  			continue
   685  		}
   686  
   687  		if line[0] == '"' || line[0] == '`' {
   688  			p, err := strconv.Unquote(string(line))
   689  			if err != nil {
   690  				return "" // malformed quoted string or multiline module path
   691  			}
   692  			return p
   693  		}
   694  
   695  		return string(line)
   696  	}
   697  	return "" // missing module path
   698  }