bazelbuild / rules_rust

Rust rules for Bazel
https://bazelbuild.github.io/rules_rust/
Apache License 2.0
667 stars 431 forks source link

Bazel Coverage failing to generate lcov with `--@rules_rust//rust/settings:experimental_use_cc_common_link=true` #2729

Open Ryang20718 opened 4 months ago

Ryang20718 commented 4 months ago

bazel coverage with the following additional build setting leads to .dat files not getting generated

build --@rules_rust//rust/settings:experimental_use_cc_common_link=true

To repro, first see code cov is generated on main.

git clone https://github.com/bazelbuild/rules_rust.git
bazelisk coverage //test/inline_test_with_deps/... 
cat bazel-out/_coverage/_coverage_report.dat

With this newly added flag, code coverage fails

bazelisk coverage //test/inline_test_with_deps/... --@rules_rust//rust/settings:experimental_use_cc_common_link=true
cat bazel-out/_coverage/_coverage_report.dat

reproable on bazel 7.2.1 and 6.5

Ryang20718 commented 2 months ago

I’m attempting to debug why --@rules_rust//rust/settings:experimental_use_cc_common_link=true causes coverage instrumentation to fail to generate on rules_rust (0.49.3)

leveraging bazel’s -s --sandbox_debug, I’m seeing the main diff is --codegen=link-arg=--coverage (i think :sweat: )

i.e, for a running bazel coverage on a rust_test without this flag --@rules_rust//rust/settings:experimental_use_cc_common_link=true enabled

bazel-out/k8-opt-exec-2B5CBBC6/bin/external/rules_rust/util/process_wrapper/process_wrapper
--arg-file
bazel-out/k8-opt/bin/external/crate_index__serde-1.0.204/serde_build_script.linksearchpaths
<snip>'--codegen=linker=/usr/bin/gcc'
'--codegen=link-arg=--coverage'
'--codegen=link-arg=-fuse-ld=gold'
'--codegen=link-arg=-Wl,-no-as-needed'
'--codegen=link-arg=-Wl,-z,relro,-z,now'
'--codegen=link-arg=-B/usr/bin'
'--codegen=link-arg=-pass-exit-codes'
'--codegen=link-arg=-lstdc++'
'--codegen=link-arg=-lm'
'--codegen=link-arg=-Wl,--gc-sections'
'--extern=serde=bazel-out/k8-opt/bin/external/crate_index__serde-1.0.204/libserde-1107839584.rlib'
'--extern=thiserror=bazel-out/k8-opt/bin/external/crate_index__thiserror-1.0.63/libthiserror-1171055757.rlib'
'-Ldependency=bazel-out/k8-opt-exec-2B5CBBC6/bin/external/crate_index__serde_derive-1.0.204'
'-Ldependency=bazel-out/k8-opt/bin/external/crate_index__serde-1.0.204'
'-Ldependency=bazel-out/k8-opt-exec-2B5CBBC6/bin/external/crate_index__thiserror-impl-1.0.63'
'-Ldependency=bazel-out/k8-opt/bin/external/crate_index__thiserror-1.0.63'
'--codegen=instrument-coverage'
'-Ctarget-cpu=x86-64-v3'
'-Cforce-frame-pointers=y'
'-Cforce-unwind-tables=y')

With this flag enabled, we don’t see codegen=link-arg=--coverage at all

I think it has to do with this: https://github.com/bazelbuild/rules_rust/blob/ef8ac18bb44f8b480b7f50b6c031504a94785f2e/rust/private/rustc.bzl#L1317-L1333

I think we need to build and link these artifacts https://github.com/bazelbuild/rules_rust/blob/ef8ac18bb44f8b480b7f50b6c031504a94785f2e/rust/private/rustc.bzl#L1283-L1286 with --coverage flags, but I’m not entirely sure where those should be added? Wondering if I’m heading down the right direction?