walk.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright 2018 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. // Package gopathwalk is like filepath.Walk but specialized for finding Go
  5. // packages, particularly in $GOPATH and $GOROOT.
  6. package gopathwalk
  7. import (
  8. "bufio"
  9. "bytes"
  10. "fmt"
  11. "go/build"
  12. "io/ioutil"
  13. "log"
  14. "os"
  15. "path/filepath"
  16. "strings"
  17. "golang.org/x/tools/internal/fastwalk"
  18. )
  19. // Options controls the behavior of a Walk call.
  20. type Options struct {
  21. Debug bool // Enable debug logging
  22. ModulesEnabled bool // Search module caches. Also disables legacy goimports ignore rules.
  23. }
  24. // RootType indicates the type of a Root.
  25. type RootType int
  26. const (
  27. RootUnknown RootType = iota
  28. RootGOROOT
  29. RootGOPATH
  30. RootCurrentModule
  31. RootModuleCache
  32. RootOther
  33. )
  34. // A Root is a starting point for a Walk.
  35. type Root struct {
  36. Path string
  37. Type RootType
  38. }
  39. // SrcDirsRoots returns the roots from build.Default.SrcDirs(). Not modules-compatible.
  40. func SrcDirsRoots(ctx *build.Context) []Root {
  41. var roots []Root
  42. roots = append(roots, Root{filepath.Join(ctx.GOROOT, "src"), RootGOROOT})
  43. for _, p := range filepath.SplitList(ctx.GOPATH) {
  44. roots = append(roots, Root{filepath.Join(p, "src"), RootGOPATH})
  45. }
  46. return roots
  47. }
  48. // Walk walks Go source directories ($GOROOT, $GOPATH, etc) to find packages.
  49. // For each package found, add will be called (concurrently) with the absolute
  50. // paths of the containing source directory and the package directory.
  51. // add will be called concurrently.
  52. func Walk(roots []Root, add func(root Root, dir string), opts Options) {
  53. for _, root := range roots {
  54. walkDir(root, add, opts)
  55. }
  56. }
  57. func walkDir(root Root, add func(Root, string), opts Options) {
  58. if _, err := os.Stat(root.Path); os.IsNotExist(err) {
  59. if opts.Debug {
  60. log.Printf("skipping nonexistant directory: %v", root.Path)
  61. }
  62. return
  63. }
  64. if opts.Debug {
  65. log.Printf("scanning %s", root.Path)
  66. }
  67. w := &walker{
  68. root: root,
  69. add: add,
  70. opts: opts,
  71. }
  72. w.init()
  73. if err := fastwalk.Walk(root.Path, w.walk); err != nil {
  74. log.Printf("gopathwalk: scanning directory %v: %v", root.Path, err)
  75. }
  76. if opts.Debug {
  77. log.Printf("scanned %s", root.Path)
  78. }
  79. }
  80. // walker is the callback for fastwalk.Walk.
  81. type walker struct {
  82. root Root // The source directory to scan.
  83. add func(Root, string) // The callback that will be invoked for every possible Go package dir.
  84. opts Options // Options passed to Walk by the user.
  85. ignoredDirs []os.FileInfo // The ignored directories, loaded from .goimportsignore files.
  86. }
  87. // init initializes the walker based on its Options.
  88. func (w *walker) init() {
  89. var ignoredPaths []string
  90. if w.root.Type == RootModuleCache {
  91. ignoredPaths = []string{"cache"}
  92. }
  93. if !w.opts.ModulesEnabled && w.root.Type == RootGOPATH {
  94. ignoredPaths = w.getIgnoredDirs(w.root.Path)
  95. ignoredPaths = append(ignoredPaths, "v", "mod")
  96. }
  97. for _, p := range ignoredPaths {
  98. full := filepath.Join(w.root.Path, p)
  99. if fi, err := os.Stat(full); err == nil {
  100. w.ignoredDirs = append(w.ignoredDirs, fi)
  101. if w.opts.Debug {
  102. log.Printf("Directory added to ignore list: %s", full)
  103. }
  104. } else if w.opts.Debug {
  105. log.Printf("Error statting ignored directory: %v", err)
  106. }
  107. }
  108. }
  109. // getIgnoredDirs reads an optional config file at <path>/.goimportsignore
  110. // of relative directories to ignore when scanning for go files.
  111. // The provided path is one of the $GOPATH entries with "src" appended.
  112. func (w *walker) getIgnoredDirs(path string) []string {
  113. file := filepath.Join(path, ".goimportsignore")
  114. slurp, err := ioutil.ReadFile(file)
  115. if w.opts.Debug {
  116. if err != nil {
  117. log.Print(err)
  118. } else {
  119. log.Printf("Read %s", file)
  120. }
  121. }
  122. if err != nil {
  123. return nil
  124. }
  125. var ignoredDirs []string
  126. bs := bufio.NewScanner(bytes.NewReader(slurp))
  127. for bs.Scan() {
  128. line := strings.TrimSpace(bs.Text())
  129. if line == "" || strings.HasPrefix(line, "#") {
  130. continue
  131. }
  132. ignoredDirs = append(ignoredDirs, line)
  133. }
  134. return ignoredDirs
  135. }
  136. func (w *walker) shouldSkipDir(fi os.FileInfo) bool {
  137. for _, ignoredDir := range w.ignoredDirs {
  138. if os.SameFile(fi, ignoredDir) {
  139. return true
  140. }
  141. }
  142. return false
  143. }
  144. func (w *walker) walk(path string, typ os.FileMode) error {
  145. dir := filepath.Dir(path)
  146. if typ.IsRegular() {
  147. if dir == w.root.Path && (w.root.Type == RootGOROOT || w.root.Type == RootGOPATH) {
  148. // Doesn't make sense to have regular files
  149. // directly in your $GOPATH/src or $GOROOT/src.
  150. return fastwalk.SkipFiles
  151. }
  152. if !strings.HasSuffix(path, ".go") {
  153. return nil
  154. }
  155. w.add(w.root, dir)
  156. return fastwalk.SkipFiles
  157. }
  158. if typ == os.ModeDir {
  159. base := filepath.Base(path)
  160. if base == "" || base[0] == '.' || base[0] == '_' ||
  161. base == "testdata" ||
  162. (w.root.Type == RootGOROOT && w.opts.ModulesEnabled && base == "vendor") ||
  163. (!w.opts.ModulesEnabled && base == "node_modules") {
  164. return filepath.SkipDir
  165. }
  166. fi, err := os.Lstat(path)
  167. if err == nil && w.shouldSkipDir(fi) {
  168. return filepath.SkipDir
  169. }
  170. return nil
  171. }
  172. if typ == os.ModeSymlink {
  173. base := filepath.Base(path)
  174. if strings.HasPrefix(base, ".#") {
  175. // Emacs noise.
  176. return nil
  177. }
  178. fi, err := os.Lstat(path)
  179. if err != nil {
  180. // Just ignore it.
  181. return nil
  182. }
  183. if w.shouldTraverse(dir, fi) {
  184. return fastwalk.TraverseLink
  185. }
  186. }
  187. return nil
  188. }
  189. // shouldTraverse reports whether the symlink fi, found in dir,
  190. // should be followed. It makes sure symlinks were never visited
  191. // before to avoid symlink loops.
  192. func (w *walker) shouldTraverse(dir string, fi os.FileInfo) bool {
  193. path := filepath.Join(dir, fi.Name())
  194. target, err := filepath.EvalSymlinks(path)
  195. if err != nil {
  196. return false
  197. }
  198. ts, err := os.Stat(target)
  199. if err != nil {
  200. fmt.Fprintln(os.Stderr, err)
  201. return false
  202. }
  203. if !ts.IsDir() {
  204. return false
  205. }
  206. if w.shouldSkipDir(ts) {
  207. return false
  208. }
  209. // Check for symlink loops by statting each directory component
  210. // and seeing if any are the same file as ts.
  211. for {
  212. parent := filepath.Dir(path)
  213. if parent == path {
  214. // Made it to the root without seeing a cycle.
  215. // Use this symlink.
  216. return true
  217. }
  218. parentInfo, err := os.Stat(parent)
  219. if err != nil {
  220. return false
  221. }
  222. if os.SameFile(ts, parentInfo) {
  223. // Cycle. Don't traverse.
  224. return false
  225. }
  226. path = parent
  227. }
  228. }