Skip to content

fix coverage generation #3491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,15 @@ def _rust_test_impl(ctx):
owner = ctx.label,
)

coverage = toolchain.llvm_cov and ctx.coverage_instrumented(target = ctx.attr.crate)
providers = rustc_compile_action(
ctx = ctx,
attr = ctx.attr,
toolchain = toolchain,
crate_info_dict = crate_info_dict,
rust_flags = get_rust_test_flags(ctx.attr),
skip_expanding_rustc_env = True,
include_coverage = coverage,
)
data = getattr(ctx.attr, "data", [])

Expand All @@ -440,7 +442,7 @@ def _rust_test_impl(ctx):
data,
{},
)
if toolchain.llvm_cov and ctx.configuration.coverage_enabled:
if coverage:
if not toolchain.llvm_profdata:
fail("toolchain.llvm_profdata is required if toolchain.llvm_cov is set.")

Expand Down
18 changes: 13 additions & 5 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,8 @@ def construct_arguments(
use_json_output = False,
build_metadata = False,
force_depend_on_objects = False,
skip_expanding_rustc_env = False):
skip_expanding_rustc_env = False,
include_coverage = False):
"""Builds an Args object containing common rustc flags

Args:
Expand Down Expand Up @@ -865,6 +866,7 @@ def construct_arguments(
build_metadata (bool): Generate CLI arguments for building *only* .rmeta files. This requires use_json_output.
force_depend_on_objects (bool): Force using `.rlib` object files instead of metadata (`.rmeta`) files even if they are available.
skip_expanding_rustc_env (bool): Whether to skip expanding CrateInfo.rustc_env_attr
include_coverage: (bool): Whether we should include coverage flags for this crate.

Returns:
tuple: A tuple of the following items
Expand Down Expand Up @@ -1057,7 +1059,7 @@ def construct_arguments(
rustc_flags.add("--extern")
rustc_flags.add("proc_macro")

if toolchain.llvm_cov and ctx.configuration.coverage_enabled:
if toolchain.llvm_cov and include_coverage:
# https://doc.rust-lang.org/rustc/instrument-coverage.html
rustc_flags.add("--codegen=instrument-coverage")

Expand Down Expand Up @@ -1157,7 +1159,8 @@ def rustc_compile_action(
force_all_deps_direct = False,
crate_info_dict = None,
skip_expanding_rustc_env = False,
include_coverage = True):
include_coverage = None,
include_coverage_provider = True):
"""Create and run a rustc compile action based on the current rule's attributes

Args:
Expand All @@ -1171,6 +1174,7 @@ def rustc_compile_action(
crate_info_dict: A mutable dict used to create CrateInfo provider
skip_expanding_rustc_env (bool, optional): Whether to expand CrateInfo.rustc_env
include_coverage (bool, optional): Whether to generate coverage information or not.
include_coverage_provider: (bool, optional): Whether to the coverage provider or not.

Returns:
list: A list of the following providers:
Expand All @@ -1180,6 +1184,9 @@ def rustc_compile_action(
"""
crate_info = rust_common.create_crate_info(**crate_info_dict)

if include_coverage == None:
include_coverage = toolchain.llvm_cov and ctx.coverage_instrumented()

build_metadata = crate_info_dict.get("metadata", None)
rustc_output = crate_info_dict.get("rustc_output", None)
rustc_rmeta_output = crate_info_dict.get("rustc_rmeta_output", None)
Expand Down Expand Up @@ -1277,6 +1284,7 @@ def rustc_compile_action(
stamp = stamp,
use_json_output = bool(build_metadata) or bool(rustc_output) or bool(rustc_rmeta_output),
skip_expanding_rustc_env = skip_expanding_rustc_env,
include_coverage = include_coverage,
)

args_metadata = None
Expand Down Expand Up @@ -1496,7 +1504,7 @@ def rustc_compile_action(
outputs = [crate_info.output]

coverage_runfiles = []
if toolchain.llvm_cov and ctx.configuration.coverage_enabled and crate_info.is_test:
if include_coverage:
coverage_runfiles = [toolchain.llvm_cov, toolchain.llvm_profdata]

experimental_use_coverage_metadata_files = toolchain._experimental_use_coverage_metadata_files
Expand Down Expand Up @@ -1549,7 +1557,7 @@ def rustc_compile_action(
# baseline_coverage.dat created here will conflict with the baseline_coverage.dat of the
# underlying target, which is a build failure. So we add an option to disable it so that this
# function can be invoked from aspects for rules that have its own InstrumentedFilesInfo.
if include_coverage:
if include_coverage and include_coverage_provider:
providers.append(
coverage_common.instrumented_files_info(
ctx,
Expand Down