Exercises # gazelle:python_generation_mode file — one rule per source
file instead of one rule per directory.
.
└── calc/
├── BUILD.bazel # # gazelle:python_generation_mode file
├── add.py # → py_library(name = "add")
├── helpers.py # → py_library(name = "helpers")
├── mul.py # → py_library(name = "mul", deps = [":add"])
└── add_test.py # → py_test(name = "add_test", deps = [":add", ":helpers", ":mul"])
- The directive is read on the package (not the workspace root), so the
switch is local to
//calcand other packages keep package-mode behavior. - Per-file libraries are named after the file basename (
add.py→:add,mul.py→:mul). - A per-file library can depend on a sibling per-file library —
mul.pyimportsfrom calc.add import add, and the resolver fills indeps = [":add"]via the RuleIndex. - Test files emit one
py_testper file. The test'sdepsaggregate every sibling library's import set, since file mode keeps the surrounding library's deps reachable from each test target.
bazel run //:gazelle # generate / update BUILD files
bazel test //... # run the per-file tests
bazel run //:gazelle -- update -mode=diff # idempotency checkThe BUILD.bazel here is checked in pre-generated; the diff check should
report no changes.