rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

Code coverage support

Open sgammon opened this issue 3 years ago • 15 comments

Hey authors of rules_rust,

Has there been any consideration yet regarding source coverage with rules_rust? Using projects like cargo-cov, or cargo-tarpaulin, it looks pretty easy from the outside but perhaps not from within Bazel.

IIRC, instrumented_files= would need to be provided for Rust targets, but that part is relatively easy if there is already a tool that can produce lcov-compatible outputs.

sgammon avatar Apr 17 '21 06:04 sgammon

forget i said anything about crates: https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/source-based-code-coverage.html

i've got a sample working locally outside of Bazel, which uses -Zinstrument-coverage/grcov/lcov to generate coverage data and merge with our canonical LCOV report, which already existed. it would be absolutely amazing if this was rolled into rules_rust, though.

i'm happy to contribute if lib maintainers can help me with a direction?

sgammon avatar Apr 17 '21 18:04 sgammon

Hey, I'm not super familiar with how coverage works but do think it should be something the rules support.

I'm wondering if other maintainers might be able to shine some light on this?

@hlopko @dfreese pinging you two because you seem to be pretty knowledgeable about some subtle details of Bazel and rustc. Hoping you'll have some thoughts 😄

UebelAndre avatar Apr 19 '21 23:04 UebelAndre

Good references should be from rules_go:

  • https://github.com/bazelbuild/rules_go/issues/140 (and some PRs mentioned here)
  • https://docs.google.com/document/d/1-ZWHF-Q-qCKf19ik-t33ie58BkNurrYYzKR4OLtcilY/edit

Generally Bazel's coverage is not yet complete. If your code coverage tooling does not follow the Java's convention, you gona have a bad time.

sluongng avatar May 11 '21 05:05 sluongng

CC @c-mita (I hear they're looking into coverage these days :)

hlopko avatar Jul 27 '21 06:07 hlopko

https://github.com/bazelbuild/rules_rust/issues/1077 seems relates

UebelAndre avatar Mar 19 '22 13:03 UebelAndre

I've been looking into this a bit, and it's definitely possible today by using the C++ related coverage script attribute. It feels a bit like a hack but afaict there still no real movement in bazel around this.

keith avatar May 07 '22 17:05 keith

I've been looking into this a bit, and it's definitely possible today by using the C++ related coverage script attribute. It feels a bit like a hack but afaict there still no real movement in bazel around this.

@keith What's this coverage script attribute?

UebelAndre avatar May 07 '22 20:05 UebelAndre

Here's a super WIP version https://github.com/bazelbuild/rules_rust/pull/1324

keith avatar May 07 '22 20:05 keith

FWIW I think that's our best shot at having working coverage for Rust anytime soon. Took a super-quick look and the PR looks reasonable. Thanks for working on it @keith!

hlopko avatar May 09 '22 14:05 hlopko

Thanks @hlopko !

keith avatar May 09 '22 16:05 keith

I've been looking into this a bit, and it's definitely possible today by using the C++ related coverage script attribute. It feels a bit like a hack but afaict there still no real movement in bazel around this.

I have a personal desire to fix this; to create a general interface for rules to use to support coverage collection (along with cross-language support). But other things have stolen my time.

The _lcov_merger and _collect_cc_coverage attributes are your best bet for now. I suppose technically those form something of an API, but they're completely undocumented.

c-mita avatar May 09 '22 16:05 c-mita

Yea I haven't addressed _lcov_merger, but am using _collect_cc_coverage in https://github.com/bazelbuild/rules_rust/pull/1324, should work fine for now

keith avatar May 09 '22 16:05 keith

Does https://github.com/bazelbuild/rules_rust/pull/1324 address the concerns here?

UebelAndre avatar Jul 11 '22 01:07 UebelAndre

I've been poking at our bazel setup to see why we don't get coverage, and it seems the rules_rust coverage data is incompatible with --experimental_split_coverage_postprocessing - I'm not sure why this should be.

Should I file a separate bug for that, or is this bug being used as a tracking bug for coverage for rust in general?

rbtcollins avatar Jan 09 '24 06:01 rbtcollins

same for me, it looks like coverage is not created when I have the flag experimental_split_coverage_postprocessing enabled

cristifalcas avatar Feb 20 '24 09:02 cristifalcas