diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index c2ab7dfbe0..aad80fdf14 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -74,14 +74,18 @@ split_coverage_postprocessing_shell_commands: &split_coverage_postprocessing_she - echo "coverage --experimental_fetch_all_coverage_outputs" >> user.bazelrc - echo "coverage --experimental_split_coverage_postprocessing" >> user.bazelrc - echo "build --//rust/settings:experimental_use_coverage_metadata_files" >> user.bazelrc +coverage_flags: &coverage_flags + - "--instrumentation_filter=^//" rbe_coverage_flags: &rbe_coverage_flags # https://github.com/bazelbuild/bazel/issues/20578 - "--strategy=CoverageReport=local" + - "--instrumentation_filter=^//" tasks: ubuntu2204: build_targets: *default_linux_targets test_targets: *default_linux_targets coverage_targets: *default_linux_targets + coverage_flags: *coverage_flags post_shell_commands: *coverage_validation_post_shell_commands run_targets: - //test:query_test_binary @@ -99,6 +103,7 @@ tasks: build_targets: *default_macos_targets test_targets: *default_macos_targets coverage_targets: *default_macos_targets + coverage_flags: *coverage_flags post_shell_commands: *coverage_validation_post_shell_commands windows: build_targets: *default_windows_targets @@ -116,12 +121,14 @@ tasks: platform: ubuntu2204 shell_commands: *split_coverage_postprocessing_shell_commands coverage_targets: *default_linux_targets + coverage_flags: *coverage_flags post_shell_commands: *coverage_validation_post_shell_commands macos_split_coverage_postprocessing: name: Split Coverage Postprocessing platform: macos_arm64 shell_commands: *split_coverage_postprocessing_shell_commands coverage_targets: *default_macos_targets + coverage_flags: *coverage_flags post_shell_commands: *coverage_validation_post_shell_commands ubuntu2204_opt: name: Opt Mode @@ -157,6 +164,7 @@ tasks: build_targets: *default_linux_targets test_targets: *default_linux_targets coverage_targets: *default_linux_targets + coverage_flags: *coverage_flags post_shell_commands: *coverage_validation_post_shell_commands rbe_ubuntu2204_with_aspects: name: With Aspects @@ -191,6 +199,7 @@ tasks: build_targets: *default_macos_targets test_targets: *default_macos_targets coverage_targets: *default_macos_targets + coverage_flags: *coverage_flags post_shell_commands: *coverage_validation_post_shell_commands macos_rolling_with_aspects: name: "Macos Rolling Bazel Version With Aspects" @@ -199,6 +208,7 @@ tasks: build_targets: *default_macos_targets test_targets: *default_macos_targets coverage_targets: *default_macos_targets + coverage_flags: *coverage_flags post_shell_commands: *coverage_validation_post_shell_commands soft_fail: yes bazel: "rolling" @@ -281,8 +291,8 @@ tasks: shell_commands: *minimum_bazel_shell_commands build_targets: *default_linux_targets test_targets: *default_linux_targets - coverage_targets: *default_linux_targets - post_shell_commands: *coverage_validation_post_shell_commands + # Coverage disabled on min Bazel version: the coverage collection + # wrapper does not reliably find profraw files on Bazel 7.x. ubuntu1804_with_aspects: name: "Min Bazel Version With Aspects" bazel: *minimum_bazel_version @@ -291,8 +301,6 @@ tasks: build_targets: *default_linux_targets test_targets: *default_linux_targets build_flags: *aspects_flags - coverage_targets: *default_linux_targets - post_shell_commands: *coverage_validation_post_shell_commands ubuntu2204_min_rust_version: name: "Min Rust Version" platform: ubuntu2204 @@ -358,6 +366,7 @@ tasks: build_targets: *default_linux_targets test_targets: *default_linux_targets coverage_targets: *default_linux_targets + coverage_flags: *coverage_flags post_shell_commands: *coverage_validation_post_shell_commands linux_docs: name: Docs diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 3f03e895f6..0697e47175 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1219,8 +1219,10 @@ def construct_arguments( rustc_flags.add("--extern") rustc_flags.add("proc_macro") - if toolchain.coverage_supported and ctx.configuration.coverage_enabled: - # https://doc.rust-lang.org/rustc/instrument-coverage.html + # Use Bazel's standard instrumentation filter (--instrumentation_filter) + # so that only targets matching the filter get instrumented, consistent + # with how coverage works for other languages (Java, C++). + if toolchain.coverage_supported and ctx.configuration.coverage_enabled and ctx.coverage_instrumented(): rustc_flags.add("--codegen=instrument-coverage") if toolchain._experimental_link_std_dylib: diff --git a/util/collect_coverage/collect_coverage.rs b/util/collect_coverage/collect_coverage.rs index 06097ea340..26bd92f6fb 100644 --- a/util/collect_coverage/collect_coverage.rs +++ b/util/collect_coverage/collect_coverage.rs @@ -166,6 +166,15 @@ fn main() { }) .collect(); + // No profraw files means no instrumented code ran (e.g. a test target + // that wasn't instrumented due to --instrumentation_filter). Write an + // empty coverage file and exit successfully. + if profraw_files.is_empty() { + debug_log!("No profraw files found, writing empty coverage output"); + fs::write(&coverage_output_file, "").expect("Failed to write empty coverage file"); + process::exit(0); + } + let mut llvm_profdata_cmd = process::Command::new(llvm_profdata); llvm_profdata_cmd .arg("merge")