cargo llvm-cov: Coverage Reports in CI
cargo llvm-cov runs your tests with LLVM source-based instrumentation and emits a coverage report in formats like lcov, Cobertura, or a terminal summary.
It wraps the rustc instrument-coverage machinery so you do not have to set the env vars by hand. Output plugs straight into Codecov or a coverage gate.
What it does
cargo llvm-cov builds the workspace with -C instrument-coverage, runs the tests, and merges the profraw data into a report. --lcov --output-path lcov.info writes an lcov file; --fail-under-lines N makes the command exit non-zero below a threshold.
Common usage
cargo llvm-cov --workspace --lcov --output-path lcov.info
cargo llvm-cov --workspace --fail-under-lines 80
cargo llvm-cov nextest --profile ci # coverage via nextest
cargo llvm-cov --html # local HTML reportFlags
| Flag | What it does |
|---|---|
| --lcov --output-path <f> | Write an lcov.info report |
| --cobertura | Emit Cobertura XML |
| --workspace | Cover all workspace crates |
| --fail-under-lines <n> | Exit non-zero if line coverage is below n% |
| nextest | Run coverage through cargo-nextest |
| --ignore-filename-regex <re> | Exclude paths from the report |
In CI
Install via cargo install cargo-llvm-cov --locked and add the rustc component with rustup component add llvm-tools-preview. Coverage builds are not cache-compatible with normal builds (different flags), so use a separate target/ or accept a rebuild. Upload lcov.info to your coverage service after the run.
Common errors in CI
"error: failed to find llvm-tools-preview, please install it" means run rustup component add llvm-tools-preview. "error: no profraw files found" means the tests did not run or instrumentation was off. A non-zero exit with "error: coverage ... is below the fail-under threshold" is the gate firing as intended.