From b20e46d60b14d5e367e52ec14989dc27b4829774 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 12 Dec 2019 16:33:10 +0000 Subject: 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. --- external/main.go | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) (limited to 'external/main.go') diff --git a/external/main.go b/external/main.go index 2e5ee4900b..0c1d84d5b7 100644 --- a/external/main.go +++ b/external/main.go @@ -38,33 +38,18 @@ type pkg struct { // findGoDirs returns a filepath.WalkFunc that identifies all // directories that contain Go source code in a certain tree. func findGoDirs(at string) ([]string, error) { - var goDirs []string - dir := "" + dirSet := make(map[string]bool) err := filepath.Walk(at, func(path string, info os.FileInfo, err error) error { - // Skip testdata - if info.IsDir() && info.Name() == "testdata" { + // Skip folders that are guaranteed to not be relevant + if info.IsDir() && (info.Name() == "testdata" || info.Name() == ".git") { 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 // (i.e. marked as a Go directory). - if strings.HasSuffix(info.Name(), ".go") && !strings.HasSuffix(info.Name(), "_test.go") { - goDirs = append(goDirs, dir) - dir = "" - return nil + if !info.IsDir() && strings.HasSuffix(info.Name(), ".go") && !strings.HasSuffix(info.Name(), "_test.go") { + dirSet[filepath.Dir(path)] = true } return nil @@ -74,6 +59,11 @@ func findGoDirs(at string) ([]string, error) { return nil, err } + goDirs := []string{} + for k, _ := range dirSet { + goDirs = append(goDirs, k) + } + return goDirs, nil } -- cgit 1.4.1