Skip to content

Read Bazel as a build system: tests, build types, .bazelrc options, proto_library - #381

Open
leftibot wants to merge 2 commits into
cpp-best-practices:mainfrom
leftibot:bazel
Open

leftibot wants to merge 2 commits into
cpp-best-practices:mainfrom
leftibot:bazel

Conversation

@leftibot

Copy link
Copy Markdown
Collaborator

Bazel is now read as a build system (#207).

googletest (whose primary build is Bazel), Catch2, nlohmann/json, SwiftLint, grpc-proto, envoy's data-plane-api and protovalidate build or test with Bazel. Nothing read it: their bazel test jobs counted as plain jobs with no configuration, .bazelrc's -Werror and -std=c++17 were unseen, and grpc-proto's proto_library rules weren't schema validation.

What is read

  • Recognition: a MODULE.bazel, WORKSPACE(.bazel) or BUILD.bazel makes Bazel a build system of the C, C++ and Swift profiles. Its output directories (bazel-bin, bazel-<workspace>) are build trees.
  • BUILD files: they're parsed for the rules they call (build_systems/bazel/syntax.py).
    • Each file records the kinds of rule it uses and its test rules (cc_test, swift_test, any *_test).
    • A statement that doesn't parse is a problem, and the rest of the file is still read. Over the corpus's 613 Bazel files, only treefmt-nix's deliberately invalid test file fails.
    • Starlark's syntax is Python's, so statements are parsed with Python's own ast. That's the standard library, not a parsing dependency (no tree-sitter). Only literals are read as values; anything computed (glob(), select(), +) is kept as text.
  • tests-run: bazel test and bazel coverage run the tests the BUILD files define. This also works through bazelisk and past startup options (bazel --batch test).
  • ci-build-types: a command's last --compilation_mode / -c is its build type. dbg is Debug, opt is Release, and the default, fastbuild, is neither.
  • .bazelrc: a bazel command is expanded into itself with the options its rc files give it written out, plus --ignore_all_rc_files, with which bazel reads none. It's the same command, so --copt=-Werror, --cxxopt=-std=c++17 and -c opt there count as its own, and the existing flag questions see them.
    • A command takes common's options and those of the commands it inherits (test inherits build; coverage inherits test).
    • --config=name brings each command's :name options in its place, recursively (googletest's build:gcc --config=linux).
    • With --enable_platform_specific_config (Catch2, SwiftLint), the config for the host's OS applies, in the builds on that OS.
    • import and try-import of workspace files are followed (at most 10 deep, tested at the limit), as is a --bazelrc= file.
  • schema-validation: a bazel build (or test) validates Protobuf schemas when the BUILD files define a proto_library rule, which protoc compiles as the build does.

Left for a decision

Bazel's own files could be judged as a format, as CMake's are in #378: formatted and linted by buildifier (buildifier -mode=check, -lint=warn). That would add failures to the seven Bazel projects in the corpus, so it's left out until you decide. Until then, .bzl macro files stay listed as an unrecognised suffix, while BUILD and MODULE files are accounted as build files.

Corpus

Compared against main's recording, the seven Bazel projects now list Bazel among their build systems. Two statuses change:

  • grpc-proto: schema-validation goes FAIL→PASS, from Bazel's proto_library. This holds under --strict too.
  • envoy's data-plane-api: schema-validation goes FAIL→PASS. tests-run goes FAIL→WARN: its C++ cc_test rules run by bazel test now count, and its auxiliary Go tests still warn.

Other changes, which leave statuses as they were:

  • googletest:
    • tests-run counts the Bazel tests beside CTest's;
    • compiler-warnings now finds -Wall for appleclang, from .bazelrc's build:macos, which CI brings through --config;
    • it still misses other flags, so the status is unchanged.
  • Catch2: tests-run counts the Bazel tests beside CTest's and Meson's.
  • protovalidate: schema-validation credits Bazel proto_library beside buf build.
  • SwiftLint: its C code's tests-run now finds its 3 Bazel test files. One runner covers every rule's language, so SwiftLint's swift_test rules count for its C code too. Splitting the runner by rule kind can follow if you want it stricter.
  • Unrecognised suffixes: .bazel leaves the list everywhere. .bzl, .bazelrc and .bzlmod stay (see "Left for a decision").

Tests

  • New test files: tests/test_starlark_syntax.py (the reader), tests/test_bazel.py (recognition, the rules and tests read, build trees, build types, tests-run) and tests/test_bazelrc.py (commands, configs, platform configs, imports, --bazelrc, cycles). Protobuf's tests cover proto_library, and there's a limit test for rc imports.
  • Mutants: every branch of the new code was checked against a mutant. Each survivor got a test or, where the mutated code was redundant, a simplification.
  • Updated expectations: the C/C++ tests-run messages now name bazel test among the runners, and the snapshots list the new tools.

Gates

pytest, ruff, ruff format, mypy, lizard, coverage, jscpd and strict self-validation: all pass.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt

leftibot and others added 2 commits November 24, 2026 21:55
…h command

googletest (whose primary build is Bazel), Catch2, nlohmann/json and SwiftLint build and test
with Bazel, but nothing read it. Their `bazel test` jobs counted as plain jobs with no
configuration, and `.bazelrc`'s `-Werror`, `-std=c++17` and `--compilation_mode` were unseen
(cpp-best-practices#207).

- Recognition: a MODULE.bazel, WORKSPACE(.bazel) or BUILD.bazel makes Bazel a build system of
  the C, C++ and Swift profiles. Its output directories (`bazel-bin`, `bazel-<workspace>`) are
  build trees.
- BUILD files are parsed for the rules they call (`build_systems/bazel/syntax`). A statement
  that does not parse is a problem, and the rest of the file is still read.
  - Each file records the kinds of rule it uses and its test rules (`cc_test`, `swift_test`,
    any `*_test`).
  - Starlark's syntax is Python's, so statements are parsed with Python's own `ast`. It is in
    the standard library, not a parsing dependency. Only literals are read as values;
    anything computed (`glob()`, `select()`, `+`) is kept as its text.
- Tests: `bazel test` and `bazel coverage` (also through bazelisk, past startup options) run
  the tests the BUILD files define.
- Build types: a command's last `--compilation_mode` / `-c` is its build type. `dbg` is Debug,
  `opt` is Release, and the default, `fastbuild`, is neither.
- `.bazelrc`: a bazel command is expanded into itself with the options its rc files give it
  written out, plus `--ignore_all_rc_files`, with which bazel reads none. This is the same
  command, so `--copt=-Werror` and `-c opt` count as its own.
  - A command takes the options of `common` and of the commands it inherits (test inherits
    build; coverage inherits test).
  - `--config=name` brings each command's `:name` options in its place, recursively.
  - With `--enable_platform_specific_config`, the config for the host's OS applies, in the
    builds on that OS.
  - `import` and `try-import` of workspace files are followed, as is a `--bazelrc=` file;
    imports nest at most 10 deep.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
grpc-proto's only CI job is `bazelisk build //...` over BUILD files of `proto_library` rules,
and envoy's data-plane-api builds its APIs the same way. Bazel compiles each rule's schemas
with protoc, but schema-validation failed with "none found" (cpp-best-practices#207).

A `bazel build` (or `test`, `coverage`) now validates the schemas when the BUILD files define a
`proto_library` rule. A command that builds nothing (`bazel query`) does not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.18182% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.49%. Comparing base (79c1e97) to head (bc7e7b2).

Files with missing lines Patch % Lines
project_validator/build_systems/bazel/rc.py 98.26% 2 Missing ⚠️
project_validator/build_systems/bazel/syntax.py 97.72% 2 Missing ⚠️
project_validator/build_systems/bazel/build.py 98.14% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #381      +/-   ##
==========================================
- Coverage   99.50%   99.49%   -0.02%     
==========================================
  Files         320      324       +4     
  Lines       21149    21421     +272     
  Branches     2540     2589      +49     
==========================================
+ Hits        21045    21312     +267     
- Misses        104      109       +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant