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	"go/build"
 14	"io/ioutil"
 15	"log"
 16	"os"
 17	"path/filepath"
 18	"strings"
 19
 20	"golang.org/x/tools/internal/fastwalk"
 21)
 22
 23// Options controls the behavior of a Walk call.
 24type Options struct {
 25	Debug          bool // Enable debug logging
 26	ModulesEnabled bool // Search module caches. Also disables legacy goimports ignore rules.
 27}
 28
 29// RootType indicates the type of a Root.
 30type RootType int
 31
 32const (
 33	RootUnknown RootType = iota
 34	RootGOROOT
 35	RootGOPATH
 36	RootCurrentModule
 37	RootModuleCache
 38	RootOther
 39)
 40
 41// A Root is a starting point for a Walk.
 42type Root struct {
 43	Path string
 44	Type RootType
 45}
 46
 47// SrcDirsRoots returns the roots from build.Default.SrcDirs(). Not modules-compatible.
 48func SrcDirsRoots(ctx *build.Context) []Root {
 49	var roots []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})
 53	}
 54	return roots
 55}
 56
 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.
 61func Walk(roots []Root, add func(root Root, dir string), opts Options) {
 62	for _, root := range roots {
 63		walkDir(root, add, opts)
 64	}
 65}
 66
 67func walkDir(root Root, add func(Root, string), opts Options) {
 68	if _, err := os.Stat(root.Path); os.IsNotExist(err) {
 69		if opts.Debug {
 70			log.Printf("skipping nonexistant directory: %v", root.Path)
 71		}
 72		return
 73	}
 74	if opts.Debug {
 75		log.Printf("scanning %s", root.Path)
 76	}
 77	w := &walker{
 78		root: root,
 79		add:  add,
 80		opts: opts,
 81	}
 82	w.init()
 83	if err := fastwalk.Walk(root.Path, w.walk); err != nil {
 84		log.Printf("gopathwalk: scanning directory %v: %v", root.Path, err)
 85	}
 86
 87	if opts.Debug {
 88		log.Printf("scanned %s", root.Path)
 89	}
 90}
 91
 92// walker is the callback for fastwalk.Walk.
 93type walker struct {
 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.
 97
 98	ignoredDirs []os.FileInfo // The ignored directories, loaded from .goimportsignore files.
 99}
100
101// init initializes the walker based on its Options.
102func (w *walker) init() {
103	var ignoredPaths []string
104	if w.root.Type == RootModuleCache {
105		ignoredPaths = []string{"cache"}
106	}
107	if !w.opts.ModulesEnabled && w.root.Type == RootGOPATH {
108		ignoredPaths = w.getIgnoredDirs(w.root.Path)
109		ignoredPaths = append(ignoredPaths, "v", "mod")
110	}
111
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)
116			if w.opts.Debug {
117				log.Printf("Directory added to ignore list: %s", full)
118			}
119		} else if w.opts.Debug {
120			log.Printf("Error statting ignored directory: %v", err)
121		}
122	}
123}
124
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.
128func (w *walker) getIgnoredDirs(path string) []string {
129	file := filepath.Join(path, ".goimportsignore")
130	slurp, err := ioutil.ReadFile(file)
131	if w.opts.Debug {
132		if err != nil {
133			log.Print(err)
134		} else {
135			log.Printf("Read %s", file)
136		}
137	}
138	if err != nil {
139		return nil
140	}
141
142	var ignoredDirs []string
143	bs := bufio.NewScanner(bytes.NewReader(slurp))
144	for bs.Scan() {
145		line := strings.TrimSpace(bs.Text())
146		if line == "" || strings.HasPrefix(line, "#") {
147			continue
148		}
149		ignoredDirs = append(ignoredDirs, line)
150	}
151	return ignoredDirs
152}
153
154func (w *walker) shouldSkipDir(fi os.FileInfo) bool {
155	for _, ignoredDir := range w.ignoredDirs {
156		if os.SameFile(fi, ignoredDir) {
157			return true
158		}
159	}
160	return false
161}
162
163func (w *walker) walk(path string, typ os.FileMode) error {
164	dir := filepath.Dir(path)
165	if typ.IsRegular() {
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
170		}
171		if !strings.HasSuffix(path, ".go") {
172			return nil
173		}
174
175		w.add(w.root, dir)
176		return fastwalk.SkipFiles
177	}
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
185		}
186		fi, err := os.Lstat(path)
187		if err == nil && w.shouldSkipDir(fi) {
188			return filepath.SkipDir
189		}
190		return nil
191	}
192	if typ == os.ModeSymlink {
193		base := filepath.Base(path)
194		if strings.HasPrefix(base, ".#") {
195			// Emacs noise.
196			return nil
197		}
198		fi, err := os.Lstat(path)
199		if err != nil {
200			// Just ignore it.
201			return nil
202		}
203		if w.shouldTraverse(dir, fi) {
204			return fastwalk.TraverseLink
205		}
206	}
207	return nil
208}
209
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.
213func (w *walker) shouldTraverse(dir string, fi os.FileInfo) bool {
214	path := filepath.Join(dir, fi.Name())
215	target, err := filepath.EvalSymlinks(path)
216	if err != nil {
217		return false
218	}
219	ts, err := os.Stat(target)
220	if err != nil {
221		fmt.Fprintln(os.Stderr, err)
222		return false
223	}
224	if !ts.IsDir() {
225		return false
226	}
227	if w.shouldSkipDir(ts) {
228		return false
229	}
230	// Check for symlink loops by statting each directory component
231	// and seeing if any are the same file as ts.
232	for {
233		parent := filepath.Dir(path)
234		if parent == path {
235			// Made it to the root without seeing a cycle.
236			// Use this symlink.
237			return true
238		}
239		parentInfo, err := os.Stat(parent)
240		if err != nil {
241			return false
242		}
243		if os.SameFile(ts, parentInfo) {
244			// Cycle. Don't traverse.
245			return false
246		}
247		path = parent
248	}
249
250}