GoPLS Viewer

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