You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

663 lines
18 KiB

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