Skip to content

Commit

Permalink
JacocoRunner script: update for Bazel 6.0+ (#1516)
Browse files Browse the repository at this point in the history
* 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).

* 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.
  • Loading branch information
gergelyfabian authored Oct 4, 2023
1 parent 6dd98a1 commit d94ee55
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 89 deletions.
2 changes: 1 addition & 1 deletion docs/coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
3 changes: 2 additions & 1 deletion manual_test/coverage_local_jacocorunner/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.sh 6
cp /tmp/bazel_jacocorunner_build/JacocoCoverage_jarjar_deploy.jar $root_dir/manual_test/coverage_local_jacocorunner
}

Expand Down
25 changes: 25 additions & 0 deletions scripts/build_jacocorunner/0001-Build-Jacoco-for-Bazel-6.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
From 712d62a8238f3a7fe51e1cf4cc2520b5f249e1d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gergely=20F=C3=A1bi=C3=A1n?= <gergo.fb@gmail.com>
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

106 changes: 54 additions & 52 deletions scripts/build_jacocorunner/build_jacocorunner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:
#
Expand All @@ -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 \
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
#
Expand All @@ -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 \
Expand All @@ -60,15 +77,6 @@ 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
Expand All @@ -78,25 +86,32 @@ mkdir -p $build_dir
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:
jacoco_patches="$jacoco_patches 0001-Build-Jacoco-for-Bazel-5.0+.patch"
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
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
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

Expand Down Expand Up @@ -132,9 +147,10 @@ mvn clean install
(
cd $bazel_repo
git remote update
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
Expand Down

0 comments on commit d94ee55

Please sign in to comment.