Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

cargo-llvm-cov

> 包管理
Open source

Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).

1.4K stars0 likes0 views
WebsiteGitHub

About

Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).

cargo-llvm-cov

Cargo subcommand to easily use LLVM source-based code coverage.

This is a wrapper around rustc [-C instrument-coverage][instrument-coverage] and provides:

  • Generate very precise coverage data. (line, region, and branch coverage. branch coverage is currently optional and requires nightly, see [#8] for more)
  • Support cargo test, cargo run, and [cargo nextest][nextest] with command-line interface compatible with cargo.
  • Support for proc-macro, including coverage of UI tests (trybuild, ui_test).
  • Support for doc tests. (this is currently optional and requires nightly, see [#2] for more)
  • Fast because it only instrument necessary crates and does not introduce extra layers between rustc, cargo, and llvm-tools.

Table of Contents:

  • Usage
    • Basic usage
    • Merge coverages generated under different test conditions
    • Get coverage of C/C++ code linked to Rust library/binary
    • Get coverage of external tests
    • Get coverage of AFL fuzzers
    • Exclude file from coverage
    • Exclude code from coverage
    • Platform Support
      • Windows
      • WASM
    • Continuous Integration
      • GitHub Actions and Codecov
      • GitLab CI
    • Display coverage in VS Code
    • Environment variables
    • Additional JSON information
  • Installation
  • Known limitations
  • Related Projects
  • License

Usage

Basic usage

Complete list of options (click to show)

(See docs directory for options of subcommands)

…

By default, run tests (via cargo test), and print the coverage summary to stdout.

bash
cargo llvm-cov

Currently, doc tests are disabled by default because nightly-only features are required to make coverage work for doc tests. see [#2] for more.

To run cargo run instead of cargo test, use run subcommand.

bash
cargo llvm-cov run

With html report (the report will be generated to target/llvm-cov/html directory):

bash
cargo llvm-cov --html
open target/llvm-cov/html/index.html

or

bash
cargo llvm-cov --open

With plain text report (if --output-path is not specified, the report will be printed to stdout):

bash
cargo llvm-cov --text | less -R

With json report (if --output-path is not specified, the report will be printed to stdout):

bash
cargo llvm-cov --json --output-path cov.json

With lcov report (if --output-path is not specified, the report will be printed to stdout):

bash
cargo llvm-cov --lcov --output-path lcov.info

You can get a coverage report in a different format based on the results of a previous run by using cargo llvm-cov report.

bash
cargo llvm-cov --html          # run tests and generate html report
cargo llvm-cov report --lcov # generate lcov report

cargo llvm-cov/cargo llvm-cov run/cargo llvm-cov nextest cleans some build artifacts by default to avoid false positives/false negatives due to old build artifacts. This behavior is disabled when --no-clean, --no-report, or --no-run is passed, and old build artifacts are retained. When using these flags, it is recommended to first run cargo llvm-cov clean --workspace to remove artifacts that may affect the coverage results.

bash
cargo llvm-cov clean --workspace # remove artifacts that may affect the coverage results
cargo llvm-cov --no-clean

Merge coverages generated under different test conditions

You can merge the coverages generated under different test conditions by using --no-report and cargo llvm-cov report.

bash
cargo llvm-cov clean --workspace # remove artifacts that may affect the coverage results
cargo llvm-cov --no-report --features a
cargo llvm-cov --no-report --features b
cargo llvm-cov report --lcov # generate report without tests

[!TIP] To include coverage for doctests you also need to pass --doctests to cargo llvm-cov report.

Get coverage of C/C++ code linked to Rust library/binary

Set CC, CXX, LLVM_COV, and LLVM_PROFDATA environment variables to Clang/LLVM compatible with the LLVM version used in rustc, and run cargo-llvm-cov with --include-ffi flag.

bash
CC=<clang-path> \
CXX=<clang++-path> \
LLVM_COV=<llvm-cov-path> \
LLVM_PROFDATA=<llvm-profdata-path> \
  cargo llvm-cov --lcov --include-ffi

Known compatible Rust (installed via rustup) and LLVM versions:

Rust 1.60-1.77 Rust 1.78-1.81 Rust 1.82-1.98 Rust 1.99-1.100
LLVM 14-17 ✓
LLVM 18 ✓
LLVM 19-22 ✓
LLVM 23 ✓

Get coverage of external tests

cargo test, cargo run, and [cargo nextest][nextest] are available as builtin, but cargo-llvm-cov can also be used for arbitrary binaries built using cargo (including other cargo subcommands or external tests that use make, [xtask], etc.)

bash
# Set the environment variables needed to get coverage.
source <(cargo llvm-cov show-env --sh)
# Remove artifacts that may affect the coverage results.
# This command should be called after show-env.
cargo llvm-cov clean --workspace
# Above two commands should be called before build binaries.

cargo build # Build rust binaries with instrumentation.
# Commands using binaries in target/debug/*, including `cargo test` and other cargo subcommands.
# ...

cargo llvm-cov report --lcov # Generate report without tests.

[!CAUTION] cargo-llvm-cov subcommands other than report and clean may not work correctly in the context where environment variables are set by show-env; consider using normal cargo/cargo-nextest/etc. commands.

[!TIP] To include coverage for doctests you also need to pass --doctests to both cargo llvm-cov show-env and cargo llvm-cov report.

[!TIP] The same thing can be achieved in PowerShell 6+ by substituting the source command with:

powershell
Invoke-Expression (cargo llvm-cov show-env --pwsh | Out-String)

See "Test show-env ..." in our CI config for usage with other shells (cmd.exe, csh, tcsh, fish, nushell, xonsh).

Get coverage of AFL fuzzers

cargo-llvm-cov can be used with AFL.rs similar to the way external tests are done, but with a few caveats.

…

Exclude file from coverage

To exclude specific file patterns from the report, use the --ignore-filename-regex option.

bash
cargo llvm-cov --open --ignore-filename-regex build

By default, vendored sources will not be included in the report. Also, the following patterns and crates specified by --exclude or --exclude-from-report are excluded:

{SEPARATOR}rustc{SEPARATOR}([0-9a-f]+|[0-9]+\.[0-9]+\.[0-9]+){SEPARATOR}
^{WORKSPACE_ROOT}({SEPARATOR}.*)?{SEPARATOR}(tests|examples|benches){SEPARATOR}
^{WORKSPACE_ROOT}({SEPARATOR}.*)?{SEPARATOR}(tests.rs|[0-9a-zA-Z_-]+[_-]tests.rs)$
^{TARGET_DIR}($|{SEPARATOR})
^{CARGO_HOME}{SEPARATOR}(registry|git){SEPARATOR}
^{RUSTUP_HOME}{SEPARATOR}toolchains($|{SEPARATOR})

You can use --no-default-ignore-filename-regex to disable default exclude setting.

Exclude code from coverage

To exclude the specific function or module from coverage, use the [#[coverage(off)] attribute][rust-lang/rust#84605].

Since #[coverage(off)] is unstable, it is recommended to use it together with cfg(coverage) or cfg(coverage_nightly) set by cargo-llvm-cov.

rust
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

// function
#[cfg_attr(coverage_nightly, coverage(off))]
fn exclude_fn_from_coverage() {
    // ...
}

// module
#[cfg_attr(coverage_nightly, coverage(off))]
mod exclude_mod_from_coverage {
    // ...
}

cfgs are set under the following conditions:

  • cfg(coverage) is always set when using cargo-llvm-cov (unless --no-cfg-coverage flag passed)
  • cfg(coverage_nightly) is set when using cargo-llvm-cov with nightly toolchain (unless --no-cfg-coverage-nightly flag passed)

Rust 1.80+ warns the above cfgs as unexpected_cfgs. The recommended way to address this is to add a lints table to Cargo.toml.

toml
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage,coverage_nightly)'] }

If you want to ignore all #[test]-related code, you can use module-level #[coverage(off)] attribute:

rust
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    // ...
}

cargo-llvm-cov excludes code contained in the directory named tests and file named tests.rs/*_tests.rs/*-tests.rs from the report by default, so you can also use it instead of #[coverage(off)] attribute.

Platform Support

Windows

{x86_64,i686}-pc-windows-msvc and {x86_64,aarch64}-pc-windows-gnullvm are confirmed to work.

As of 2025-12-30, x86_64-pc-windows-gnu (rust-lang/rust#111098) and aarch64-pc-windows-msvc (rust-lang/rust#150123) are known to not work with default setup.

WASM

See the wasm-bindgen guide for the way to get coverage for wasm32-unknown-unknown target.

Continuous Integration

GitHub Actions and Codecov

Here is an example of GitHub Actions workflow that uploads coverage to [Codecov].

…

Currently, when using --lcov flag, [only line coverage is available on Codecov][#20].

By using --codecov flag instead of --lcov flag, you can use region coverage on Codecov:

yaml
- name: Generate code coverage
  run: cargo llvm-cov --all-features --workspace --codecov --output-path codecov.json
- name: Upload coverage to Codecov
  uses: codecov/codecov-action@v5
  with:
    token: ${{ secrets.CODECOV_TOKEN }} # required for private repos or protected branches
    files: codecov.json
    fail_ci_if_error: true

Note that the way Codecov shows region/branch coverage is not very good.

GitLab CI

First of all, when running the CI you need to make sure cargo-llvm-cov is available in the execution script. Whether you add it to a custom image, or run cargo install as part of your pipeline, it should be available and in PATH. Once done, it's simple:

yaml
unit_tests:
  artifacts:
    reports:
      junit: target/nextest/default/junit.xml
      coverage_report:
        coverage_format: cobertura
        path: target/llvm-cov-target/cobertura.xml
  # this uses region for coverage summary
  coverage: '/TOTAL\s+(\d+\s+)+(\d+\.\d+\%)/'
  script:
    - cargo llvm-cov nextest
    - cargo llvm-cov report --cobertura --output-path target/llvm-cov-target/cobertura.xml

[!CAUTION] GitLab has certain [limits for Cobertura reports](https://docs.gitlab.com/ci/testing

Issues· 62 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rustcargoclicoveragerust

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category包管理
PricingOpen source

> Related tools

N
npm
Node.js 默认包管理器
P
pnpm
高效节省磁盘的 Node 包管理器
I
is-gif
Check if a Buffer/Uint8Array is a GIF image