Skip to content

Commit d94ee55

Browse files
JacocoRunner script: update for Bazel 6.0+ (#1516)
* 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.
1 parent 6dd98a1 commit d94ee55

File tree

6 files changed

+133
-89
lines changed

6 files changed

+133
-89
lines changed

docs/coverage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ build to use that one instead of the default `jacocorunner`.
7070

7171
You can build jacocorunner with a script in `scripts/build_jacocorunner/build_jacocorunner.sh` (see comments there for more explanation and options).
7272

73-
A new version of this script (for Bazel 5.0+) can be found in `scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh`.
73+
An older version of this script (for Bazel 4) can be found in `scripts/build_jacocorunner/build_jacocorunner_bazel_4.0.sh`.
7474

7575
Then, you can use the `jacocorunner` property of `scala_toolchain` to provide the jacocorunner you have built:
7676

manual_test/coverage_local_jacocorunner/test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ test_coverage_with_local_jacocorunner() {
2424
}
2525

2626
build_local_jacocorunner() {
27-
$root_dir/scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh
27+
# Build for Bazel 6 (as this is the default Bazel version in the repo).
28+
$root_dir/scripts/build_jacocorunner/build_jacocorunner.sh 6
2829
cp /tmp/bazel_jacocorunner_build/JacocoCoverage_jarjar_deploy.jar $root_dir/manual_test/coverage_local_jacocorunner
2930
}
3031

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
From 712d62a8238f3a7fe51e1cf4cc2520b5f249e1d3 Mon Sep 17 00:00:00 2001
2+
From: =?UTF-8?q?Gergely=20F=C3=A1bi=C3=A1n?= <[email protected]>
3+
Date: Fri, 18 Dec 2020 11:43:59 +0100
4+
Subject: [PATCH] Build Jacoco for Bazel
5+
6+
---
7+
org.jacoco.build/pom.xml | 2 +-
8+
1 file changed, 1 insertion(+), 1 deletion(-)
9+
10+
diff --git a/org.jacoco.build/pom.xml b/org.jacoco.build/pom.xml
11+
index 8aae1543..067cc6a7 100644
12+
--- a/org.jacoco.build/pom.xml
13+
+++ b/org.jacoco.build/pom.xml
14+
@@ -706,7 +706,7 @@
15+
project.getProperties().setProperty("build.date", buildDate);
16+
17+
buildNumber = project.getProperties().get("buildNumber");
18+
- pkgName = buildNumber.substring(buildNumber.length() - 7, buildNumber.length());
19+
+ pkgName = "43a39aa";
20+
project.getProperties().setProperty("jacoco.runtime.package.name", "org.jacoco.agent.rt.internal_" + pkgName);
21+
22+
void loadLicense(String libraryId) {
23+
--
24+
2.25.1
25+

scripts/build_jacocorunner/build_jacocorunner.sh

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,26 @@
44
#
55
# The default `JacocoCoverage_jarjar_deploy.jar` has some issues:
66
#
7-
# 1. Bazel uses Jacoco 0.8.3 that has poor Scala support, including a bug that filters out Scala lambdas on Scala >=2.12
8-
#
9-
# Bug report:
10-
# https://github.com/bazelbuild/rules_scala/issues/1056
11-
# https://github.com/bazelbuild/bazel/issues/11674
12-
#
13-
# Backported fix from Jacoco 0.8.5 to Jacoco 0.8.3 (current Bazel is not compatible with Jacoco 0.8.5):
14-
# https://github.com/gergelyfabian/jacoco/tree/0.8.3-scala-lambda-fix
15-
#
16-
# 2. Bazel ignores Jacoco's filtering for branch coverage metrics:
17-
#
18-
# Bug report:
19-
# https://github.com/bazelbuild/bazel/issues/12696
20-
#
21-
# Proposed fix:
22-
# https://github.com/gergelyfabian/bazel/tree/branch_coverage_respect_jacoco_filtering
23-
#
24-
# 3. Scala support on newer Jacoco versions (including 0.8.5) is still lacking some functionality
7+
# 1. Scala support on newer Jacoco versions (including 0.8.7) is still lacking some functionality
258
#
269
# E.g. a lot of generated methods for case classes, lazy vals or other Scala features are causing falsely missed branches in branch coverage.
2710
#
2811
# Proposed changes in:
2912
# https://github.com/gergelyfabian/jacoco/tree/scala
3013
#
31-
# Backported to 0.8.3 (to be usable with current Bazel):
32-
# https://github.com/gergelyfabian/jacoco/tree/0.8.3-scala
14+
# Backported to 0.8.7 (to be usable with current Bazel):
15+
# https://github.com/gergelyfabian/jacoco/tree/0.8.7-scala
16+
#
17+
# 2. Bazel's code for generating `JacocoCoverage_jarjar_deploy.jar` needs changes after our Jacoco changes
3318
#
34-
# These branches also include the Scala 2.12 lambda coverage fix.
19+
# It implements an interface that we have extended, so that implementation also needs to be extended.
20+
#
21+
# This has been added on https://github.com/gergelyfabian/bazel/tree/jacoco_0.8.7_scala.
3522
#
3623
# You can use this script to build a custom version of `JacocoCoverage_jarjar_deploy.jar`, including any fixes from the above list you wish
3724
# and then provide the built jar as a parameter of `java_toolchain` and/or `scala_toolchain` to use the changed behavior for coverage.
3825
#
39-
# Choose the fixes from the above list by configuring the used branches for Bazel and Jacoco repos below.
26+
# Choose the fixes from the above list by configuring the used branch for Jacoco/Bazel repos below.
4027
#
4128
# Patches:
4229
#
@@ -55,16 +42,12 @@
5542

5643
set -e
5744

58-
59-
60-
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
61-
readlink_cmd="readlink"
62-
elif [[ "$OSTYPE" == "darwin"* ]]; then
63-
readlink_cmd="greadlink"
64-
else
65-
echo "OS not supported: $OSTYPE"
66-
exit 1
67-
fi
45+
# Note!!
46+
# Ensure Java 8 is used for building Jacoco (experienced issue when using e.g. Java 17).
47+
# You may need to change this on your system.
48+
# If this matches your system, you could uncomment these lines:
49+
#export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
50+
#export PATH=$JAVA_HOME/bin:$PATH
6851

6952
JAVA_VERSION=$(java -version 2>&1 | head -1 \
7053
| cut -d'"' -f2 \
@@ -77,41 +60,60 @@ if [ "$JAVA_VERSION" != "8" ]; then
7760
exit 1
7861
fi
7962

63+
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
64+
readlink_cmd="readlink"
65+
elif [[ "$OSTYPE" == "darwin"* ]]; then
66+
readlink_cmd="greadlink"
67+
else
68+
echo "OS not supported: $OSTYPE"
69+
exit 1
70+
fi
71+
8072
source_path=$($readlink_cmd -f $(dirname ${BASH_SOURCE[0]}))
8173

8274
build_dir=/tmp/bazel_jacocorunner_build
8375

8476
mkdir -p $build_dir
8577

78+
# Read the Bazel major version.
79+
bazel_major_version=$1
80+
if [ -z "$bazel_major_version" ]; then
81+
echo "Please provide Bazel major version"
82+
exit 1
83+
elif [ "$bazel_major_version" != "5" ] && [ "$bazel_major_version" != "6" ]; then
84+
echo "Unsupported Bazel major version: $bazel_major_version"
85+
exit 1
86+
fi
87+
echo "Selected Bazel major version: $bazel_major_version"
88+
8689
jacoco_repo=$build_dir/jacoco
8790
# Take a fork for Jacoco that contains Scala fixes.
8891
jacoco_remote=https://github.com/gergelyfabian/jacoco
89-
# Choose a branch you'd like to use.
90-
# Default option, take only fixes for Scala 2.12 lambdas backported to Jacoco 0.8.3:
91-
jacoco_branch=0.8.3-scala-lambda-fix
92-
# Advanced option, take further fixes for Scala (2.11, 2.12 and 2.13) - branch in development:
93-
#jacoco_branch=0.8.3-scala
92+
# Take further fixes for Scala (2.11, 2.12 and 2.13) - branch in development:
93+
jacoco_branch=0.8.7-scala
9494

9595
# Choose the patches that you'd like to use:
9696
jacoco_patches=""
97-
# Bazel needs to have a certain Jacoco package version:
98-
jacoco_patches="$jacoco_patches 0001-Build-Jacoco-for-Bazel.patch"
97+
# Bazel needs to have a certain Jacoco package version (dependent on Bazel major version):
98+
jacoco_patches="$jacoco_patches 0001-Build-Jacoco-for-Bazel-$bazel_major_version.0.patch"
9999
# Uncomment this if you are behind a proxy:
100100
#jacoco_patches="$jacoco_patches 0002-Build-Jacoco-behind-proxy.patch"
101101

102102

103-
# Jacoco version should be 0.8.3 in any case as Bazel is only compatible with that at this moment.
104-
jacoco_version=0.8.3
103+
# Jacoco version should be 0.8.7 in any case as Bazel is only compatible with that at this moment.
104+
jacoco_version=0.8.7
105105

106106
bazel_repo=$build_dir/bazel
107-
bazel_remote=https://github.com/bazelbuild/bazel
108-
bazel_branch=master
109-
bazel_tag=4.1.0
110-
111-
# Advanced option: take a fork that has fixes for Jacoco LCOV formatter, to respect Jacoco filtering
112-
# (fixes for Scala in Jacoco respected in Bazel branch coverage):
113-
#bazel_remote=https://github.com/gergelyfabian/bazel
114-
#bazel_branch=branch_coverage_respect_jacoco_filtering
107+
bazel_remote=https://github.com/gergelyfabian/bazel
108+
if [ "$bazel_major_version" = "5" ]; then
109+
bazel_version=6.0.0-pre.20220520.1
110+
# Version of Bazel with extending Bazel's Jacoco interface implementation for our 0.8.7-scala jacoco branch.
111+
bazel_branch=jacoco_0.8.7_scala
112+
else
113+
bazel_version=6.3.2
114+
# Version of Bazel with extending Bazel's Jacoco interface implementation for our 0.8.7-scala jacoco branch.
115+
bazel_branch=6.3.2_jacoco_0.8.7_scala
116+
fi
115117

116118
bazel_build_target=JacocoCoverage_jarjar_deploy.jar
117119

@@ -147,10 +149,10 @@ mvn clean install
147149
(
148150
cd $bazel_repo
149151
git remote update
150-
git checkout tags/$bazel_tag
152+
git reset --hard HEAD
153+
git checkout $bazel_branch
151154

152-
# Advanced option - check out a branch instead of the release tag
153-
# git checkout origin/$bazel_branch
155+
echo "$bazel_version" > .bazelversion
154156

155157
# Prepare Jacoco version.
156158
cd third_party/java/jacoco

scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh renamed to scripts/build_jacocorunner/build_jacocorunner_bazel_4.0.sh

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,39 @@
44
#
55
# The default `JacocoCoverage_jarjar_deploy.jar` has some issues:
66
#
7-
# 1. Scala support on newer Jacoco versions (including 0.8.7) is still lacking some functionality
7+
# 1. Bazel uses Jacoco 0.8.3 that has poor Scala support, including a bug that filters out Scala lambdas on Scala >=2.12
8+
#
9+
# Bug report:
10+
# https://github.com/bazelbuild/rules_scala/issues/1056
11+
# https://github.com/bazelbuild/bazel/issues/11674
12+
#
13+
# Backported fix from Jacoco 0.8.5 to Jacoco 0.8.3 (current Bazel is not compatible with Jacoco 0.8.5):
14+
# https://github.com/gergelyfabian/jacoco/tree/0.8.3-scala-lambda-fix
15+
#
16+
# 2. Bazel ignores Jacoco's filtering for branch coverage metrics:
17+
#
18+
# Bug report:
19+
# https://github.com/bazelbuild/bazel/issues/12696
20+
#
21+
# Proposed fix:
22+
# https://github.com/gergelyfabian/bazel/tree/branch_coverage_respect_jacoco_filtering
23+
#
24+
# 3. Scala support on newer Jacoco versions (including 0.8.5) is still lacking some functionality
825
#
926
# E.g. a lot of generated methods for case classes, lazy vals or other Scala features are causing falsely missed branches in branch coverage.
1027
#
1128
# Proposed changes in:
1229
# https://github.com/gergelyfabian/jacoco/tree/scala
1330
#
14-
# Backported to 0.8.7 (to be usable with current Bazel):
15-
# https://github.com/gergelyfabian/jacoco/tree/0.8.7-scala
16-
#
17-
# 2. Bazel's code for generating `JacocoCoverage_jarjar_deploy.jar` needs changes after our Jacoco changes
31+
# Backported to 0.8.3 (to be usable with current Bazel):
32+
# https://github.com/gergelyfabian/jacoco/tree/0.8.3-scala
1833
#
19-
# It implements an interface that we have extended, so that implementation also needs to be extended.
20-
#
21-
# This has been added on https://github.com/gergelyfabian/bazel/tree/jacoco_0.8.7_scala.
34+
# These branches also include the Scala 2.12 lambda coverage fix.
2235
#
2336
# You can use this script to build a custom version of `JacocoCoverage_jarjar_deploy.jar`, including any fixes from the above list you wish
2437
# and then provide the built jar as a parameter of `java_toolchain` and/or `scala_toolchain` to use the changed behavior for coverage.
2538
#
26-
# Choose the fixes from the above list by configuring the used branch for Jacoco/Bazel repos below.
39+
# Choose the fixes from the above list by configuring the used branches for Bazel and Jacoco repos below.
2740
#
2841
# Patches:
2942
#
@@ -42,12 +55,16 @@
4255

4356
set -e
4457

45-
# Note!!
46-
# Ensure Java 8 is used for building Jacoco (experienced issue when using e.g. Java 17).
47-
# You may need to change this on your system.
48-
# If this matches your system, you could uncomment these lines:
49-
#export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
50-
#export PATH=$JAVA_HOME/bin:$PATH
58+
59+
60+
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
61+
readlink_cmd="readlink"
62+
elif [[ "$OSTYPE" == "darwin"* ]]; then
63+
readlink_cmd="greadlink"
64+
else
65+
echo "OS not supported: $OSTYPE"
66+
exit 1
67+
fi
5168

5269
JAVA_VERSION=$(java -version 2>&1 | head -1 \
5370
| cut -d'"' -f2 \
@@ -60,15 +77,6 @@ if [ "$JAVA_VERSION" != "8" ]; then
6077
exit 1
6178
fi
6279

63-
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
64-
readlink_cmd="readlink"
65-
elif [[ "$OSTYPE" == "darwin"* ]]; then
66-
readlink_cmd="greadlink"
67-
else
68-
echo "OS not supported: $OSTYPE"
69-
exit 1
70-
fi
71-
7280
source_path=$($readlink_cmd -f $(dirname ${BASH_SOURCE[0]}))
7381

7482
build_dir=/tmp/bazel_jacocorunner_build
@@ -78,25 +86,32 @@ mkdir -p $build_dir
7886
jacoco_repo=$build_dir/jacoco
7987
# Take a fork for Jacoco that contains Scala fixes.
8088
jacoco_remote=https://github.com/gergelyfabian/jacoco
81-
# Take further fixes for Scala (2.11, 2.12 and 2.13) - branch in development:
82-
jacoco_branch=0.8.7-scala
89+
# Choose a branch you'd like to use.
90+
# Default option, take only fixes for Scala 2.12 lambdas backported to Jacoco 0.8.3:
91+
jacoco_branch=0.8.3-scala-lambda-fix
92+
# Advanced option, take further fixes for Scala (2.11, 2.12 and 2.13) - branch in development:
93+
#jacoco_branch=0.8.3-scala
8394

8495
# Choose the patches that you'd like to use:
8596
jacoco_patches=""
8697
# Bazel needs to have a certain Jacoco package version:
87-
jacoco_patches="$jacoco_patches 0001-Build-Jacoco-for-Bazel-5.0+.patch"
98+
jacoco_patches="$jacoco_patches 0001-Build-Jacoco-for-Bazel.patch"
8899
# Uncomment this if you are behind a proxy:
89100
#jacoco_patches="$jacoco_patches 0002-Build-Jacoco-behind-proxy.patch"
90101

91102

92-
# Jacoco version should be 0.8.7 in any case as Bazel is only compatible with that at this moment.
93-
jacoco_version=0.8.7
103+
# Jacoco version should be 0.8.3 in any case as Bazel is only compatible with that at this moment.
104+
jacoco_version=0.8.3
94105

95106
bazel_repo=$build_dir/bazel
96-
bazel_remote=https://github.com/gergelyfabian/bazel
97-
bazel_version=6.0.0-pre.20220520.1
98-
# Version of Bazel with extending Bazel's Jacoco interface implementation for our 0.8.7-scala jacoco branch.
99-
bazel_branch=jacoco_0.8.7_scala
107+
bazel_remote=https://github.com/bazelbuild/bazel
108+
bazel_branch=master
109+
bazel_tag=4.1.0
110+
111+
# Advanced option: take a fork that has fixes for Jacoco LCOV formatter, to respect Jacoco filtering
112+
# (fixes for Scala in Jacoco respected in Bazel branch coverage):
113+
#bazel_remote=https://github.com/gergelyfabian/bazel
114+
#bazel_branch=branch_coverage_respect_jacoco_filtering
100115

101116
bazel_build_target=JacocoCoverage_jarjar_deploy.jar
102117

@@ -132,9 +147,10 @@ mvn clean install
132147
(
133148
cd $bazel_repo
134149
git remote update
135-
git checkout $bazel_branch
150+
git checkout tags/$bazel_tag
136151

137-
echo "$bazel_version" > .bazelversion
152+
# Advanced option - check out a branch instead of the release tag
153+
# git checkout origin/$bazel_branch
138154

139155
# Prepare Jacoco version.
140156
cd third_party/java/jacoco

0 commit comments

Comments
 (0)