fix(external): Ensure findGoDirs "finds" top-level directory
Due to the lexical walk order of `filepath.Walk` the previous directory identification logic failed under certain conditions if the top-level directory contained Go files that showed up *after* the first subdirectories. To simplify the logic a set of directories is now gathered instead on a file-level.
This commit is contained in:
parent
a5473293e7
commit
b20e46d60b
1 changed files with 10 additions and 20 deletions
30
external/main.go
vendored
30
external/main.go
vendored
|
@ -38,33 +38,18 @@ type pkg struct {
|
||||||
// findGoDirs returns a filepath.WalkFunc that identifies all
|
// findGoDirs returns a filepath.WalkFunc that identifies all
|
||||||
// directories that contain Go source code in a certain tree.
|
// directories that contain Go source code in a certain tree.
|
||||||
func findGoDirs(at string) ([]string, error) {
|
func findGoDirs(at string) ([]string, error) {
|
||||||
var goDirs []string
|
dirSet := make(map[string]bool)
|
||||||
dir := ""
|
|
||||||
|
|
||||||
err := filepath.Walk(at, func(path string, info os.FileInfo, err error) error {
|
err := filepath.Walk(at, func(path string, info os.FileInfo, err error) error {
|
||||||
// Skip testdata
|
// Skip folders that are guaranteed to not be relevant
|
||||||
if info.IsDir() && info.Name() == "testdata" {
|
if info.IsDir() && (info.Name() == "testdata" || info.Name() == ".git") {
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep track of the last seen directory.
|
|
||||||
if info.IsDir() {
|
|
||||||
dir = path
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the directory has already been "popped", nothing else needs
|
|
||||||
// to happen.
|
|
||||||
if dir == "" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the current file is a Go file, then the directory is popped
|
// If the current file is a Go file, then the directory is popped
|
||||||
// (i.e. marked as a Go directory).
|
// (i.e. marked as a Go directory).
|
||||||
if strings.HasSuffix(info.Name(), ".go") && !strings.HasSuffix(info.Name(), "_test.go") {
|
if !info.IsDir() && strings.HasSuffix(info.Name(), ".go") && !strings.HasSuffix(info.Name(), "_test.go") {
|
||||||
goDirs = append(goDirs, dir)
|
dirSet[filepath.Dir(path)] = true
|
||||||
dir = ""
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -74,6 +59,11 @@ func findGoDirs(at string) ([]string, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
goDirs := []string{}
|
||||||
|
for k, _ := range dirSet {
|
||||||
|
goDirs = append(goDirs, k)
|
||||||
|
}
|
||||||
|
|
||||||
return goDirs, nil
|
return goDirs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue