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.
5 // Package gopathwalk is like filepath.Walk but specialized for finding Go
6 // packages, particularly in $GOPATH and $GOROOT.
20 "golang.org/x/tools/internal/fastwalk"
23 // Options controls the behavior of a Walk call.
25 Debug bool // Enable debug logging
26 ModulesEnabled bool // Search module caches. Also disables legacy goimports ignore rules.
29 // RootType indicates the type of a Root.
33 RootUnknown RootType = iota
41 // A Root is a starting point for a Walk.
47 // SrcDirsRoots returns the roots from build.Default.SrcDirs(). Not modules-compatible.
48 func SrcDirsRoots(ctx *build.Context) []Root {
50 roots = append(roots, Root{filepath.Join(ctx.GOROOT, "src"), RootGOROOT})
51 for _, p := range filepath.SplitList(ctx.GOPATH) {
52 roots = append(roots, Root{filepath.Join(p, "src"), RootGOPATH})
57 // Walk walks Go source directories ($GOROOT, $GOPATH, etc) to find packages.
58 // For each package found, add will be called (concurrently) with the absolute
59 // paths of the containing source directory and the package directory.
60 // add will be called concurrently.
61 func Walk(roots []Root, add func(root Root, dir string), opts Options) {
62 for _, root := range roots {
63 walkDir(root, add, opts)
67 func walkDir(root Root, add func(Root, string), opts Options) {
68 if _, err := os.Stat(root.Path); os.IsNotExist(err) {
70 log.Printf("skipping nonexistant directory: %v", root.Path)
75 log.Printf("scanning %s", root.Path)
83 if err := fastwalk.Walk(root.Path, w.walk); err != nil {
84 log.Printf("gopathwalk: scanning directory %v: %v", root.Path, err)
88 log.Printf("scanned %s", root.Path)
92 // walker is the callback for fastwalk.Walk.
94 root Root // The source directory to scan.
95 add func(Root, string) // The callback that will be invoked for every possible Go package dir.
96 opts Options // Options passed to Walk by the user.
98 ignoredDirs []os.FileInfo // The ignored directories, loaded from .goimportsignore files.
101 // init initializes the walker based on its Options.
102 func (w *walker) init() {
103 var ignoredPaths []string
104 if w.root.Type == RootModuleCache {
105 ignoredPaths = []string{"cache"}
107 if !w.opts.ModulesEnabled && w.root.Type == RootGOPATH {
108 ignoredPaths = w.getIgnoredDirs(w.root.Path)
109 ignoredPaths = append(ignoredPaths, "v", "mod")
112 for _, p := range ignoredPaths {
113 full := filepath.Join(w.root.Path, p)
114 if fi, err := os.Stat(full); err == nil {
115 w.ignoredDirs = append(w.ignoredDirs, fi)
117 log.Printf("Directory added to ignore list: %s", full)
119 } else if w.opts.Debug {
120 log.Printf("Error statting ignored directory: %v", err)
125 // getIgnoredDirs reads an optional config file at <path>/.goimportsignore
126 // of relative directories to ignore when scanning for go files.
127 // The provided path is one of the $GOPATH entries with "src" appended.
128 func (w *walker) getIgnoredDirs(path string) []string {
129 file := filepath.Join(path, ".goimportsignore")
130 slurp, err := ioutil.ReadFile(file)
135 log.Printf("Read %s", file)
142 var ignoredDirs []string
143 bs := bufio.NewScanner(bytes.NewReader(slurp))
145 line := strings.TrimSpace(bs.Text())
146 if line == "" || strings.HasPrefix(line, "#") {
149 ignoredDirs = append(ignoredDirs, line)
154 func (w *walker) shouldSkipDir(fi os.FileInfo) bool {
155 for _, ignoredDir := range w.ignoredDirs {
156 if os.SameFile(fi, ignoredDir) {
163 func (w *walker) walk(path string, typ os.FileMode) error {
164 dir := filepath.Dir(path)
166 if dir == w.root.Path && (w.root.Type == RootGOROOT || w.root.Type == RootGOPATH) {
167 // Doesn't make sense to have regular files
168 // directly in your $GOPATH/src or $GOROOT/src.
169 return fastwalk.SkipFiles
171 if !strings.HasSuffix(path, ".go") {
176 return fastwalk.SkipFiles
178 if typ == os.ModeDir {
179 base := filepath.Base(path)
180 if base == "" || base[0] == '.' || base[0] == '_' ||
181 base == "testdata" ||
182 (w.root.Type == RootGOROOT && w.opts.ModulesEnabled && base == "vendor") ||
183 (!w.opts.ModulesEnabled && base == "node_modules") {
184 return filepath.SkipDir
186 fi, err := os.Lstat(path)
187 if err == nil && w.shouldSkipDir(fi) {
188 return filepath.SkipDir
192 if typ == os.ModeSymlink {
193 base := filepath.Base(path)
194 if strings.HasPrefix(base, ".#") {
198 fi, err := os.Lstat(path)
203 if w.shouldTraverse(dir, fi) {
204 return fastwalk.TraverseLink
210 // shouldTraverse reports whether the symlink fi, found in dir,
211 // should be followed. It makes sure symlinks were never visited
212 // before to avoid symlink loops.
213 func (w *walker) shouldTraverse(dir string, fi os.FileInfo) bool {
214 path := filepath.Join(dir, fi.Name())
215 target, err := filepath.EvalSymlinks(path)
219 ts, err := os.Stat(target)
221 fmt.Fprintln(os.Stderr, err)
227 if w.shouldSkipDir(ts) {
230 // Check for symlink loops by statting each directory component
231 // and seeing if any are the same file as ts.
233 parent := filepath.Dir(path)
235 // Made it to the root without seeing a cycle.
239 parentInfo, err := os.Stat(parent)
243 if os.SameFile(ts, parentInfo) {
244 // Cycle. Don't traverse.