# Add Code Coverage to CI for a Bazel Repository

Yes. An agent runs bazel coverage on the modules that have tests, writes a CI job that publishes the combined LCOV report, and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-coverage-to-ci/bazel/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

Coverage in Bazel is a different command from testing, with its own flags and its own output location, so the agent starts by finding which targets can produce any. In a repository like bazelbuild/examples it reads `.bazelci/presubmit.yml`, which imports nine task files for Buildkite, and looks for test targets in the modules they build. `java-maven/BUILD` has a `java_test` named `tests` with `test_class = "com.example.myproject.TestApp"` and a `container_structure_test` tagged `requires-docker`, which `.bazelci/misc.yml` excludes with `--test_tag_filters=-requires-docker`. `go-tutorial/stage3/fortune/BUILD` has a `go_test` named `fortune_test` that embeds the `fortune` library. The C++ tutorial stages have no tests at all. Each module is its own workspace with its own Bazel version: `java-maven/.bazelversion` says `8.x` and `go-tutorial/stage3/.bazelversion` says `7.4.0`. There is no `.github/workflows` directory.

It then runs coverage for real in its sandbox, one module at a time, with Bazelisk so each directory's `.bazelversion` is honoured. The command is `bazel coverage --combined_report=lcov` on the module's test targets, with the same tag filter the presubmit uses so the Docker-dependent target is skipped. The combined report is written under the output path, at `_coverage/_coverage_report.dat`, which `bazel info output_path` locates. The agent opens that file and checks that it names the library sources, `App.java` and `fortune.go`, and not only the test files. That check matters because Bazel decides which targets to instrument from the packages of the tests, so a library in a different package from its test is left out unless `--instrumentation_filter` names it. In both modules here the test sits in the library's package; the agent still writes the filter into the job so a library added later in another package is measured.

With the commands proven, it writes the CI job. Because the repository's presubmit runs on Buildkite, which the agent cannot execute from a sandbox, it adds coverage as a GitHub Actions workflow, `.github/workflows/coverage.yml`, with a matrix of one entry per module that has tests: its directory, its targets and its flags. Each entry installs Bazelisk, runs `bazel coverage`, renders the LCOV file to HTML with `genhtml` from the `lcov` package, uploads both as an artifact, and writes the line rate from `lcov --summary` to the job summary. A threshold is included but commented out, with the measured number beside it, so the team switches it on at a value they have seen.

The workflow triggers on pull requests that touch a module in the matrix and on pushes to the default branch. It does not replace `bazel test` in the presubmit: a coverage run rebuilds with instrumentation and shares no test results with an ordinary run, so making it the only job would slow the common case.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add code coverage to CI for this Bazel repository and open a pull request. Read README.md, every file under .bazelci, and every MODULE.bazel, BUILD, .bazelrc and .bazelversion first. Use bazel query in each module to list its test targets and their tags, and say which modules have none. Install Bazelisk and, in each module that has tests, run bazel coverage --combined_report=lcov on them with the same tag filters the existing CI uses; locate the combined report with bazel info output_path, and confirm it lists the library source files and not only tests. Record the line rate per module with lcov --summary. If a module's dependencies cannot be fetched, report the error and leave that module out; do not change its dependency list. Then write .github/workflows/coverage.yml: a matrix with one entry per module that has tests (directory, targets, flags), each installing Bazelisk so the module's .bazelversion is used, running bazel coverage with an explicit --instrumentation_filter for the module's packages, rendering HTML with genhtml, uploading the LCOV file and HTML as an artifact, and writing the line rate to the job summary. Include a minimum line rate check, commented out, with the measured value next to it. Trigger on pull requests that touch those modules and on pushes to the default branch. Leave the existing CI config unchanged. In the pull request description give the measured line rate per module, the modules skipped and why, and the exact commands.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that adds one file, `.github/workflows/coverage.yml`, and changes nothing else. Its description has a table with a row per module: the test targets, the Bazel version Bazelisk resolved, the line rate measured in the sandbox, and the path of the report. Below it are the modules with no tests, which is most of them in a tutorial repository, and any module that was skipped because a dependency could not be fetched, with the error quoted. In `java-maven`, for instance, the JUnit and Guava artifacts come from the two repositories `MODULE.bazel` lists, and a fetch failure there is reported, not worked around.

A reviewer checks that the matrix flags match the presubmit's, so coverage skips exactly what the tests skip; that the instrumentation filter covers the module's source packages and excludes generated or third-party code; and that the artifact contains the HTML and not just the raw file. They decide the threshold. The first run of the workflow on the pull request itself is the real confirmation, and it is what the reviewer should look at before merging, since the sandbox proved the commands and not the hosted runner.

If the team later wants the number on each pull request as a comment or sent to a coverage service, that is a follow-up to the same session: the LCOV file the job already produces is the input either needs.

## Run it on every first of the month

Coverage jobs drift: someone adds a module with tests and the matrix never hears about it. An automation in the Project with a `schedule` trigger, a `cron` of `0 6 1 * *` and your `timezone`, re-runs the survey each month. Its prompt tells the run to query every module for test targets, compare the list with the matrix in `coverage.yml`, and open a pull request that adds the missing entries with their measured line rate, or finish with a result saying the matrix is complete.

For a single repository the first run is a hand-off: from an MCP client, `create_session` with this prompt and the repository starts it, and `wait_for_sessions` returns the answer once the turn ends.

## Why run it on Rush?

Working out Bazel coverage locally means cold builds with instrumentation in each module, under each module's Bazel version, while reading a report file to see whether the right sources were instrumented. It ties up a machine and it is tedious to get right, which is why many Bazel repositories have tests and no coverage number. The agent does the cold builds in its own sandbox and keeps every command and its output in the run's record, so the line rates in the pull request can be traced to the invocation that produced them.

The workflow that comes back was derived from commands that ran, not from a template. Each flag in it appears in the transcript next to the result of using it, and where the agent had to leave a module out, the reason is the actual error text.

## Frequently asked questions

### Why is my Bazel coverage report empty or only showing test files?

Usually the instrumentation filter. Bazel instruments targets in the packages it infers from the tests, so libraries elsewhere are not measured unless `--instrumentation_filter` matches them. The agent checks the report for the library sources by name and sets the filter explicitly.

### Does it work for C++ targets?

Yes, when there are `cc_test` targets to run. C++ coverage depends on the toolchain's coverage tools being present on the runner, so the agent verifies the report in its sandbox and states the compiler it used. A module with no tests, like a tutorial stage that only builds a binary, is listed as having nothing to measure.

### Can it add coverage to our existing Buildkite or other CI instead?

It can write the step for whichever system the prompt names, using the same `bazel coverage` command. It cannot execute a hosted pipeline from its sandbox, so in that case the description separates what was run, the Bazel commands, from what was only written, the pipeline step.

### Will the coverage job slow down every pull request?

Only pull requests that touch a module in the matrix trigger it, and it runs beside the test job, not in place of it. Coverage builds are instrumented and cached separately from ordinary test runs, so the cost is one extra build of the affected module.

## More Bazel jobs

- [Set Up GitHub Actions CI for a Bazel Repository](https://getrush.ai/run/add-github-actions-ci/bazel/)
- [Migrate a Bazel WORKSPACE to Bzlmod](https://getrush.ai/run/migrate-to-bzlmod/bazel/)
- [Write Unit Tests for a C++ Project Built with Bazel](https://getrush.ai/run/write-unit-tests/bazel/)

## The same job on other stacks

- [Add Code Coverage to CI for a Chrome Extension](https://getrush.ai/run/add-coverage-to-ci/chrome-extension/)
- [Add Test Coverage Reporting to a CMake Project's CI](https://getrush.ai/run/add-coverage-to-ci/cmake/)
- [Add Test Coverage Reporting to a Cobra CLI's CI](https://getrush.ai/run/add-coverage-to-ci/cobra/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
