Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gazelle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This directory contains a plugin for
[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.
Expand Down
14 changes: 6 additions & 8 deletions gazelle/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package python

import (
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -29,9 +30,6 @@ const (

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.
Expand Down Expand Up @@ -106,9 +104,9 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
// 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
}
Expand All @@ -120,7 +118,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
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
Expand All @@ -135,7 +133,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}

if !cfg.CoarseGrainedGeneration() && hasEntrypointFile(path) {
return errHaltDigging
return fs.SkipDir
}

return nil
Expand Down Expand Up @@ -168,7 +166,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
return nil
},
)
if err != nil && err != errHaltDigging {
if err != nil {
log.Printf("ERROR: %v\n", err)
return language.GenerateResult{}
}
Expand Down