mozilla / grcov

Rust tool to collect and aggregate code coverage data for multiple source files
Mozilla Public License 2.0
1.18k stars 148 forks source link

grcov doesn't work with proc macros #494

Open zbraniecki opened 4 years ago

zbraniecki commented 4 years ago

In https://github.com/unicode-org/icu4x/pull/331 we faced an issue where grcov failed because we have a proc macro crate.

It shows as:

error: the linked panic runtime `panic_unwind` is not compiled with this crate's panic strategy `abort`
marco-c commented 4 years ago

@zbraniecki I assume you have -Cpanic=abort -Zpanic_abort_tests in RUSTFLAGS?

zbraniecki commented 4 years ago

I did, and had to remove them to make the grcov work with proc macros.

dunnock commented 4 years ago

Faced similar issue and after suggestion https://github.com/rust-lang/rust/issues/78011 following has worked for me:

Extending my workspace's Cargo.toml has helped:

cargo-features = ["named-profiles"]

[profile.coverage]
panic = "abort"
opt-level = 0
overflow-checks = false
incremental = false
codegen-units = 1
inherits = "test"

Then I am able to collect code coverage metrics with a following command:

export RUSTFLAGS="-Zprofile"
cargo test -p $pkg --lib --all-features --profile=coverage -Z unstable-options
# and collect coverage metrics respectively
grcov ./target/coverage/ -s . -t lcov --llvm --branch --ignore-not-existing --ignore "cli*" --ignore "*test*"  --ignore "target/*"  --ignore "*migrations*" --excl-start '#\[cfg\(test\)\]' --excl-stop '^}' -o $coverage_report

Note: we have to comment out above lines in Cargo.toml when compiling on rust-stable.

gilescope commented 3 years ago

We should mention this on the readme as proc-macros are not uncommon!

marco-c commented 3 years ago

I think proc-macros are supported by source-based coverage, I've recently added support for it in grcov (version 0.6.0). Short explanation at https://marco-c.github.io/2020/11/24/rust-source-based-code-coverage.html, docs at https://github.com/mozilla/grcov#example-how-to-generate-source-based-coverage-for-a-rust-project, full example at https://github.com/marco-c/rust-code-coverage-sample.

DanielJoyce commented 3 years ago

Yeah, I know the code is called by the test, grcov flags the macro as having been called, but then the body of the function the macro is used on is listed as not called.

MichaelScofield commented 2 years ago

Running into the same problems, too. Had to use -Cpanic=unwind. Is there another walk around?