Skip to content
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

Fix code coverage collection. #2001

Merged
merged 4 commits into from
Jun 13, 2023
Merged
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
33 changes: 27 additions & 6 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ crate_universe_vendor_example_targets: &crate_universe_vendor_example_targets
crate_universe_unnamed_vendor_example_targets: &crate_universe_unnamed_vendor_example_targets
- "//vendor_remote_manifests:crates_vendor"
- "//vendor_remote_pkgs:crates_vendor"
coverage_validation_post_shell_commands: &coverage_validation_post_shell_commands
- |
grep -q '^SF:.*\.rs$' bazel-out/_coverage/_coverage_report.dat \
|| { 1>&2 echo "Failed to find any Rust coverage" \
; 1>&2 cat bazel-out/_coverage/_coverage_report.dat \
; exit 1 \
; }
tasks:
ubuntu2004:
build_targets: *default_linux_targets
test_targets: *default_linux_targets
coverage_targets: *default_linux_targets
post_shell_commands: *coverage_validation_post_shell_commands
rbe_ubuntu1604:
shell_commands:
- sed -i 's/^# load("@bazelci_rules/load("@bazelci_rules/' WORKSPACE.bazel
Expand All @@ -57,6 +65,7 @@ tasks:
build_targets: *default_macos_targets
test_targets: *default_macos_targets
coverage_targets: *default_macos_targets
post_shell_commands: *coverage_validation_post_shell_commands
windows:
build_targets: *default_windows_targets
test_targets: *default_windows_targets
Expand Down Expand Up @@ -90,9 +99,11 @@ tasks:
ubuntu2004_with_aspects:
name: With Aspects
platform: ubuntu2004
build_flags: *aspects_flags
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
rbe_ubuntu1604_with_aspects:
name: With Aspects
platform: rbe_ubuntu1604
Expand Down Expand Up @@ -126,15 +137,19 @@ tasks:
macos_with_aspects:
name: With Aspects
platform: macos
build_flags: *aspects_flags
build_targets: *default_macos_targets
test_targets: *default_macos_targets
build_flags: *aspects_flags
coverage_targets: *default_macos_targets
post_shell_commands: *coverage_validation_post_shell_commands
macos_rolling_with_aspects:
name: "Macos Rolling Bazel Version With Aspects"
platform: macos
build_flags: *aspects_flags
build_targets: *default_macos_targets
test_targets: *default_macos_targets
build_flags: *aspects_flags
coverage_targets: *default_macos_targets
post_shell_commands: *coverage_validation_post_shell_commands
soft_fail: yes
bazel: "rolling"
windows_with_aspects:
Expand Down Expand Up @@ -190,17 +205,21 @@ tasks:
ubuntu1804:
name: "Min Bazel Version"
# If updating the minimum bazel version, please also update /docs/index.md
bazel: "5.0.0"
bazel: "5.2.0"
platform: ubuntu1804
build_targets: *default_linux_targets
test_targets: *default_linux_targets
coverage_targets: *default_linux_targets
post_shell_commands: *coverage_validation_post_shell_commands
ubuntu1804_with_aspects:
name: "Min Bazel Version With Aspects"
bazel: "5.0.0"
bazel: "5.2.0"
platform: ubuntu1804
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
ubuntu2004_min_rust_version:
name: "Min Rust Version"
platform: ubuntu2004
Expand Down Expand Up @@ -257,10 +276,12 @@ tasks:
name: "Rolling Bazel Version With Aspects"
bazel: "rolling"
platform: ubuntu2004
build_flags: *aspects_flags
soft_fail: yes
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
linux_docs:
name: Docs
platform: ubuntu2004
Expand Down
11 changes: 7 additions & 4 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
## https://bazel.build/docs/best-practices#bazelrc-file
###############################################################################

# https://bazel.build/reference/command-line-reference#flag--enable_platform_specific_config
common --enable_platform_specific_config

# https://bazel.build/docs/windows#symlink
startup --windows_enable_symlinks

# Required on windows
common --enable_platform_specific_config
startup:windows --windows_enable_symlinks
build:windows --enable_runfiles

# Enable the only currently supported report type
# https://bazel.build/reference/command-line-reference#flag--combined_report
coverage --combined_report=lcov

###############################################################################
## Unique configuration groups
###############################################################################
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Failure to do so will result in rules attempting to match a `stable` toolchain w

## Supported bazel versions

The oldest version of Bazel the `main` branch is tested against is `5.0.0`. Previous versions may still be functional in certain environments, but this is the minimum version we strive to fully support.
The oldest version of Bazel the `main` branch is tested against is `5.2.0`. Previous versions may still be functional in certain environments, but this is the minimum version we strive to fully support.

We test these rules against the latest rolling releases of Bazel, and aim for compatibility with them, but prioritise stable releases over rolling releases where necessary.

Expand Down
41 changes: 33 additions & 8 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,16 @@ def _rust_test_impl(ctx):
if not toolchain.llvm_profdata:
fail("toolchain.llvm_profdata is required if toolchain.llvm_cov is set.")

env["RUST_LLVM_COV"] = toolchain.llvm_cov.path
env["RUST_LLVM_PROFDATA"] = toolchain.llvm_profdata.path
llvm_cov_path = toolchain.llvm_cov.short_path
if llvm_cov_path.startswith("../"):
llvm_cov_path = llvm_cov_path[len("../"):]

llvm_profdata_path = toolchain.llvm_profdata.short_path
if llvm_profdata_path.startswith("../"):
llvm_profdata_path = llvm_profdata_path[len("../"):]

env["RUST_LLVM_COV"] = llvm_cov_path
env["RUST_LLVM_PROFDATA"] = llvm_profdata_path
components = "{}/{}".format(ctx.label.workspace_root, ctx.label.package).split("/")
env["CARGO_MANIFEST_DIR"] = "/".join([c for c in components if c])
providers.append(testing.TestEnvironment(env))
Expand Down Expand Up @@ -652,11 +660,6 @@ _common_attrs = {
),
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
"_collect_cc_coverage": attr.label(
default = Label("//util:collect_coverage"),
executable = True,
cfg = "exec",
),
"_error_format": attr.label(
default = Label("//:error_format"),
),
Expand Down Expand Up @@ -698,6 +701,28 @@ _common_attrs = {
),
}

_coverage_attrs = {
"_collect_cc_coverage": attr.label(
default = Label("//util:collect_coverage"),
executable = True,
cfg = "exec",
),
# Bazel’s coverage runner
# (https:/bazelbuild/bazel/blob/6.0.0/tools/test/collect_coverage.sh)
# needs a binary called “lcov_merge.” Its location is passed in the
# LCOV_MERGER environmental variable. For builtin rules, this variable
# is set automatically based on a magic “$lcov_merger” or
# “:lcov_merger” attribute, but it’s not possible to create such
# attributes in Starlark. Therefore we specify the variable ourselves.
# Note that the coverage runner runs in the runfiles root instead of
# the execution root, therefore we use “path” instead of “short_path.”
"_lcov_merger": attr.label(
default = configuration_field(fragment = "coverage", name = "output_generator"),
executable = True,
cfg = "exec",
),
}

_experimental_use_cc_common_link_attrs = {
"experimental_use_cc_common_link": attr.int(
doc = (
Expand Down Expand Up @@ -765,7 +790,7 @@ _rust_test_attrs = dict({
default = Label("@bazel_tools//tools/cpp:grep-includes"),
executable = True,
),
}.items() + _experimental_use_cc_common_link_attrs.items())
}.items() + _coverage_attrs.items() + _experimental_use_cc_common_link_attrs.items())

_common_providers = [
rust_common.crate_info,
Expand Down
15 changes: 12 additions & 3 deletions util/collect_coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ if [[ -n "${VERBOSE_COVERAGE:-}" ]]; then
set -x
fi

if [[ "${RUNFILES_DIR:0:1}" != "/" ]]; then
if [[ -n "${ROOT}" ]]; then
RUNFILES_DIR="${ROOT}/${RUNFILES_DIR}"
fi
fi

readonly profdata_file=$COVERAGE_DIR/coverage.profdata

"$RUNFILES_DIR/$TEST_WORKSPACE/$RUST_LLVM_PROFDATA" \
"$RUNFILES_DIR/$RUST_LLVM_PROFDATA" \
merge \
--sparse "$COVERAGE_DIR"/*.profraw \
-output "$profdata_file"

"$RUNFILES_DIR/$TEST_WORKSPACE/$RUST_LLVM_COV" \
"$RUNFILES_DIR/$RUST_LLVM_COV" \
export \
-format=lcov \
-instr-profile "$profdata_file" \
Expand All @@ -22,4 +28,7 @@ readonly profdata_file=$COVERAGE_DIR/coverage.profdata
-path-equivalence=.,"$ROOT" \
"$RUNFILES_DIR/$TEST_WORKSPACE/$TEST_BINARY" \
@"$COVERAGE_MANIFEST" \
| sed 's#/proc/self/cwd/##' > "$COVERAGE_OUTPUT_FILE"
| sed 's#/proc/self/cwd/##' > "$COVERAGE_DIR/rust_coverage.dat"

# Bazel doesn't support LLVM profdata coverage amongst other coverage formats.
rm "$profdata_file"