[gazelle] Use filepath.WalkDir instead of filepath.Walk (#770)
diff --git a/gazelle/README.md b/gazelle/README.md index 51055cb..fe3fb2d 100644 --- a/gazelle/README.md +++ b/gazelle/README.md
@@ -4,6 +4,8 @@ [Gazelle](https://github.com/bazelbuild/bazel-gazelle) that generates BUILD file content for Python code. +It requires Go 1.16+ to compile. + ## Installation First, you'll need to add Gazelle to your `WORKSPACE` file.
diff --git a/gazelle/generate.go b/gazelle/generate.go index 685068d..077acb8 100644 --- a/gazelle/generate.go +++ b/gazelle/generate.go
@@ -2,6 +2,7 @@ import ( "fmt" + "io/fs" "log" "os" "path/filepath" @@ -29,9 +30,6 @@ var ( buildFilenames = []string{"BUILD", "BUILD.bazel"} - // errHaltDigging is an error that signals whether the generator should halt - // digging the source tree searching for modules in subdirectories. - errHaltDigging = fmt.Errorf("halt digging") ) // GenerateRules extracts build metadata from source files in a directory. @@ -106,9 +104,9 @@ // boundaryPackages represents child Bazel packages that are used as a // boundary to stop processing under that tree. boundaryPackages := make(map[string]struct{}) - err := filepath.Walk( + err := filepath.WalkDir( filepath.Join(args.Dir, d), - func(path string, info os.FileInfo, err error) error { + func(path string, entry fs.DirEntry, err error) error { if err != nil { return err } @@ -120,7 +118,7 @@ return nil } } - if info.IsDir() { + if entry.IsDir() { // If we are visiting a directory, we determine if we should // halt digging the tree based on a few criterias: // 1. The directory has a BUILD or BUILD.bazel files. Then @@ -135,7 +133,7 @@ } if !cfg.CoarseGrainedGeneration() && hasEntrypointFile(path) { - return errHaltDigging + return fs.SkipDir } return nil @@ -168,7 +166,7 @@ return nil }, ) - if err != nil && err != errHaltDigging { + if err != nil { log.Printf("ERROR: %v\n", err) return language.GenerateResult{} }