From c2396fb7a2bf647b193026dc9ab54adf7f15e680 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Tue, 13 Jun 2023 06:30:47 -0700 Subject: [PATCH 1/3] Minor CI and test cleanup --- .bazelci/presubmit.yml | 13 ++++++------- test/BUILD.bazel | 19 ------------------- test/rust/BUILD.bazel | 16 ++++++++++++++++ test/rustc_env_files/BUILD.bazel | 6 ++++-- test/rustc_env_files/output_test.rs | 18 ++++++++++++++++++ test/rustc_env_files/output_test.sh | 6 ------ 6 files changed, 44 insertions(+), 34 deletions(-) create mode 100644 test/rustc_env_files/output_test.rs delete mode 100755 test/rustc_env_files/output_test.sh diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 80e7bc5d6d..7b3afaa950 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -50,7 +50,6 @@ tasks: test_targets: - "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245 - "//..." - - "//test/..." - "-//test/conflicting_deps:conflicting_deps_test" # TODO: This test does not work on RBE - "-//test/versioned_dylib:versioned_dylib_test" @@ -103,8 +102,7 @@ tasks: build_targets: *default_linux_targets test_targets: - "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245 - - "..." - - "//test/..." + - "//..." - "-//test/conflicting_deps:conflicting_deps_test" # TODO: This test does not work on RBE - "-//test/versioned_dylib:versioned_dylib_test" @@ -118,8 +116,7 @@ tasks: build_targets: *default_linux_targets test_targets: - "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245 - - "..." - - "//test/..." + - "//..." - "-//test/conflicting_deps:conflicting_deps_test" # TODO: This test does not work on RBE - "-//test/versioned_dylib:versioned_dylib_test" @@ -302,11 +299,12 @@ tasks: working_directory: examples build_flags: - "--repo_env=CC=clang" + - "--config=rustfmt" + - "--config=clippy" build_targets: - "//..." test_targets: - "//..." - build_flags: *aspects_flags ubuntu2004_examples_clang_lld: name: Examples with Clang and LLD platform: ubuntu2004 @@ -316,11 +314,12 @@ tasks: build_flags: - "--repo_env=CC=clang" - "--linkopt=-fuse-ld=lld" + - "--config=rustfmt" + - "--config=clippy" build_targets: - "//..." test_targets: - "//..." - build_flags: *aspects_flags ubuntu2004_examples_rolling: name: "Examples with Rolling Bazel Version" platform: ubuntu2004 diff --git a/test/BUILD.bazel b/test/BUILD.bazel index a217437d3e..e69de29bb2 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -1,19 +0,0 @@ -load( - "@bazel_tools//tools/build_rules:test_rules.bzl", - "rule_test", -) - -rule_test( - name = "hello_lib_rule_test", - generates = ["libhello_lib-683707109.rlib"], - rule = "//test/rust:hello_lib", -) - -rule_test( - name = "hello_world_rule_test", - generates = select({ - "//rust/platform:windows": ["hello_world.exe"], - "//conditions:default": ["hello_world"], - }), - rule = "//test/rust:hello_world", -) diff --git a/test/rust/BUILD.bazel b/test/rust/BUILD.bazel index 4209f4ce87..ad873a4dc6 100644 --- a/test/rust/BUILD.bazel +++ b/test/rust/BUILD.bazel @@ -1,3 +1,4 @@ +load("@bazel_tools//tools/build_rules:test_rules.bzl", "rule_test") load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test") package(default_visibility = ["//visibility:public"]) @@ -24,3 +25,18 @@ rust_test( name = "hello_lib_test", crate = ":hello_lib", ) + +rule_test( + name = "hello_lib_rule_test", + generates = ["libhello_lib-683707109.rlib"], + rule = ":hello_lib", +) + +rule_test( + name = "hello_world_rule_test", + generates = select({ + "//rust/platform:windows": ["hello_world.exe"], + "//conditions:default": ["hello_world"], + }), + rule = "//test/rust:hello_world", +) diff --git a/test/rustc_env_files/BUILD.bazel b/test/rustc_env_files/BUILD.bazel index cb48b2ef4a..d32457fa43 100644 --- a/test/rustc_env_files/BUILD.bazel +++ b/test/rustc_env_files/BUILD.bazel @@ -20,11 +20,13 @@ write_file( ], ) -sh_test( +rust_test( name = "output_test", - srcs = ["output_test.sh"], + srcs = ["output_test.rs"], args = ["$(rootpath :hello_env)"], data = [":hello_env"], + edition = "2018", + use_libtest_harness = False, ) rust_library( diff --git a/test/rustc_env_files/output_test.rs b/test/rustc_env_files/output_test.rs new file mode 100644 index 0000000000..daa71c3ca4 --- /dev/null +++ b/test/rustc_env_files/output_test.rs @@ -0,0 +1,18 @@ +use std::process; + +fn main() { + let binary = std::env::args().nth(1).expect("No argument was provided"); + + let output = process::Command::new(binary) + .output() + .expect("Failed to spawn process"); + if !output.status.success() { + eprintln!("Failed to execute binary"); + eprintln!("{}", std::str::from_utf8(&output.stdout).unwrap()); + eprintln!("{}", std::str::from_utf8(&output.stderr).unwrap()); + process::exit(output.status.code().unwrap()); + } + + let stdout = std::str::from_utf8(&output.stdout).unwrap().trim(); + assert_eq!("Howdy from version 1.2.3", stdout); +} diff --git a/test/rustc_env_files/output_test.sh b/test/rustc_env_files/output_test.sh deleted file mode 100755 index 9bb91fe474..0000000000 --- a/test/rustc_env_files/output_test.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -eu - -set -o pipefail - -output="$($1)" -[[ "${output}" == "Howdy from version 1.2.3" ]] || { echo >&2 "Unexpected output: ${output}"; exit 1;} From 270d735599659061f965cb3313d04963b3fdc054 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Sat, 10 Jun 2023 10:57:07 -0700 Subject: [PATCH 2/3] Bump min tested Bazel version to 5.2.0 --- .bazelci/presubmit.yml | 4 ++-- docs/index.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 7b3afaa950..d64ad1919b 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -190,13 +190,13 @@ 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 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 diff --git a/docs/index.md b/docs/index.md index 78fdd1d5ca..78ed9a77cb 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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. From e4748862016f8081067e6d913e600b52d7238de1 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Tue, 13 Jun 2023 06:00:24 -0700 Subject: [PATCH 3/3] Fix code coverage collection. --- .bazelci/presubmit.yml | 29 ++++++++++++++++++++++++---- .bazelrc | 11 +++++++---- rust/private/rust.bzl | 41 ++++++++++++++++++++++++++++++++-------- util/collect_coverage.sh | 15 ++++++++++++--- 4 files changed, 77 insertions(+), 19 deletions(-) diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index d64ad1919b..5a17ccc9a2 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -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 @@ -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 @@ -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 @@ -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: @@ -194,6 +209,8 @@ tasks: 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.2.0" @@ -201,6 +218,8 @@ 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 ubuntu2004_min_rust_version: name: "Min Rust Version" platform: ubuntu2004 @@ -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 diff --git a/.bazelrc b/.bazelrc index 1a11f65a62..b4d00296fd 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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 ############################################################################### diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 0cc569ecaf..2316981435 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -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)) @@ -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"), ), @@ -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://github.com/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 = ( @@ -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, diff --git a/util/collect_coverage.sh b/util/collect_coverage.sh index 328a5c6f01..648de5eeef 100755 --- a/util/collect_coverage.sh +++ b/util/collect_coverage.sh @@ -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" \ @@ -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"