From 5fa10d728c5b46938146a16db29b003be8be4b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gergely=20F=C3=A1bi=C3=A1n?= Date: Thu, 28 Sep 2023 06:43:04 +0200 Subject: [PATCH 1/2] JacocoRunner script: update for Bazel 6.0+ Added a parameter to specify the Bazel major version (to have a single script for Bazel 5 and 6). Manually tested on a repo with both Bazel versions. Had to add a new patch file for Bazel 6. Added new Bazel branch with the Jacoco interface implementation, updated for Bazel 6.3.2. Updated manual test_coverage_with_local_jacocorunner to run the script with Bazel 6 provided as a parameter (it was failing after the repo's upgrade to Bazel 6). --- .../coverage_local_jacocorunner/test.sh | 3 +- ... => 0001-Build-Jacoco-for-Bazel-5.0.patch} | 0 .../0001-Build-Jacoco-for-Bazel-6.0.patch | 25 +++++++++++++++++ .../build_jacocorunner_bazel_5.0+.sh | 28 +++++++++++++++---- 4 files changed, 50 insertions(+), 6 deletions(-) rename scripts/build_jacocorunner/{0001-Build-Jacoco-for-Bazel-5.0+.patch => 0001-Build-Jacoco-for-Bazel-5.0.patch} (100%) create mode 100644 scripts/build_jacocorunner/0001-Build-Jacoco-for-Bazel-6.0.patch diff --git a/manual_test/coverage_local_jacocorunner/test.sh b/manual_test/coverage_local_jacocorunner/test.sh index 96d788ec4..8d0e4799b 100755 --- a/manual_test/coverage_local_jacocorunner/test.sh +++ b/manual_test/coverage_local_jacocorunner/test.sh @@ -24,7 +24,8 @@ test_coverage_with_local_jacocorunner() { } build_local_jacocorunner() { - $root_dir/scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh + # Build for Bazel 6 (as this is the default Bazel version in the repo). + $root_dir/scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh 6 cp /tmp/bazel_jacocorunner_build/JacocoCoverage_jarjar_deploy.jar $root_dir/manual_test/coverage_local_jacocorunner } diff --git a/scripts/build_jacocorunner/0001-Build-Jacoco-for-Bazel-5.0+.patch b/scripts/build_jacocorunner/0001-Build-Jacoco-for-Bazel-5.0.patch similarity index 100% rename from scripts/build_jacocorunner/0001-Build-Jacoco-for-Bazel-5.0+.patch rename to scripts/build_jacocorunner/0001-Build-Jacoco-for-Bazel-5.0.patch diff --git a/scripts/build_jacocorunner/0001-Build-Jacoco-for-Bazel-6.0.patch b/scripts/build_jacocorunner/0001-Build-Jacoco-for-Bazel-6.0.patch new file mode 100644 index 000000000..394e7b47e --- /dev/null +++ b/scripts/build_jacocorunner/0001-Build-Jacoco-for-Bazel-6.0.patch @@ -0,0 +1,25 @@ +From 712d62a8238f3a7fe51e1cf4cc2520b5f249e1d3 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Gergely=20F=C3=A1bi=C3=A1n?= +Date: Fri, 18 Dec 2020 11:43:59 +0100 +Subject: [PATCH] Build Jacoco for Bazel + +--- + org.jacoco.build/pom.xml | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/org.jacoco.build/pom.xml b/org.jacoco.build/pom.xml +index 8aae1543..067cc6a7 100644 +--- a/org.jacoco.build/pom.xml ++++ b/org.jacoco.build/pom.xml +@@ -706,7 +706,7 @@ + project.getProperties().setProperty("build.date", buildDate); + + buildNumber = project.getProperties().get("buildNumber"); +- pkgName = buildNumber.substring(buildNumber.length() - 7, buildNumber.length()); ++ pkgName = "43a39aa"; + project.getProperties().setProperty("jacoco.runtime.package.name", "org.jacoco.agent.rt.internal_" + pkgName); + + void loadLicense(String libraryId) { +-- +2.25.1 + diff --git a/scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh b/scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh index b228a9fc2..711d3c053 100755 --- a/scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh +++ b/scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh @@ -75,6 +75,17 @@ build_dir=/tmp/bazel_jacocorunner_build mkdir -p $build_dir +# Read the Bazel major version. +bazel_major_version=$1 +if [ -z "$bazel_major_version" ]; then + echo "Please provide Bazel major version" + exit 1 +elif [ "$bazel_major_version" != "5" ] && [ "$bazel_major_version" != "6" ]; then + echo "Unsupported Bazel major version: $bazel_major_version" + exit 1 +fi +echo "Selected Bazel major version: $bazel_major_version" + jacoco_repo=$build_dir/jacoco # Take a fork for Jacoco that contains Scala fixes. jacoco_remote=https://github.com/gergelyfabian/jacoco @@ -83,8 +94,8 @@ jacoco_branch=0.8.7-scala # Choose the patches that you'd like to use: jacoco_patches="" -# Bazel needs to have a certain Jacoco package version: -jacoco_patches="$jacoco_patches 0001-Build-Jacoco-for-Bazel-5.0+.patch" +# Bazel needs to have a certain Jacoco package version (dependent on Bazel major version): +jacoco_patches="$jacoco_patches 0001-Build-Jacoco-for-Bazel-$bazel_major_version.0.patch" # Uncomment this if you are behind a proxy: #jacoco_patches="$jacoco_patches 0002-Build-Jacoco-behind-proxy.patch" @@ -94,9 +105,15 @@ jacoco_version=0.8.7 bazel_repo=$build_dir/bazel bazel_remote=https://github.com/gergelyfabian/bazel -bazel_version=6.0.0-pre.20220520.1 -# Version of Bazel with extending Bazel's Jacoco interface implementation for our 0.8.7-scala jacoco branch. -bazel_branch=jacoco_0.8.7_scala +if [ "$bazel_major_version" = "5" ]; then + bazel_version=6.0.0-pre.20220520.1 + # Version of Bazel with extending Bazel's Jacoco interface implementation for our 0.8.7-scala jacoco branch. + bazel_branch=jacoco_0.8.7_scala +else + bazel_version=6.3.2 + # Version of Bazel with extending Bazel's Jacoco interface implementation for our 0.8.7-scala jacoco branch. + bazel_branch=6.3.2_jacoco_0.8.7_scala +fi bazel_build_target=JacocoCoverage_jarjar_deploy.jar @@ -132,6 +149,7 @@ mvn clean install ( cd $bazel_repo git remote update +git reset --hard HEAD git checkout $bazel_branch echo "$bazel_version" > .bazelversion From 5918c550997fe8f292ce7b93e2713785efec0c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gergely=20F=C3=A1bi=C3=A1n?= Date: Thu, 28 Sep 2023 09:57:44 +0200 Subject: [PATCH 2/2] JacocoRunner script: renaming scripts to make Bazel 6.0 version the default The script for Bazel 5/6 should be the default one. Leaving the version for Bazel 4 as an alternative with a version suffix in its filename. --- docs/coverage.md | 2 +- .../coverage_local_jacocorunner/test.sh | 2 +- .../build_jacocorunner/build_jacocorunner.sh | 106 +++++++++--------- ....0+.sh => build_jacocorunner_bazel_4.0.sh} | 106 +++++++++--------- 4 files changed, 108 insertions(+), 108 deletions(-) rename scripts/build_jacocorunner/{build_jacocorunner_bazel_5.0+.sh => build_jacocorunner_bazel_4.0.sh} (64%) diff --git a/docs/coverage.md b/docs/coverage.md index 4396dbec2..d0f741c7f 100644 --- a/docs/coverage.md +++ b/docs/coverage.md @@ -70,7 +70,7 @@ build to use that one instead of the default `jacocorunner`. You can build jacocorunner with a script in `scripts/build_jacocorunner/build_jacocorunner.sh` (see comments there for more explanation and options). -A new version of this script (for Bazel 5.0+) can be found in `scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh`. +An older version of this script (for Bazel 4) can be found in `scripts/build_jacocorunner/build_jacocorunner_bazel_4.0.sh`. Then, you can use the `jacocorunner` property of `scala_toolchain` to provide the jacocorunner you have built: diff --git a/manual_test/coverage_local_jacocorunner/test.sh b/manual_test/coverage_local_jacocorunner/test.sh index 8d0e4799b..d2813a714 100755 --- a/manual_test/coverage_local_jacocorunner/test.sh +++ b/manual_test/coverage_local_jacocorunner/test.sh @@ -25,7 +25,7 @@ test_coverage_with_local_jacocorunner() { build_local_jacocorunner() { # Build for Bazel 6 (as this is the default Bazel version in the repo). - $root_dir/scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh 6 + $root_dir/scripts/build_jacocorunner/build_jacocorunner.sh 6 cp /tmp/bazel_jacocorunner_build/JacocoCoverage_jarjar_deploy.jar $root_dir/manual_test/coverage_local_jacocorunner } diff --git a/scripts/build_jacocorunner/build_jacocorunner.sh b/scripts/build_jacocorunner/build_jacocorunner.sh index 1a8ca81f6..711d3c053 100755 --- a/scripts/build_jacocorunner/build_jacocorunner.sh +++ b/scripts/build_jacocorunner/build_jacocorunner.sh @@ -4,39 +4,26 @@ # # The default `JacocoCoverage_jarjar_deploy.jar` has some issues: # -# 1. Bazel uses Jacoco 0.8.3 that has poor Scala support, including a bug that filters out Scala lambdas on Scala >=2.12 -# -# Bug report: -# https://github.com/bazelbuild/rules_scala/issues/1056 -# https://github.com/bazelbuild/bazel/issues/11674 -# -# Backported fix from Jacoco 0.8.5 to Jacoco 0.8.3 (current Bazel is not compatible with Jacoco 0.8.5): -# https://github.com/gergelyfabian/jacoco/tree/0.8.3-scala-lambda-fix -# -# 2. Bazel ignores Jacoco's filtering for branch coverage metrics: -# -# Bug report: -# https://github.com/bazelbuild/bazel/issues/12696 -# -# Proposed fix: -# https://github.com/gergelyfabian/bazel/tree/branch_coverage_respect_jacoco_filtering -# -# 3. Scala support on newer Jacoco versions (including 0.8.5) is still lacking some functionality +# 1. Scala support on newer Jacoco versions (including 0.8.7) is still lacking some functionality # # E.g. a lot of generated methods for case classes, lazy vals or other Scala features are causing falsely missed branches in branch coverage. # # Proposed changes in: # https://github.com/gergelyfabian/jacoco/tree/scala # -# Backported to 0.8.3 (to be usable with current Bazel): -# https://github.com/gergelyfabian/jacoco/tree/0.8.3-scala +# Backported to 0.8.7 (to be usable with current Bazel): +# https://github.com/gergelyfabian/jacoco/tree/0.8.7-scala +# +# 2. Bazel's code for generating `JacocoCoverage_jarjar_deploy.jar` needs changes after our Jacoco changes # -# These branches also include the Scala 2.12 lambda coverage fix. +# It implements an interface that we have extended, so that implementation also needs to be extended. +# +# This has been added on https://github.com/gergelyfabian/bazel/tree/jacoco_0.8.7_scala. # # You can use this script to build a custom version of `JacocoCoverage_jarjar_deploy.jar`, including any fixes from the above list you wish # and then provide the built jar as a parameter of `java_toolchain` and/or `scala_toolchain` to use the changed behavior for coverage. # -# Choose the fixes from the above list by configuring the used branches for Bazel and Jacoco repos below. +# Choose the fixes from the above list by configuring the used branch for Jacoco/Bazel repos below. # # Patches: # @@ -55,16 +42,12 @@ set -e - - -if [[ "$OSTYPE" == "linux-gnu"* ]]; then - readlink_cmd="readlink" -elif [[ "$OSTYPE" == "darwin"* ]]; then - readlink_cmd="greadlink" -else - echo "OS not supported: $OSTYPE" - exit 1 -fi +# Note!! +# Ensure Java 8 is used for building Jacoco (experienced issue when using e.g. Java 17). +# You may need to change this on your system. +# If this matches your system, you could uncomment these lines: +#export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 +#export PATH=$JAVA_HOME/bin:$PATH JAVA_VERSION=$(java -version 2>&1 | head -1 \ | cut -d'"' -f2 \ @@ -77,41 +60,60 @@ if [ "$JAVA_VERSION" != "8" ]; then exit 1 fi +if [[ "$OSTYPE" == "linux-gnu"* ]]; then + readlink_cmd="readlink" +elif [[ "$OSTYPE" == "darwin"* ]]; then + readlink_cmd="greadlink" +else + echo "OS not supported: $OSTYPE" + exit 1 +fi + source_path=$($readlink_cmd -f $(dirname ${BASH_SOURCE[0]})) build_dir=/tmp/bazel_jacocorunner_build mkdir -p $build_dir +# Read the Bazel major version. +bazel_major_version=$1 +if [ -z "$bazel_major_version" ]; then + echo "Please provide Bazel major version" + exit 1 +elif [ "$bazel_major_version" != "5" ] && [ "$bazel_major_version" != "6" ]; then + echo "Unsupported Bazel major version: $bazel_major_version" + exit 1 +fi +echo "Selected Bazel major version: $bazel_major_version" + jacoco_repo=$build_dir/jacoco # Take a fork for Jacoco that contains Scala fixes. jacoco_remote=https://github.com/gergelyfabian/jacoco -# Choose a branch you'd like to use. -# Default option, take only fixes for Scala 2.12 lambdas backported to Jacoco 0.8.3: -jacoco_branch=0.8.3-scala-lambda-fix -# Advanced option, take further fixes for Scala (2.11, 2.12 and 2.13) - branch in development: -#jacoco_branch=0.8.3-scala +# Take further fixes for Scala (2.11, 2.12 and 2.13) - branch in development: +jacoco_branch=0.8.7-scala # Choose the patches that you'd like to use: jacoco_patches="" -# Bazel needs to have a certain Jacoco package version: -jacoco_patches="$jacoco_patches 0001-Build-Jacoco-for-Bazel.patch" +# Bazel needs to have a certain Jacoco package version (dependent on Bazel major version): +jacoco_patches="$jacoco_patches 0001-Build-Jacoco-for-Bazel-$bazel_major_version.0.patch" # Uncomment this if you are behind a proxy: #jacoco_patches="$jacoco_patches 0002-Build-Jacoco-behind-proxy.patch" -# Jacoco version should be 0.8.3 in any case as Bazel is only compatible with that at this moment. -jacoco_version=0.8.3 +# Jacoco version should be 0.8.7 in any case as Bazel is only compatible with that at this moment. +jacoco_version=0.8.7 bazel_repo=$build_dir/bazel -bazel_remote=https://github.com/bazelbuild/bazel -bazel_branch=master -bazel_tag=4.1.0 - -# Advanced option: take a fork that has fixes for Jacoco LCOV formatter, to respect Jacoco filtering -# (fixes for Scala in Jacoco respected in Bazel branch coverage): -#bazel_remote=https://github.com/gergelyfabian/bazel -#bazel_branch=branch_coverage_respect_jacoco_filtering +bazel_remote=https://github.com/gergelyfabian/bazel +if [ "$bazel_major_version" = "5" ]; then + bazel_version=6.0.0-pre.20220520.1 + # Version of Bazel with extending Bazel's Jacoco interface implementation for our 0.8.7-scala jacoco branch. + bazel_branch=jacoco_0.8.7_scala +else + bazel_version=6.3.2 + # Version of Bazel with extending Bazel's Jacoco interface implementation for our 0.8.7-scala jacoco branch. + bazel_branch=6.3.2_jacoco_0.8.7_scala +fi bazel_build_target=JacocoCoverage_jarjar_deploy.jar @@ -147,10 +149,10 @@ mvn clean install ( cd $bazel_repo git remote update -git checkout tags/$bazel_tag +git reset --hard HEAD +git checkout $bazel_branch -# Advanced option - check out a branch instead of the release tag -# git checkout origin/$bazel_branch +echo "$bazel_version" > .bazelversion # Prepare Jacoco version. cd third_party/java/jacoco diff --git a/scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh b/scripts/build_jacocorunner/build_jacocorunner_bazel_4.0.sh similarity index 64% rename from scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh rename to scripts/build_jacocorunner/build_jacocorunner_bazel_4.0.sh index 711d3c053..1a8ca81f6 100755 --- a/scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh +++ b/scripts/build_jacocorunner/build_jacocorunner_bazel_4.0.sh @@ -4,26 +4,39 @@ # # The default `JacocoCoverage_jarjar_deploy.jar` has some issues: # -# 1. Scala support on newer Jacoco versions (including 0.8.7) is still lacking some functionality +# 1. Bazel uses Jacoco 0.8.3 that has poor Scala support, including a bug that filters out Scala lambdas on Scala >=2.12 +# +# Bug report: +# https://github.com/bazelbuild/rules_scala/issues/1056 +# https://github.com/bazelbuild/bazel/issues/11674 +# +# Backported fix from Jacoco 0.8.5 to Jacoco 0.8.3 (current Bazel is not compatible with Jacoco 0.8.5): +# https://github.com/gergelyfabian/jacoco/tree/0.8.3-scala-lambda-fix +# +# 2. Bazel ignores Jacoco's filtering for branch coverage metrics: +# +# Bug report: +# https://github.com/bazelbuild/bazel/issues/12696 +# +# Proposed fix: +# https://github.com/gergelyfabian/bazel/tree/branch_coverage_respect_jacoco_filtering +# +# 3. Scala support on newer Jacoco versions (including 0.8.5) is still lacking some functionality # # E.g. a lot of generated methods for case classes, lazy vals or other Scala features are causing falsely missed branches in branch coverage. # # Proposed changes in: # https://github.com/gergelyfabian/jacoco/tree/scala # -# Backported to 0.8.7 (to be usable with current Bazel): -# https://github.com/gergelyfabian/jacoco/tree/0.8.7-scala -# -# 2. Bazel's code for generating `JacocoCoverage_jarjar_deploy.jar` needs changes after our Jacoco changes +# Backported to 0.8.3 (to be usable with current Bazel): +# https://github.com/gergelyfabian/jacoco/tree/0.8.3-scala # -# It implements an interface that we have extended, so that implementation also needs to be extended. -# -# This has been added on https://github.com/gergelyfabian/bazel/tree/jacoco_0.8.7_scala. +# These branches also include the Scala 2.12 lambda coverage fix. # # You can use this script to build a custom version of `JacocoCoverage_jarjar_deploy.jar`, including any fixes from the above list you wish # and then provide the built jar as a parameter of `java_toolchain` and/or `scala_toolchain` to use the changed behavior for coverage. # -# Choose the fixes from the above list by configuring the used branch for Jacoco/Bazel repos below. +# Choose the fixes from the above list by configuring the used branches for Bazel and Jacoco repos below. # # Patches: # @@ -42,12 +55,16 @@ set -e -# Note!! -# Ensure Java 8 is used for building Jacoco (experienced issue when using e.g. Java 17). -# You may need to change this on your system. -# If this matches your system, you could uncomment these lines: -#export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 -#export PATH=$JAVA_HOME/bin:$PATH + + +if [[ "$OSTYPE" == "linux-gnu"* ]]; then + readlink_cmd="readlink" +elif [[ "$OSTYPE" == "darwin"* ]]; then + readlink_cmd="greadlink" +else + echo "OS not supported: $OSTYPE" + exit 1 +fi JAVA_VERSION=$(java -version 2>&1 | head -1 \ | cut -d'"' -f2 \ @@ -60,60 +77,41 @@ if [ "$JAVA_VERSION" != "8" ]; then exit 1 fi -if [[ "$OSTYPE" == "linux-gnu"* ]]; then - readlink_cmd="readlink" -elif [[ "$OSTYPE" == "darwin"* ]]; then - readlink_cmd="greadlink" -else - echo "OS not supported: $OSTYPE" - exit 1 -fi - source_path=$($readlink_cmd -f $(dirname ${BASH_SOURCE[0]})) build_dir=/tmp/bazel_jacocorunner_build mkdir -p $build_dir -# Read the Bazel major version. -bazel_major_version=$1 -if [ -z "$bazel_major_version" ]; then - echo "Please provide Bazel major version" - exit 1 -elif [ "$bazel_major_version" != "5" ] && [ "$bazel_major_version" != "6" ]; then - echo "Unsupported Bazel major version: $bazel_major_version" - exit 1 -fi -echo "Selected Bazel major version: $bazel_major_version" - jacoco_repo=$build_dir/jacoco # Take a fork for Jacoco that contains Scala fixes. jacoco_remote=https://github.com/gergelyfabian/jacoco -# Take further fixes for Scala (2.11, 2.12 and 2.13) - branch in development: -jacoco_branch=0.8.7-scala +# Choose a branch you'd like to use. +# Default option, take only fixes for Scala 2.12 lambdas backported to Jacoco 0.8.3: +jacoco_branch=0.8.3-scala-lambda-fix +# Advanced option, take further fixes for Scala (2.11, 2.12 and 2.13) - branch in development: +#jacoco_branch=0.8.3-scala # Choose the patches that you'd like to use: jacoco_patches="" -# Bazel needs to have a certain Jacoco package version (dependent on Bazel major version): -jacoco_patches="$jacoco_patches 0001-Build-Jacoco-for-Bazel-$bazel_major_version.0.patch" +# Bazel needs to have a certain Jacoco package version: +jacoco_patches="$jacoco_patches 0001-Build-Jacoco-for-Bazel.patch" # Uncomment this if you are behind a proxy: #jacoco_patches="$jacoco_patches 0002-Build-Jacoco-behind-proxy.patch" -# Jacoco version should be 0.8.7 in any case as Bazel is only compatible with that at this moment. -jacoco_version=0.8.7 +# Jacoco version should be 0.8.3 in any case as Bazel is only compatible with that at this moment. +jacoco_version=0.8.3 bazel_repo=$build_dir/bazel -bazel_remote=https://github.com/gergelyfabian/bazel -if [ "$bazel_major_version" = "5" ]; then - bazel_version=6.0.0-pre.20220520.1 - # Version of Bazel with extending Bazel's Jacoco interface implementation for our 0.8.7-scala jacoco branch. - bazel_branch=jacoco_0.8.7_scala -else - bazel_version=6.3.2 - # Version of Bazel with extending Bazel's Jacoco interface implementation for our 0.8.7-scala jacoco branch. - bazel_branch=6.3.2_jacoco_0.8.7_scala -fi +bazel_remote=https://github.com/bazelbuild/bazel +bazel_branch=master +bazel_tag=4.1.0 + +# Advanced option: take a fork that has fixes for Jacoco LCOV formatter, to respect Jacoco filtering +# (fixes for Scala in Jacoco respected in Bazel branch coverage): +#bazel_remote=https://github.com/gergelyfabian/bazel +#bazel_branch=branch_coverage_respect_jacoco_filtering bazel_build_target=JacocoCoverage_jarjar_deploy.jar @@ -149,10 +147,10 @@ mvn clean install ( cd $bazel_repo git remote update -git reset --hard HEAD -git checkout $bazel_branch +git checkout tags/$bazel_tag -echo "$bazel_version" > .bazelversion +# Advanced option - check out a branch instead of the release tag +# git checkout origin/$bazel_branch # Prepare Jacoco version. cd third_party/java/jacoco