about summary refs log tree commit diff
path: root/external/main.go
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-12-12T16·33+0000
committerVincent Ambo <mail@tazj.in>2019-12-13T00·39+0000
commitb20e46d60b14d5e367e52ec14989dc27b4829774 (patch)
tree81b9f4a585225b31e641a88cfba42653146de717 /external/main.go
parenta5473293e78fc0b01def5c4c3f7e11854d88d8d0 (diff)
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.
Diffstat (limited to 'external/main.go')
-rw-r--r--external/main.go30
1 files changed, 10 insertions, 20 deletions
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
 }