17 "golang.org/x/mod/module"
18 "mvdan.cc/gofumpt/gofumports/internal/gocommand"
19 "mvdan.cc/gofumpt/gofumports/internal/gopathwalk"
22 // ModuleResolver implements resolver for modules using the go command as little
24 type ModuleResolver struct {
27 dummyVendorMod *gocommand.ModuleJSON // If vendoring is enabled, the pseudo-module that represents the /vendor directory.
28 roots []gopathwalk.Root
29 scanSema chan struct{} // scanSema prevents concurrent scans and guards scannedRoots.
30 scannedRoots map[gopathwalk.Root]bool
33 main *gocommand.ModuleJSON
34 modsByModPath []*gocommand.ModuleJSON // All modules, ordered by # of path components in module Path...
35 modsByDir []*gocommand.ModuleJSON // ...or Dir.
37 // moduleCacheCache stores information about the module cache.
38 moduleCacheCache *dirInfoCache
39 otherCache *dirInfoCache
42 func newModuleResolver(e *ProcessEnv) *ModuleResolver {
45 scanSema: make(chan struct{}, 1),
47 r.scanSema <- struct{}{}
51 func (r *ModuleResolver) init() error {
56 goenv, err := r.env.goEnv()
60 inv := gocommand.Invocation{
61 BuildFlags: r.env.BuildFlags,
62 ModFlag: r.env.ModFlag,
63 ModFile: r.env.ModFile,
66 WorkingDir: r.env.WorkingDir,
68 mainMod, vendorEnabled, err := gocommand.VendorEnabled(context.TODO(), inv, r.env.GocmdRunner)
73 if mainMod != nil && vendorEnabled {
74 // Vendor mode is on, so all the non-Main modules are irrelevant,
75 // and we need to search /vendor for everything.
77 r.dummyVendorMod = &gocommand.ModuleJSON{
79 Dir: filepath.Join(mainMod.Dir, "vendor"),
81 r.modsByModPath = []*gocommand.ModuleJSON{mainMod, r.dummyVendorMod}
82 r.modsByDir = []*gocommand.ModuleJSON{mainMod, r.dummyVendorMod}
84 // Vendor mode is off, so run go list -m ... to find everything.
88 if gmc := r.env.Env["GOMODCACHE"]; gmc != "" {
89 r.moduleCacheDir = gmc
91 gopaths := filepath.SplitList(goenv["GOPATH"])
92 if len(gopaths) == 0 {
93 return fmt.Errorf("empty GOPATH")
95 r.moduleCacheDir = filepath.Join(gopaths[0], "/pkg/mod")
98 sort.Slice(r.modsByModPath, func(i, j int) bool {
99 count := func(x int) int {
100 return strings.Count(r.modsByModPath[x].Path, "/")
102 return count(j) < count(i) // descending order
104 sort.Slice(r.modsByDir, func(i, j int) bool {
105 count := func(x int) int {
106 return strings.Count(r.modsByDir[x].Dir, "/")
108 return count(j) < count(i) // descending order
111 r.roots = []gopathwalk.Root{
112 {filepath.Join(goenv["GOROOT"], "/src"), gopathwalk.RootGOROOT},
115 r.roots = append(r.roots, gopathwalk.Root{r.main.Dir, gopathwalk.RootCurrentModule})
118 r.roots = append(r.roots, gopathwalk.Root{r.dummyVendorMod.Dir, gopathwalk.RootOther})
120 addDep := func(mod *gocommand.ModuleJSON) {
121 if mod.Replace == nil {
122 // This is redundant with the cache, but we'll skip it cheaply enough.
123 r.roots = append(r.roots, gopathwalk.Root{mod.Dir, gopathwalk.RootModuleCache})
125 r.roots = append(r.roots, gopathwalk.Root{mod.Dir, gopathwalk.RootOther})
128 // Walk dependent modules before scanning the full mod cache, direct deps first.
129 for _, mod := range r.modsByModPath {
130 if !mod.Indirect && !mod.Main {
134 for _, mod := range r.modsByModPath {
135 if mod.Indirect && !mod.Main {
139 r.roots = append(r.roots, gopathwalk.Root{r.moduleCacheDir, gopathwalk.RootModuleCache})
142 r.scannedRoots = map[gopathwalk.Root]bool{}
143 if r.moduleCacheCache == nil {
144 r.moduleCacheCache = &dirInfoCache{
145 dirs: map[string]*directoryPackageInfo{},
146 listeners: map[*int]cacheListener{},
149 if r.otherCache == nil {
150 r.otherCache = &dirInfoCache{
151 dirs: map[string]*directoryPackageInfo{},
152 listeners: map[*int]cacheListener{},
159 func (r *ModuleResolver) initAllMods() error {
160 stdout, err := r.env.invokeGo(context.TODO(), "list", "-m", "-json", "...")
164 for dec := json.NewDecoder(stdout); dec.More(); {
165 mod := &gocommand.ModuleJSON{}
166 if err := dec.Decode(mod); err != nil {
170 if r.env.Logf != nil {
171 r.env.Logf("module %v has not been downloaded and will be ignored", mod.Path)
173 // Can't do anything with a module that's not downloaded.
176 // golang/go#36193: the go command doesn't always clean paths.
177 mod.Dir = filepath.Clean(mod.Dir)
178 r.modsByModPath = append(r.modsByModPath, mod)
179 r.modsByDir = append(r.modsByDir, mod)
187 func (r *ModuleResolver) ClearForNewScan() {
189 r.scannedRoots = map[gopathwalk.Root]bool{}
190 r.otherCache = &dirInfoCache{
191 dirs: map[string]*directoryPackageInfo{},
192 listeners: map[*int]cacheListener{},
194 r.scanSema <- struct{}{}
197 func (r *ModuleResolver) ClearForNewMod() {
201 moduleCacheCache: r.moduleCacheCache,
202 otherCache: r.otherCache,
203 scanSema: r.scanSema,
206 r.scanSema <- struct{}{}
209 // findPackage returns the module and directory that contains the package at
210 // the given import path, or returns nil, "" if no module is in scope.
211 func (r *ModuleResolver) findPackage(importPath string) (*gocommand.ModuleJSON, string) {
212 // This can't find packages in the stdlib, but that's harmless for all
213 // the existing code paths.
214 for _, m := range r.modsByModPath {
215 if !strings.HasPrefix(importPath, m.Path) {
218 pathInModule := importPath[len(m.Path):]
219 pkgDir := filepath.Join(m.Dir, pathInModule)
220 if r.dirIsNestedModule(pkgDir, m) {
224 if info, ok := r.cacheLoad(pkgDir); ok {
225 if loaded, err := info.reachedStatus(nameLoaded); loaded {
227 continue // No package in this dir.
231 if scanned, err := info.reachedStatus(directoryScanned); scanned && err != nil {
232 continue // Dir is unreadable, etc.
234 // This is slightly wrong: a directory doesn't have to have an
235 // importable package to count as a package for package-to-module
236 // resolution. package main or _test files should count but
238 // TODO(heschi): fix this.
239 if _, err := r.cachePackageName(info); err == nil {
244 // Not cached. Read the filesystem.
245 pkgFiles, err := ioutil.ReadDir(pkgDir)
249 // A module only contains a package if it has buildable go
250 // files in that directory. If not, it could be provided by an
251 // outer module. See #29736.
252 for _, fi := range pkgFiles {
253 if ok, _ := r.env.matchFile(pkgDir, fi.Name()); ok {
261 func (r *ModuleResolver) cacheLoad(dir string) (directoryPackageInfo, bool) {
262 if info, ok := r.moduleCacheCache.Load(dir); ok {
265 return r.otherCache.Load(dir)
268 func (r *ModuleResolver) cacheStore(info directoryPackageInfo) {
269 if info.rootType == gopathwalk.RootModuleCache {
270 r.moduleCacheCache.Store(info.dir, info)
272 r.otherCache.Store(info.dir, info)
276 func (r *ModuleResolver) cacheKeys() []string {
277 return append(r.moduleCacheCache.Keys(), r.otherCache.Keys()...)
280 // cachePackageName caches the package name for a dir already in the cache.
281 func (r *ModuleResolver) cachePackageName(info directoryPackageInfo) (string, error) {
282 if info.rootType == gopathwalk.RootModuleCache {
283 return r.moduleCacheCache.CachePackageName(info)
285 return r.otherCache.CachePackageName(info)
288 func (r *ModuleResolver) cacheExports(ctx context.Context, env *ProcessEnv, info directoryPackageInfo) (string, []string, error) {
289 if info.rootType == gopathwalk.RootModuleCache {
290 return r.moduleCacheCache.CacheExports(ctx, env, info)
292 return r.otherCache.CacheExports(ctx, env, info)
295 // findModuleByDir returns the module that contains dir, or nil if no such
296 // module is in scope.
297 func (r *ModuleResolver) findModuleByDir(dir string) *gocommand.ModuleJSON {
298 // This is quite tricky and may not be correct. dir could be:
299 // - a package in the main module.
300 // - a replace target underneath the main module's directory.
301 // - a nested module in the above.
302 // - a replace target somewhere totally random.
303 // - a nested module in the above.
304 // - in the mod cache.
305 // - in /vendor/ in -mod=vendor mode.
306 // - nested module? Dunno.
307 // Rumor has it that replace targets cannot contain other replace targets.
308 for _, m := range r.modsByDir {
309 if !strings.HasPrefix(dir, m.Dir) {
313 if r.dirIsNestedModule(dir, m) {
322 // dirIsNestedModule reports if dir is contained in a nested module underneath
323 // mod, not actually in mod.
324 func (r *ModuleResolver) dirIsNestedModule(dir string, mod *gocommand.ModuleJSON) bool {
325 if !strings.HasPrefix(dir, mod.Dir) {
328 if r.dirInModuleCache(dir) {
329 // Nested modules in the module cache are pruned,
330 // so it cannot be a nested module.
333 if mod != nil && mod == r.dummyVendorMod {
334 // The /vendor pseudomodule is flattened and doesn't actually count.
337 modDir, _ := r.modInfo(dir)
341 return modDir != mod.Dir
344 func (r *ModuleResolver) modInfo(dir string) (modDir string, modName string) {
345 readModName := func(modFile string) string {
346 modBytes, err := ioutil.ReadFile(modFile)
350 return modulePath(modBytes)
353 if r.dirInModuleCache(dir) {
354 if matches := modCacheRegexp.FindStringSubmatch(dir); len(matches) == 3 {
355 index := strings.Index(dir, matches[1]+"@"+matches[2])
356 modDir := filepath.Join(dir[:index], matches[1]+"@"+matches[2])
357 return modDir, readModName(filepath.Join(modDir, "go.mod"))
361 if info, ok := r.cacheLoad(dir); ok {
362 return info.moduleDir, info.moduleName
364 f := filepath.Join(dir, "go.mod")
365 info, err := os.Stat(f)
366 if err == nil && !info.IsDir() {
367 return dir, readModName(f)
370 d := filepath.Dir(dir)
371 if len(d) >= len(dir) {
372 return "", "" // reached top of file system, no go.mod
378 func (r *ModuleResolver) dirInModuleCache(dir string) bool {
379 if r.moduleCacheDir == "" {
382 return strings.HasPrefix(dir, r.moduleCacheDir)
385 func (r *ModuleResolver) loadPackageNames(importPaths []string, srcDir string) (map[string]string, error) {
386 if err := r.init(); err != nil {
389 names := map[string]string{}
390 for _, path := range importPaths {
391 _, packageDir := r.findPackage(path)
392 if packageDir == "" {
395 name, err := packageDirToName(packageDir)
404 func (r *ModuleResolver) scan(ctx context.Context, callback *scanCallback) error {
405 if err := r.init(); err != nil {
409 processDir := func(info directoryPackageInfo) {
410 // Skip this directory if we were not able to get the package information successfully.
411 if scanned, err := info.reachedStatus(directoryScanned); !scanned || err != nil {
414 pkg, err := r.canonicalize(info)
419 if !callback.dirFound(pkg) {
422 pkg.packageName, err = r.cachePackageName(info)
427 if !callback.packageNameLoaded(pkg) {
430 _, exports, err := r.loadExports(ctx, pkg, false)
434 callback.exportsLoaded(pkg, exports)
437 // Start processing everything in the cache, and listen for the new stuff
438 // we discover in the walk below.
439 stop1 := r.moduleCacheCache.ScanAndListen(ctx, processDir)
441 stop2 := r.otherCache.ScanAndListen(ctx, processDir)
444 // We assume cached directories are fully cached, including all their
445 // children, and have not changed. We can skip them.
446 skip := func(root gopathwalk.Root, dir string) bool {
447 info, ok := r.cacheLoad(dir)
451 // This directory can be skipped as long as we have already scanned it.
452 // Packages with errors will continue to have errors, so there is no need
454 packageScanned, _ := info.reachedStatus(directoryScanned)
455 return packageScanned
458 // Add anything new to the cache, and process it if we're still listening.
459 add := func(root gopathwalk.Root, dir string) {
460 r.cacheStore(r.scanDirForPackage(root, dir))
463 // r.roots and the callback are not necessarily safe to use in the
464 // goroutine below. Process them eagerly.
465 roots := filterRoots(r.roots, callback.rootFound)
466 // We can't cancel walks, because we need them to finish to have a usable
467 // cache. Instead, run them in a separate goroutine and detach.
468 scanDone := make(chan struct{})
475 defer func() { r.scanSema <- struct{}{} }()
476 // We have the lock on r.scannedRoots, and no other scans can run.
477 for _, root := range roots {
478 if ctx.Err() != nil {
482 if r.scannedRoots[root] {
485 gopathwalk.WalkSkip([]gopathwalk.Root{root}, add, skip, gopathwalk.Options{Logf: r.env.Logf, ModulesEnabled: true})
486 r.scannedRoots[root] = true
497 func (r *ModuleResolver) scoreImportPath(ctx context.Context, path string) float64 {
498 if _, ok := stdlib[path]; ok {
501 mod, _ := r.findPackage(path)
502 return modRelevance(mod)
505 func modRelevance(mod *gocommand.ModuleJSON) float64 {
506 var relevance float64
508 case mod == nil: // out of scope
509 return MaxRelevance - 4
511 relevance = MaxRelevance - 3
513 relevance = MaxRelevance - 2
515 relevance = MaxRelevance - 1 // main module ties with stdlib
518 _, versionString, ok := module.SplitPathVersion(mod.Path)
520 index := strings.Index(versionString, "v")
524 if versionNumber, err := strconv.ParseFloat(versionString[index+1:], 64); err == nil {
525 relevance += versionNumber / 1000
532 // canonicalize gets the result of canonicalizing the packages using the results
533 // of initializing the resolver from 'go list -m'.
534 func (r *ModuleResolver) canonicalize(info directoryPackageInfo) (*pkg, error) {
535 // Packages in GOROOT are already canonical, regardless of the std/cmd modules.
536 if info.rootType == gopathwalk.RootGOROOT {
538 importPathShort: info.nonCanonicalImportPath,
540 packageName: path.Base(info.nonCanonicalImportPath),
541 relevance: MaxRelevance,
545 importPath := info.nonCanonicalImportPath
546 mod := r.findModuleByDir(info.dir)
547 // Check if the directory is underneath a module that's in scope.
549 // It is. If dir is the target of a replace directive,
550 // our guessed import path is wrong. Use the real one.
551 if mod.Dir == info.dir {
552 importPath = mod.Path
554 dirInMod := info.dir[len(mod.Dir)+len("/"):]
555 importPath = path.Join(mod.Path, filepath.ToSlash(dirInMod))
557 } else if !strings.HasPrefix(importPath, info.moduleName) {
558 // The module's name doesn't match the package's import path. It
559 // probably needs a replace directive we don't have.
560 return nil, fmt.Errorf("package in %q is not valid without a replace statement", info.dir)
564 importPathShort: importPath,
566 relevance: modRelevance(mod),
568 // We may have discovered a package that has a different version
569 // in scope already. Canonicalize to that one if possible.
570 if _, canonicalDir := r.findPackage(importPath); canonicalDir != "" {
571 res.dir = canonicalDir
576 func (r *ModuleResolver) loadExports(ctx context.Context, pkg *pkg, includeTest bool) (string, []string, error) {
577 if err := r.init(); err != nil {
580 if info, ok := r.cacheLoad(pkg.dir); ok && !includeTest {
581 return r.cacheExports(ctx, r.env, info)
583 return loadExportsFromFiles(ctx, r.env, pkg.dir, includeTest)
586 func (r *ModuleResolver) scanDirForPackage(root gopathwalk.Root, dir string) directoryPackageInfo {
588 if dir != root.Path {
589 subdir = dir[len(root.Path)+len("/"):]
591 importPath := filepath.ToSlash(subdir)
592 if strings.HasPrefix(importPath, "vendor/") {
593 // Only enter vendor directories if they're explicitly requested as a root.
594 return directoryPackageInfo{
595 status: directoryScanned,
596 err: fmt.Errorf("unwanted vendor directory"),
600 case gopathwalk.RootCurrentModule:
601 importPath = path.Join(r.main.Path, filepath.ToSlash(subdir))
602 case gopathwalk.RootModuleCache:
603 matches := modCacheRegexp.FindStringSubmatch(subdir)
604 if len(matches) == 0 {
605 return directoryPackageInfo{
606 status: directoryScanned,
607 err: fmt.Errorf("invalid module cache path: %v", subdir),
610 modPath, err := module.UnescapePath(filepath.ToSlash(matches[1]))
612 if r.env.Logf != nil {
613 r.env.Logf("decoding module cache path %q: %v", subdir, err)
615 return directoryPackageInfo{
616 status: directoryScanned,
617 err: fmt.Errorf("decoding module cache path %q: %v", subdir, err),
620 importPath = path.Join(modPath, filepath.ToSlash(matches[3]))
623 modDir, modName := r.modInfo(dir)
624 result := directoryPackageInfo{
625 status: directoryScanned,
628 nonCanonicalImportPath: importPath,
632 if root.Type == gopathwalk.RootGOROOT {
633 // stdlib packages are always in scope, despite the confusing go.mod
639 // modCacheRegexp splits a path in a module cache into module, module version, and package.
640 var modCacheRegexp = regexp.MustCompile(`(.*)@([^/\\]*)(.*)`)
643 slashSlash = []byte("//")
644 moduleStr = []byte("module")
647 // modulePath returns the module path from the gomod file text.
648 // If it cannot find a module path, it returns an empty string.
649 // It is tolerant of unrelated problems in the go.mod file.
651 // Copied from cmd/go/internal/modfile.
652 func modulePath(mod []byte) string {
656 if i := bytes.IndexByte(line, '\n'); i >= 0 {
657 line, mod = line[:i], line[i+1:]
659 if i := bytes.Index(line, slashSlash); i >= 0 {
662 line = bytes.TrimSpace(line)
663 if !bytes.HasPrefix(line, moduleStr) {
666 line = line[len(moduleStr):]
668 line = bytes.TrimSpace(line)
669 if len(line) == n || len(line) == 0 {
673 if line[0] == '"' || line[0] == '`' {
674 p, err := strconv.Unquote(string(line))
676 return "" // malformed quoted string or multiline module path
683 return "" // missing module path