cuelang.org/go@v0.10.1/internal/golangorgx/tools/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  	"os"
    13  	"path"
    14  	"path/filepath"
    15  	"regexp"
    16  	"sort"
    17  	"strconv"
    18  	"strings"
    19  
    20  	"cuelang.org/go/internal/golangorgx/tools/event"
    21  	"cuelang.org/go/internal/golangorgx/tools/gocommand"
    22  	"cuelang.org/go/internal/golangorgx/tools/gopathwalk"
    23  	"golang.org/x/mod/module"
    24  )
    25  
    26  // Notes(rfindley): ModuleResolver appears to be heavily optimized for scanning
    27  // as fast as possible, which is desirable for a call to goimports from the
    28  // command line, but it doesn't work as well for gopls, where it suffers from
    29  // slow startup (golang/go#44863) and intermittent hanging (golang/go#59216),
    30  // both caused by populating the cache, albeit in slightly different ways.
    31  //
    32  // A high level list of TODOs:
    33  //  - Optimize the scan itself, as there is some redundancy statting and
    34  //    reading go.mod files.
    35  //  - Invert the relationship between ProcessEnv and Resolver (see the
    36  //    docstring of ProcessEnv).
    37  //  - Make it easier to use an external resolver implementation.
    38  //
    39  // Smaller TODOs are annotated in the code below.
    40  
    41  // ModuleResolver implements the Resolver interface for a workspace using
    42  // modules.
    43  //
    44  // A goal of the ModuleResolver is to invoke the Go command as little as
    45  // possible. To this end, it runs the Go command only for listing module
    46  // information (i.e. `go list -m -e -json ...`). Package scanning, the process
    47  // of loading package information for the modules, is implemented internally
    48  // via the scan method.
    49  //
    50  // It has two types of state: the state derived from the go command, which
    51  // is populated by init, and the state derived from scans, which is populated
    52  // via scan. A root is considered scanned if it has been walked to discover
    53  // directories. However, if the scan did not require additional information
    54  // from the directory (such as package name or exports), the directory
    55  // information itself may be partially populated. It will be lazily filled in
    56  // as needed by scans, using the scanCallback.
    57  type ModuleResolver struct {
    58  	env *ProcessEnv
    59  
    60  	// Module state, populated during construction
    61  	dummyVendorMod *gocommand.ModuleJSON            // if vendoring is enabled, a pseudo-module to represent the /vendor directory
    62  	moduleCacheDir string                           // GOMODCACHE, inferred from GOPATH if unset
    63  	roots          []gopathwalk.Root                // roots to scan, in approximate order of importance
    64  	mains          []*gocommand.ModuleJSON          // main modules
    65  	mainByDir      map[string]*gocommand.ModuleJSON // module information by dir, to join with roots
    66  	modsByModPath  []*gocommand.ModuleJSON          // all modules, ordered by # of path components in their module path
    67  	modsByDir      []*gocommand.ModuleJSON          // ...or by the number of path components in their Dir.
    68  
    69  	// Scanning state, populated by scan
    70  
    71  	// scanSema prevents concurrent scans, and guards scannedRoots and the cache
    72  	// fields below (though the caches themselves are concurrency safe).
    73  	// Receive to acquire, send to release.
    74  	scanSema     chan struct{}
    75  	scannedRoots map[gopathwalk.Root]bool // if true, root has been walked
    76  
    77  	// Caches of directory info, populated by scans and scan callbacks
    78  	//
    79  	// moduleCacheCache stores cached information about roots in the module
    80  	// cache, which are immutable and therefore do not need to be invalidated.
    81  	//
    82  	// otherCache stores information about all other roots (even GOROOT), which
    83  	// may change.
    84  	moduleCacheCache *DirInfoCache
    85  	otherCache       *DirInfoCache
    86  }
    87  
    88  // newModuleResolver returns a new module-aware goimports resolver.
    89  //
    90  // Note: use caution when modifying this constructor: changes must also be
    91  // reflected in ModuleResolver.ClearForNewScan.
    92  func newModuleResolver(e *ProcessEnv, moduleCacheCache *DirInfoCache) (*ModuleResolver, error) {
    93  	r := &ModuleResolver{
    94  		env:      e,
    95  		scanSema: make(chan struct{}, 1),
    96  	}
    97  	r.scanSema <- struct{}{} // release
    98  
    99  	goenv, err := r.env.goEnv()
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	// TODO(rfindley): can we refactor to share logic with r.env.invokeGo?
   105  	inv := gocommand.Invocation{
   106  		BuildFlags: r.env.BuildFlags,
   107  		ModFlag:    r.env.ModFlag,
   108  		Env:        r.env.env(),
   109  		Logf:       r.env.Logf,
   110  		WorkingDir: r.env.WorkingDir,
   111  	}
   112  
   113  	vendorEnabled := false
   114  	var mainModVendor *gocommand.ModuleJSON
   115  
   116  	// Module vendor directories are ignored in workspace mode:
   117  	// https://go.googlesource.com/proposal/+/master/design/45713-workspace.md
   118  	if len(r.env.Env["GOWORK"]) == 0 {
   119  		// TODO(rfindley): VendorEnabled runs the go command to get GOFLAGS, but
   120  		// they should be available from the ProcessEnv. Can we avoid the redundant
   121  		// invocation?
   122  		vendorEnabled, mainModVendor, err = gocommand.VendorEnabled(context.TODO(), inv, r.env.GocmdRunner)
   123  		if err != nil {
   124  			return nil, err
   125  		}
   126  	}
   127  
   128  	if mainModVendor != nil && vendorEnabled {
   129  		// Vendor mode is on, so all the non-Main modules are irrelevant,
   130  		// and we need to search /vendor for everything.
   131  		r.mains = []*gocommand.ModuleJSON{mainModVendor}
   132  		r.dummyVendorMod = &gocommand.ModuleJSON{
   133  			Path: "",
   134  			Dir:  filepath.Join(mainModVendor.Dir, "vendor"),
   135  		}
   136  		r.modsByModPath = []*gocommand.ModuleJSON{mainModVendor, r.dummyVendorMod}
   137  		r.modsByDir = []*gocommand.ModuleJSON{mainModVendor, r.dummyVendorMod}
   138  	} else {
   139  		// Vendor mode is off, so run go list -m ... to find everything.
   140  		err := r.initAllMods()
   141  		// We expect an error when running outside of a module with
   142  		// GO111MODULE=on. Other errors are fatal.
   143  		if err != nil {
   144  			if errMsg := err.Error(); !strings.Contains(errMsg, "working directory is not part of a module") && !strings.Contains(errMsg, "go.mod file not found") {
   145  				return nil, err
   146  			}
   147  		}
   148  	}
   149  
   150  	r.moduleCacheDir = gomodcacheForEnv(goenv)
   151  	if r.moduleCacheDir == "" {
   152  		return nil, fmt.Errorf("cannot resolve GOMODCACHE")
   153  	}
   154  
   155  	sort.Slice(r.modsByModPath, func(i, j int) bool {
   156  		count := func(x int) int {
   157  			return strings.Count(r.modsByModPath[x].Path, "/")
   158  		}
   159  		return count(j) < count(i) // descending order
   160  	})
   161  	sort.Slice(r.modsByDir, func(i, j int) bool {
   162  		count := func(x int) int {
   163  			return strings.Count(r.modsByDir[x].Dir, string(filepath.Separator))
   164  		}
   165  		return count(j) < count(i) // descending order
   166  	})
   167  
   168  	r.roots = []gopathwalk.Root{
   169  		{Path: filepath.Join(goenv["GOROOT"], "/src"), Type: gopathwalk.RootGOROOT},
   170  	}
   171  	r.mainByDir = make(map[string]*gocommand.ModuleJSON)
   172  	for _, main := range r.mains {
   173  		r.roots = append(r.roots, gopathwalk.Root{Path: main.Dir, Type: gopathwalk.RootCurrentModule})
   174  		r.mainByDir[main.Dir] = main
   175  	}
   176  	if vendorEnabled {
   177  		r.roots = append(r.roots, gopathwalk.Root{Path: r.dummyVendorMod.Dir, Type: gopathwalk.RootOther})
   178  	} else {
   179  		addDep := func(mod *gocommand.ModuleJSON) {
   180  			if mod.Replace == nil {
   181  				// This is redundant with the cache, but we'll skip it cheaply enough
   182  				// when we encounter it in the module cache scan.
   183  				//
   184  				// Including it at a lower index in r.roots than the module cache dir
   185  				// helps prioritize matches from within existing dependencies.
   186  				r.roots = append(r.roots, gopathwalk.Root{Path: mod.Dir, Type: gopathwalk.RootModuleCache})
   187  			} else {
   188  				r.roots = append(r.roots, gopathwalk.Root{Path: mod.Dir, Type: gopathwalk.RootOther})
   189  			}
   190  		}
   191  		// Walk dependent modules before scanning the full mod cache, direct deps first.
   192  		for _, mod := range r.modsByModPath {
   193  			if !mod.Indirect && !mod.Main {
   194  				addDep(mod)
   195  			}
   196  		}
   197  		for _, mod := range r.modsByModPath {
   198  			if mod.Indirect && !mod.Main {
   199  				addDep(mod)
   200  			}
   201  		}
   202  		// If provided, share the moduleCacheCache.
   203  		//
   204  		// TODO(rfindley): The module cache is immutable. However, the loaded
   205  		// exports do depend on GOOS and GOARCH. Fortunately, the
   206  		// ProcessEnv.buildContext does not adjust these from build.DefaultContext
   207  		// (even though it should). So for now, this is OK to share, but we need to
   208  		// add logic for handling GOOS/GOARCH.
   209  		r.moduleCacheCache = moduleCacheCache
   210  		r.roots = append(r.roots, gopathwalk.Root{Path: r.moduleCacheDir, Type: gopathwalk.RootModuleCache})
   211  	}
   212  
   213  	r.scannedRoots = map[gopathwalk.Root]bool{}
   214  	if r.moduleCacheCache == nil {
   215  		r.moduleCacheCache = NewDirInfoCache()
   216  	}
   217  	r.otherCache = NewDirInfoCache()
   218  	return r, nil
   219  }
   220  
   221  // gomodcacheForEnv returns the GOMODCACHE value to use based on the given env
   222  // map, which must have GOMODCACHE and GOPATH populated.
   223  //
   224  // TODO(rfindley): this is defensive refactoring.
   225  //  1. Is this even relevant anymore? Can't we just read GOMODCACHE.
   226  //  2. Use this to separate module cache scanning from other scanning.
   227  func gomodcacheForEnv(goenv map[string]string) string {
   228  	if gmc := goenv["GOMODCACHE"]; gmc != "" {
   229  		return gmc
   230  	}
   231  	gopaths := filepath.SplitList(goenv["GOPATH"])
   232  	if len(gopaths) == 0 {
   233  		return ""
   234  	}
   235  	return filepath.Join(gopaths[0], "/pkg/mod")
   236  }
   237  
   238  func (r *ModuleResolver) initAllMods() error {
   239  	stdout, err := r.env.invokeGo(context.TODO(), "list", "-m", "-e", "-json", "...")
   240  	if err != nil {
   241  		return err
   242  	}
   243  	for dec := json.NewDecoder(stdout); dec.More(); {
   244  		mod := &gocommand.ModuleJSON{}
   245  		if err := dec.Decode(mod); err != nil {
   246  			return err
   247  		}
   248  		if mod.Dir == "" {
   249  			if r.env.Logf != nil {
   250  				r.env.Logf("module %v has not been downloaded and will be ignored", mod.Path)
   251  			}
   252  			// Can't do anything with a module that's not downloaded.
   253  			continue
   254  		}
   255  		// golang/go#36193: the go command doesn't always clean paths.
   256  		mod.Dir = filepath.Clean(mod.Dir)
   257  		r.modsByModPath = append(r.modsByModPath, mod)
   258  		r.modsByDir = append(r.modsByDir, mod)
   259  		if mod.Main {
   260  			r.mains = append(r.mains, mod)
   261  		}
   262  	}
   263  	return nil
   264  }
   265  
   266  // ClearForNewScan invalidates the last scan.
   267  //
   268  // It preserves the set of roots, but forgets about the set of directories.
   269  // Though it forgets the set of module cache directories, it remembers their
   270  // contents, since they are assumed to be immutable.
   271  func (r *ModuleResolver) ClearForNewScan() Resolver {
   272  	<-r.scanSema // acquire r, to guard scannedRoots
   273  	r2 := &ModuleResolver{
   274  		env:            r.env,
   275  		dummyVendorMod: r.dummyVendorMod,
   276  		moduleCacheDir: r.moduleCacheDir,
   277  		roots:          r.roots,
   278  		mains:          r.mains,
   279  		mainByDir:      r.mainByDir,
   280  		modsByModPath:  r.modsByModPath,
   281  
   282  		scanSema:         make(chan struct{}, 1),
   283  		scannedRoots:     make(map[gopathwalk.Root]bool),
   284  		otherCache:       NewDirInfoCache(),
   285  		moduleCacheCache: r.moduleCacheCache,
   286  	}
   287  	r2.scanSema <- struct{}{} // r2 must start released
   288  	// Invalidate root scans. We don't need to invalidate module cache roots,
   289  	// because they are immutable.
   290  	// (We don't support a use case where GOMODCACHE is cleaned in the middle of
   291  	// e.g. a gopls session: the user must restart gopls to get accurate
   292  	// imports.)
   293  	//
   294  	// Scanning for new directories in GOMODCACHE should be handled elsewhere,
   295  	// via a call to ScanModuleCache.
   296  	for _, root := range r.roots {
   297  		if root.Type == gopathwalk.RootModuleCache && r.scannedRoots[root] {
   298  			r2.scannedRoots[root] = true
   299  		}
   300  	}
   301  	r.scanSema <- struct{}{} // release r
   302  	return r2
   303  }
   304  
   305  // ClearModuleInfo invalidates resolver state that depends on go.mod file
   306  // contents (essentially, the output of go list -m -json ...).
   307  //
   308  // Notably, it does not forget directory contents, which are reset
   309  // asynchronously via ClearForNewScan.
   310  //
   311  // If the ProcessEnv is a GOPATH environment, ClearModuleInfo is a no op.
   312  //
   313  // TODO(rfindley): move this to a new env.go, consolidating ProcessEnv methods.
   314  func (e *ProcessEnv) ClearModuleInfo() {
   315  	if r, ok := e.resolver.(*ModuleResolver); ok {
   316  		resolver, resolverErr := newModuleResolver(e, e.ModCache)
   317  		if resolverErr == nil {
   318  			<-r.scanSema // acquire (guards caches)
   319  			resolver.moduleCacheCache = r.moduleCacheCache
   320  			resolver.otherCache = r.otherCache
   321  			r.scanSema <- struct{}{} // release
   322  		}
   323  		e.resolver = resolver
   324  		e.resolverErr = resolverErr
   325  	}
   326  }
   327  
   328  // UpdateResolver sets the resolver for the ProcessEnv to use in imports
   329  // operations. Only for use with the result of [Resolver.ClearForNewScan].
   330  //
   331  // TODO(rfindley): this awkward API is a result of the (arguably) inverted
   332  // relationship between configuration and state described in the doc comment
   333  // for [ProcessEnv].
   334  func (e *ProcessEnv) UpdateResolver(r Resolver) {
   335  	e.resolver = r
   336  	e.resolverErr = nil
   337  }
   338  
   339  // findPackage returns the module and directory from within the main modules
   340  // and their dependencies that contains the package at the given import path,
   341  // or returns nil, "" if no module is in scope.
   342  func (r *ModuleResolver) findPackage(importPath string) (*gocommand.ModuleJSON, string) {
   343  	// This can't find packages in the stdlib, but that's harmless for all
   344  	// the existing code paths.
   345  	for _, m := range r.modsByModPath {
   346  		if !strings.HasPrefix(importPath, m.Path) {
   347  			continue
   348  		}
   349  		pathInModule := importPath[len(m.Path):]
   350  		pkgDir := filepath.Join(m.Dir, pathInModule)
   351  		if r.dirIsNestedModule(pkgDir, m) {
   352  			continue
   353  		}
   354  
   355  		if info, ok := r.cacheLoad(pkgDir); ok {
   356  			if loaded, err := info.reachedStatus(nameLoaded); loaded {
   357  				if err != nil {
   358  					continue // No package in this dir.
   359  				}
   360  				return m, pkgDir
   361  			}
   362  			if scanned, err := info.reachedStatus(directoryScanned); scanned && err != nil {
   363  				continue // Dir is unreadable, etc.
   364  			}
   365  			// This is slightly wrong: a directory doesn't have to have an
   366  			// importable package to count as a package for package-to-module
   367  			// resolution. package main or _test files should count but
   368  			// don't.
   369  			// TODO(heschi): fix this.
   370  			if _, err := r.cachePackageName(info); err == nil {
   371  				return m, pkgDir
   372  			}
   373  		}
   374  
   375  		// Not cached. Read the filesystem.
   376  		pkgFiles, err := os.ReadDir(pkgDir)
   377  		if err != nil {
   378  			continue
   379  		}
   380  		// A module only contains a package if it has buildable go
   381  		// files in that directory. If not, it could be provided by an
   382  		// outer module. See #29736.
   383  		for _, fi := range pkgFiles {
   384  			if ok, _ := r.env.matchFile(pkgDir, fi.Name()); ok {
   385  				return m, pkgDir
   386  			}
   387  		}
   388  	}
   389  	return nil, ""
   390  }
   391  
   392  func (r *ModuleResolver) cacheLoad(dir string) (directoryPackageInfo, bool) {
   393  	if info, ok := r.moduleCacheCache.Load(dir); ok {
   394  		return info, ok
   395  	}
   396  	return r.otherCache.Load(dir)
   397  }
   398  
   399  func (r *ModuleResolver) cacheStore(info directoryPackageInfo) {
   400  	if info.rootType == gopathwalk.RootModuleCache {
   401  		r.moduleCacheCache.Store(info.dir, info)
   402  	} else {
   403  		r.otherCache.Store(info.dir, info)
   404  	}
   405  }
   406  
   407  // cachePackageName caches the package name for a dir already in the cache.
   408  func (r *ModuleResolver) cachePackageName(info directoryPackageInfo) (string, error) {
   409  	if info.rootType == gopathwalk.RootModuleCache {
   410  		return r.moduleCacheCache.CachePackageName(info)
   411  	}
   412  	return r.otherCache.CachePackageName(info)
   413  }
   414  
   415  func (r *ModuleResolver) cacheExports(ctx context.Context, env *ProcessEnv, info directoryPackageInfo) (string, []string, error) {
   416  	if info.rootType == gopathwalk.RootModuleCache {
   417  		return r.moduleCacheCache.CacheExports(ctx, env, info)
   418  	}
   419  	return r.otherCache.CacheExports(ctx, env, info)
   420  }
   421  
   422  // findModuleByDir returns the module that contains dir, or nil if no such
   423  // module is in scope.
   424  func (r *ModuleResolver) findModuleByDir(dir string) *gocommand.ModuleJSON {
   425  	// This is quite tricky and may not be correct. dir could be:
   426  	// - a package in the main module.
   427  	// - a replace target underneath the main module's directory.
   428  	//    - a nested module in the above.
   429  	// - a replace target somewhere totally random.
   430  	//    - a nested module in the above.
   431  	// - in the mod cache.
   432  	// - in /vendor/ in -mod=vendor mode.
   433  	//    - nested module? Dunno.
   434  	// Rumor has it that replace targets cannot contain other replace targets.
   435  	//
   436  	// Note that it is critical here that modsByDir is sorted to have deeper dirs
   437  	// first. This ensures that findModuleByDir finds the innermost module.
   438  	// See also golang/go#56291.
   439  	for _, m := range r.modsByDir {
   440  		if !strings.HasPrefix(dir, m.Dir) {
   441  			continue
   442  		}
   443  
   444  		if r.dirIsNestedModule(dir, m) {
   445  			continue
   446  		}
   447  
   448  		return m
   449  	}
   450  	return nil
   451  }
   452  
   453  // dirIsNestedModule reports if dir is contained in a nested module underneath
   454  // mod, not actually in mod.
   455  func (r *ModuleResolver) dirIsNestedModule(dir string, mod *gocommand.ModuleJSON) bool {
   456  	if !strings.HasPrefix(dir, mod.Dir) {
   457  		return false
   458  	}
   459  	if r.dirInModuleCache(dir) {
   460  		// Nested modules in the module cache are pruned,
   461  		// so it cannot be a nested module.
   462  		return false
   463  	}
   464  	if mod != nil && mod == r.dummyVendorMod {
   465  		// The /vendor pseudomodule is flattened and doesn't actually count.
   466  		return false
   467  	}
   468  	modDir, _ := r.modInfo(dir)
   469  	if modDir == "" {
   470  		return false
   471  	}
   472  	return modDir != mod.Dir
   473  }
   474  
   475  func readModName(modFile string) string {
   476  	modBytes, err := os.ReadFile(modFile)
   477  	if err != nil {
   478  		return ""
   479  	}
   480  	return modulePath(modBytes)
   481  }
   482  
   483  func (r *ModuleResolver) modInfo(dir string) (modDir, modName string) {
   484  	if r.dirInModuleCache(dir) {
   485  		if matches := modCacheRegexp.FindStringSubmatch(dir); len(matches) == 3 {
   486  			index := strings.Index(dir, matches[1]+"@"+matches[2])
   487  			modDir := filepath.Join(dir[:index], matches[1]+"@"+matches[2])
   488  			return modDir, readModName(filepath.Join(modDir, "go.mod"))
   489  		}
   490  	}
   491  	for {
   492  		if info, ok := r.cacheLoad(dir); ok {
   493  			return info.moduleDir, info.moduleName
   494  		}
   495  		f := filepath.Join(dir, "go.mod")
   496  		info, err := os.Stat(f)
   497  		if err == nil && !info.IsDir() {
   498  			return dir, readModName(f)
   499  		}
   500  
   501  		d := filepath.Dir(dir)
   502  		if len(d) >= len(dir) {
   503  			return "", "" // reached top of file system, no go.mod
   504  		}
   505  		dir = d
   506  	}
   507  }
   508  
   509  func (r *ModuleResolver) dirInModuleCache(dir string) bool {
   510  	if r.moduleCacheDir == "" {
   511  		return false
   512  	}
   513  	return strings.HasPrefix(dir, r.moduleCacheDir)
   514  }
   515  
   516  func (r *ModuleResolver) loadPackageNames(importPaths []string, srcDir string) (map[string]string, error) {
   517  	names := map[string]string{}
   518  	for _, path := range importPaths {
   519  		// TODO(rfindley): shouldn't this use the dirInfoCache?
   520  		_, packageDir := r.findPackage(path)
   521  		if packageDir == "" {
   522  			continue
   523  		}
   524  		name, err := packageDirToName(packageDir)
   525  		if err != nil {
   526  			continue
   527  		}
   528  		names[path] = name
   529  	}
   530  	return names, nil
   531  }
   532  
   533  func (r *ModuleResolver) scan(ctx context.Context, callback *scanCallback) error {
   534  	ctx, done := event.Start(ctx, "imports.ModuleResolver.scan")
   535  	defer done()
   536  
   537  	processDir := func(info directoryPackageInfo) {
   538  		// Skip this directory if we were not able to get the package information successfully.
   539  		if scanned, err := info.reachedStatus(directoryScanned); !scanned || err != nil {
   540  			return
   541  		}
   542  		pkg, err := r.canonicalize(info)
   543  		if err != nil {
   544  			return
   545  		}
   546  		if !callback.dirFound(pkg) {
   547  			return
   548  		}
   549  
   550  		pkg.packageName, err = r.cachePackageName(info)
   551  		if err != nil {
   552  			return
   553  		}
   554  		if !callback.packageNameLoaded(pkg) {
   555  			return
   556  		}
   557  
   558  		_, exports, err := r.loadExports(ctx, pkg, false)
   559  		if err != nil {
   560  			return
   561  		}
   562  		callback.exportsLoaded(pkg, exports)
   563  	}
   564  
   565  	// Start processing everything in the cache, and listen for the new stuff
   566  	// we discover in the walk below.
   567  	stop1 := r.moduleCacheCache.ScanAndListen(ctx, processDir)
   568  	defer stop1()
   569  	stop2 := r.otherCache.ScanAndListen(ctx, processDir)
   570  	defer stop2()
   571  
   572  	// We assume cached directories are fully cached, including all their
   573  	// children, and have not changed. We can skip them.
   574  	skip := func(root gopathwalk.Root, dir string) bool {
   575  		if r.env.SkipPathInScan != nil && root.Type == gopathwalk.RootCurrentModule {
   576  			if root.Path == dir {
   577  				return false
   578  			}
   579  
   580  			if r.env.SkipPathInScan(filepath.Clean(dir)) {
   581  				return true
   582  			}
   583  		}
   584  
   585  		info, ok := r.cacheLoad(dir)
   586  		if !ok {
   587  			return false
   588  		}
   589  		// This directory can be skipped as long as we have already scanned it.
   590  		// Packages with errors will continue to have errors, so there is no need
   591  		// to rescan them.
   592  		packageScanned, _ := info.reachedStatus(directoryScanned)
   593  		return packageScanned
   594  	}
   595  
   596  	add := func(root gopathwalk.Root, dir string) {
   597  		r.cacheStore(r.scanDirForPackage(root, dir))
   598  	}
   599  
   600  	// r.roots and the callback are not necessarily safe to use in the
   601  	// goroutine below. Process them eagerly.
   602  	roots := filterRoots(r.roots, callback.rootFound)
   603  	// We can't cancel walks, because we need them to finish to have a usable
   604  	// cache. Instead, run them in a separate goroutine and detach.
   605  	scanDone := make(chan struct{})
   606  	go func() {
   607  		select {
   608  		case <-ctx.Done():
   609  			return
   610  		case <-r.scanSema: // acquire
   611  		}
   612  		defer func() { r.scanSema <- struct{}{} }() // release
   613  		// We have the lock on r.scannedRoots, and no other scans can run.
   614  		for _, root := range roots {
   615  			if ctx.Err() != nil {
   616  				return
   617  			}
   618  
   619  			if r.scannedRoots[root] {
   620  				continue
   621  			}
   622  			gopathwalk.WalkSkip([]gopathwalk.Root{root}, add, skip, gopathwalk.Options{Logf: r.env.Logf, ModulesEnabled: true})
   623  			r.scannedRoots[root] = true
   624  		}
   625  		close(scanDone)
   626  	}()
   627  	select {
   628  	case <-ctx.Done():
   629  	case <-scanDone:
   630  	}
   631  	return nil
   632  }
   633  
   634  func (r *ModuleResolver) scoreImportPath(ctx context.Context, path string) float64 {
   635  	if _, ok := stdlib[path]; ok {
   636  		return MaxRelevance
   637  	}
   638  	mod, _ := r.findPackage(path)
   639  	return modRelevance(mod)
   640  }
   641  
   642  func modRelevance(mod *gocommand.ModuleJSON) float64 {
   643  	var relevance float64
   644  	switch {
   645  	case mod == nil: // out of scope
   646  		return MaxRelevance - 4
   647  	case mod.Indirect:
   648  		relevance = MaxRelevance - 3
   649  	case !mod.Main:
   650  		relevance = MaxRelevance - 2
   651  	default:
   652  		relevance = MaxRelevance - 1 // main module ties with stdlib
   653  	}
   654  
   655  	_, versionString, ok := module.SplitPathVersion(mod.Path)
   656  	if ok {
   657  		index := strings.Index(versionString, "v")
   658  		if index == -1 {
   659  			return relevance
   660  		}
   661  		if versionNumber, err := strconv.ParseFloat(versionString[index+1:], 64); err == nil {
   662  			relevance += versionNumber / 1000
   663  		}
   664  	}
   665  
   666  	return relevance
   667  }
   668  
   669  // canonicalize gets the result of canonicalizing the packages using the results
   670  // of initializing the resolver from 'go list -m'.
   671  func (r *ModuleResolver) canonicalize(info directoryPackageInfo) (*pkg, error) {
   672  	// Packages in GOROOT are already canonical, regardless of the std/cmd modules.
   673  	if info.rootType == gopathwalk.RootGOROOT {
   674  		return &pkg{
   675  			importPathShort: info.nonCanonicalImportPath,
   676  			dir:             info.dir,
   677  			packageName:     path.Base(info.nonCanonicalImportPath),
   678  			relevance:       MaxRelevance,
   679  		}, nil
   680  	}
   681  
   682  	importPath := info.nonCanonicalImportPath
   683  	mod := r.findModuleByDir(info.dir)
   684  	// Check if the directory is underneath a module that's in scope.
   685  	if mod != nil {
   686  		// It is. If dir is the target of a replace directive,
   687  		// our guessed import path is wrong. Use the real one.
   688  		if mod.Dir == info.dir {
   689  			importPath = mod.Path
   690  		} else {
   691  			dirInMod := info.dir[len(mod.Dir)+len("/"):]
   692  			importPath = path.Join(mod.Path, filepath.ToSlash(dirInMod))
   693  		}
   694  	} else if !strings.HasPrefix(importPath, info.moduleName) {
   695  		// The module's name doesn't match the package's import path. It
   696  		// probably needs a replace directive we don't have.
   697  		return nil, fmt.Errorf("package in %q is not valid without a replace statement", info.dir)
   698  	}
   699  
   700  	res := &pkg{
   701  		importPathShort: importPath,
   702  		dir:             info.dir,
   703  		relevance:       modRelevance(mod),
   704  	}
   705  	// We may have discovered a package that has a different version
   706  	// in scope already. Canonicalize to that one if possible.
   707  	if _, canonicalDir := r.findPackage(importPath); canonicalDir != "" {
   708  		res.dir = canonicalDir
   709  	}
   710  	return res, nil
   711  }
   712  
   713  func (r *ModuleResolver) loadExports(ctx context.Context, pkg *pkg, includeTest bool) (string, []string, error) {
   714  	if info, ok := r.cacheLoad(pkg.dir); ok && !includeTest {
   715  		return r.cacheExports(ctx, r.env, info)
   716  	}
   717  	return loadExportsFromFiles(ctx, r.env, pkg.dir, includeTest)
   718  }
   719  
   720  func (r *ModuleResolver) scanDirForPackage(root gopathwalk.Root, dir string) directoryPackageInfo {
   721  	subdir := ""
   722  	if dir != root.Path {
   723  		subdir = dir[len(root.Path)+len("/"):]
   724  	}
   725  	importPath := filepath.ToSlash(subdir)
   726  	if strings.HasPrefix(importPath, "vendor/") {
   727  		// Only enter vendor directories if they're explicitly requested as a root.
   728  		return directoryPackageInfo{
   729  			status: directoryScanned,
   730  			err:    fmt.Errorf("unwanted vendor directory"),
   731  		}
   732  	}
   733  	switch root.Type {
   734  	case gopathwalk.RootCurrentModule:
   735  		importPath = path.Join(r.mainByDir[root.Path].Path, filepath.ToSlash(subdir))
   736  	case gopathwalk.RootModuleCache:
   737  		matches := modCacheRegexp.FindStringSubmatch(subdir)
   738  		if len(matches) == 0 {
   739  			return directoryPackageInfo{
   740  				status: directoryScanned,
   741  				err:    fmt.Errorf("invalid module cache path: %v", subdir),
   742  			}
   743  		}
   744  		modPath, err := module.UnescapePath(filepath.ToSlash(matches[1]))
   745  		if err != nil {
   746  			if r.env.Logf != nil {
   747  				r.env.Logf("decoding module cache path %q: %v", subdir, err)
   748  			}
   749  			return directoryPackageInfo{
   750  				status: directoryScanned,
   751  				err:    fmt.Errorf("decoding module cache path %q: %v", subdir, err),
   752  			}
   753  		}
   754  		importPath = path.Join(modPath, filepath.ToSlash(matches[3]))
   755  	}
   756  
   757  	modDir, modName := r.modInfo(dir)
   758  	result := directoryPackageInfo{
   759  		status:                 directoryScanned,
   760  		dir:                    dir,
   761  		rootType:               root.Type,
   762  		nonCanonicalImportPath: importPath,
   763  		moduleDir:              modDir,
   764  		moduleName:             modName,
   765  	}
   766  	if root.Type == gopathwalk.RootGOROOT {
   767  		// stdlib packages are always in scope, despite the confusing go.mod
   768  		return result
   769  	}
   770  	return result
   771  }
   772  
   773  // modCacheRegexp splits a path in a module cache into module, module version, and package.
   774  var modCacheRegexp = regexp.MustCompile(`(.*)@([^/\\]*)(.*)`)
   775  
   776  var (
   777  	slashSlash = []byte("//")
   778  	moduleStr  = []byte("module")
   779  )
   780  
   781  // modulePath returns the module path from the gomod file text.
   782  // If it cannot find a module path, it returns an empty string.
   783  // It is tolerant of unrelated problems in the go.mod file.
   784  //
   785  // Copied from cmd/go/internal/modfile.
   786  func modulePath(mod []byte) string {
   787  	for len(mod) > 0 {
   788  		line := mod
   789  		mod = nil
   790  		if i := bytes.IndexByte(line, '\n'); i >= 0 {
   791  			line, mod = line[:i], line[i+1:]
   792  		}
   793  		if i := bytes.Index(line, slashSlash); i >= 0 {
   794  			line = line[:i]
   795  		}
   796  		line = bytes.TrimSpace(line)
   797  		if !bytes.HasPrefix(line, moduleStr) {
   798  			continue
   799  		}
   800  		line = line[len(moduleStr):]
   801  		n := len(line)
   802  		line = bytes.TrimSpace(line)
   803  		if len(line) == n || len(line) == 0 {
   804  			continue
   805  		}
   806  
   807  		if line[0] == '"' || line[0] == '`' {
   808  			p, err := strconv.Unquote(string(line))
   809  			if err != nil {
   810  				return "" // malformed quoted string or multiline module path
   811  			}
   812  			return p
   813  		}
   814  
   815  		return string(line)
   816  	}
   817  	return "" // missing module path
   818  }