Skip to content
17 changes: 13 additions & 4 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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=^//"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the default? If so is it necessary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://bazel.build/reference/command-line-reference#flag--instrumentation_filter shows the default is -/javatests[/:],-/test/java[/:]

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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions util/collect_coverage/collect_coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading