walk.go 7.2 KB

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