Skip to content

Commit 068122a

Browse files
Fixing JacocoRunner building script for Jacoco (#1485)
- Latest Bazel uses Jacoco 0.8.7 - Ensure that for building JacocoRunner we use a compatible Bazel version - Created Bazel branch that adds interface extensions (interfaceNames) to Bazel's Jacoco implementation - Java 8 needs to be used for building Jacoco Updated documentation. Updated manual test to build for Bazel 5.0+. Updated expected coverage for manual test.
1 parent 6c1f034 commit 068122a

File tree

5 files changed

+80
-39
lines changed

5 files changed

+80
-39
lines changed

docs/coverage.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ Coverage support has been only tested with [ScalaTest](http://www.scalatest.org/
6262
### Working around missing lambda coverage with Scala 2.12+
6363

6464
The current Jacoco version in Bazel (0.8.3) has missing coverage for lambdas
65-
(including for comprehensions; see issue https://github.com/bazelbuild/rules_scala/issues/1056).
65+
(including for comprehensions; see issue https://github.com/bazelbuild/rules_scala/issues/1056). Also, the support for
66+
filtering out code generated by the Scala compiler is quite reduced in Jacoco.
67+
6668
This can be worked around by building a fixed version of Jacoco yourselves (including backported fixes from 0.8.5) and reconfiguring your
6769
build to use that one instead of the default `jacocorunner`.
6870

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

73+
A new version of this script (for Bazel 5.0+) can be found in `scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh`.
74+
7175
Then, you can use the `jacocorunner` property of `scala_toolchain` to provide the jacocorunner you have built:
7276

7377
```
@@ -94,10 +98,19 @@ filegroup(
9498
In this example `jacocorunner` is provided as a local file, but you could also upload your version to an artifactory and then use `http_file` (to avoid
9599
keeping binaries in your repository).
96100

97-
Finally provide the `scala_toolchain` in your `.bazelrc` or as an option to `bazel coverage`:
101+
Finally, provide the `scala_toolchain` in your `.bazelrc` or as an option to `bazel coverage`:
98102

99103
```
100104
coverage --extra_toolchains="//coverage_local_jacocorunner:local_jacocorunner_scala_toolchain"
101105
```
102106

107+
You could also register the toolchain in your `WORKSPACE`.
108+
103109
You can verify that the locally built `jacocorunner` works with `manual_test/coverage_local_jacocorunner/test.sh`.
110+
111+
#### Notes
112+
113+
Please ensure these scripts use Java 8.
114+
115+
This should be done in the script itself, as e.g. the manual test requires a higher Java version, so you could add some
116+
code at the header of the build script to select Java 8 (appropriate for your Java installation).

manual_test/coverage_local_jacocorunner/expected-coverage.dat

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ SF:test/coverage_scalatest/A1.scala
22
FN:-1,coverage_scalatest/A1$::<clinit> ()V
33
FN:3,coverage_scalatest/A1$::<init> ()V
44
FN:5,coverage_scalatest/A1$::a1 (Z)Lcoverage_scalatest/B1$;
5-
FN:-1,coverage_scalatest/A1::a1 (Z)Lcoverage_scalatest/B1$;
65
FNDA:1,coverage_scalatest/A1$::<clinit> ()V
76
FNDA:1,coverage_scalatest/A1$::<init> ()V
87
FNDA:1,coverage_scalatest/A1$::a1 (Z)Lcoverage_scalatest/B1$;
9-
FNDA:0,coverage_scalatest/A1::a1 (Z)Lcoverage_scalatest/B1$;
10-
FNF:4
8+
FNF:3
119
FNH:3
12-
BRDA:5,0,0,-
13-
BRF:1
14-
BRH:0
10+
BRDA:5,0,0,0
11+
BRDA:5,0,1,1
12+
BRF:2
13+
BRH:1
1514
DA:3,1
1615
DA:5,1
1716
DA:6,1
@@ -23,12 +22,10 @@ SF:test/coverage_scalatest/A2.scala
2322
FN:-1,coverage_scalatest/A2$::<clinit> ()V
2423
FN:3,coverage_scalatest/A2$::<init> ()V
2524
FN:5,coverage_scalatest/A2$::a2 ()V
26-
FN:-1,coverage_scalatest/A2::a2 ()V
2725
FNDA:1,coverage_scalatest/A2$::<clinit> ()V
2826
FNDA:1,coverage_scalatest/A2$::<init> ()V
2927
FNDA:1,coverage_scalatest/A2$::a2 ()V
30-
FNDA:0,coverage_scalatest/A2::a2 ()V
31-
FNF:4
28+
FNF:3
3229
FNH:3
3330
DA:3,1
3431
DA:5,1
@@ -40,12 +37,10 @@ SF:test/coverage_scalatest/B1.scala
4037
FN:-1,coverage_scalatest/B1$::<clinit> ()V
4138
FN:3,coverage_scalatest/B1$::<init> ()V
4239
FN:6,coverage_scalatest/B1$::not_called ()V
43-
FN:-1,coverage_scalatest/B1::not_called ()V
4440
FNDA:1,coverage_scalatest/B1$::<clinit> ()V
4541
FNDA:1,coverage_scalatest/B1$::<init> ()V
4642
FNDA:0,coverage_scalatest/B1$::not_called ()V
47-
FNDA:0,coverage_scalatest/B1::not_called ()V
48-
FNF:4
43+
FNF:3
4944
FNH:2
5045
DA:3,1
5146
DA:6,0
@@ -54,32 +49,30 @@ LH:2
5449
LF:3
5550
end_of_record
5651
SF:test/coverage_scalatest/B2.java
57-
FN:3,coverage_scalatest/B2::<init> ()V
58-
FN:5,coverage_scalatest/B2::b2_a ()Ljava/lang/String;
59-
FN:9,coverage_scalatest/B2::b2_b ()V
52+
FN:4,coverage_scalatest/B2::<init> ()V
53+
FN:6,coverage_scalatest/B2::b2_a ()Ljava/lang/String;
54+
FN:10,coverage_scalatest/B2::b2_b ()V
6055
FNDA:0,coverage_scalatest/B2::<init> ()V
6156
FNDA:1,coverage_scalatest/B2::b2_a ()Ljava/lang/String;
6257
FNDA:0,coverage_scalatest/B2::b2_b ()V
6358
FNF:3
6459
FNH:1
65-
DA:3,0
66-
DA:5,1
67-
DA:9,0
60+
DA:4,0
61+
DA:6,1
6862
DA:10,0
63+
DA:11,0
6964
LH:1
7065
LF:4
7166
end_of_record
7267
SF:test/coverage_scalatest/C2.scala
7368
FN:-1,coverage_scalatest/C2$::<clinit> ()V
7469
FN:3,coverage_scalatest/C2$::<init> ()V
7570
FN:5,coverage_scalatest/C2$::c2 (Ljava/lang/String;)Ljava/lang/String;
76-
FN:-1,coverage_scalatest/C2::c2 (Ljava/lang/String;)Ljava/lang/String;
7771
FNDA:1,coverage_scalatest/C2$::<clinit> ()V
7872
FNDA:1,coverage_scalatest/C2$::<init> ()V
7973
FNDA:1,coverage_scalatest/C2$::c2 (Ljava/lang/String;)Ljava/lang/String;
80-
FNDA:1,coverage_scalatest/C2::c2 (Ljava/lang/String;)Ljava/lang/String;
81-
FNF:4
82-
FNH:4
74+
FNF:3
75+
FNH:3
8376
DA:3,1
8477
DA:5,1
8578
DA:7,1
@@ -119,7 +112,6 @@ FN:18,coverage_scalatest/D1$::$anonfun$veryLongFunctionNameIsHereAaaaaaaaa$9 (Ls
119112
FN:-1,coverage_scalatest/D1$::<clinit> ()V
120113
FN:3,coverage_scalatest/D1$::<init> ()V
121114
FN:5,coverage_scalatest/D1$::veryLongFunctionNameIsHereAaaaaaaaa ()V
122-
FN:-1,coverage_scalatest/D1::veryLongFunctionNameIsHereAaaaaaaaa ()V
123115
FNDA:1,coverage_scalatest/D1$::$anonfun$veryLongFunctionNameIsHereAaaaaaaaa$1 (Lscala/collection/immutable/List;Ljava/lang/String;)Lscala/collection/immutable/List;
124116
FNDA:1,coverage_scalatest/D1$::$anonfun$veryLongFunctionNameIsHereAaaaaaaaa$10 (Lscala/collection/immutable/List;Ljava/lang/String;)Lscala/collection/immutable/List;
125117
FNDA:1,coverage_scalatest/D1$::$anonfun$veryLongFunctionNameIsHereAaaaaaaaa$11 (Lscala/collection/immutable/List;Ljava/lang/String;)Lscala/collection/immutable/List;
@@ -152,8 +144,7 @@ FNDA:1,coverage_scalatest/D1$::$anonfun$veryLongFunctionNameIsHereAaaaaaaaa$9 (L
152144
FNDA:1,coverage_scalatest/D1$::<clinit> ()V
153145
FNDA:1,coverage_scalatest/D1$::<init> ()V
154146
FNDA:1,coverage_scalatest/D1$::veryLongFunctionNameIsHereAaaaaaaaa ()V
155-
FNDA:0,coverage_scalatest/D1::veryLongFunctionNameIsHereAaaaaaaaa ()V
156-
FNF:33
147+
FNF:32
157148
FNH:32
158149
DA:3,1
159150
DA:5,1

manual_test/coverage_local_jacocorunner/test.sh

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

2626
build_local_jacocorunner() {
27-
$root_dir/scripts/build_jacocorunner/build_jacocorunner.sh
27+
$root_dir/scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh
2828
cp /tmp/bazel_jacocorunner_build/JacocoCoverage_jarjar_deploy.jar $root_dir/manual_test/coverage_local_jacocorunner
2929
}
3030

scripts/build_jacocorunner/build_jacocorunner.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ else
6666
exit 1
6767
fi
6868

69+
JAVA_VERSION=$(java -version 2>&1 | head -1 \
70+
| cut -d'"' -f2 \
71+
| sed 's/^1\.//' \
72+
| cut -d'.' -f1)
73+
74+
if [ "$JAVA_VERSION" != "8" ]; then
75+
echo "Unexpected java version: $JAVA_VERSION"
76+
echo "Please ensure this script is run with Java 8"
77+
exit 1
78+
fi
79+
6980
source_path=$($readlink_cmd -f $(dirname ${BASH_SOURCE[0]}))
7081

7182
build_dir=/tmp/bazel_jacocorunner_build

scripts/build_jacocorunner/build_jacocorunner_bazel_5.0+.sh

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@
44
#
55
# The default `JacocoCoverage_jarjar_deploy.jar` has some issues:
66
#
7-
# 1. Scala support on newer Jacoco versions (including 0.8.6) is still lacking some functionality
7+
# 1. Scala support on newer Jacoco versions (including 0.8.7) is still lacking some functionality
88
#
99
# E.g. a lot of generated methods for case classes, lazy vals or other Scala features are causing falsely missed branches in branch coverage.
1010
#
1111
# Proposed changes in:
1212
# https://github.com/gergelyfabian/jacoco/tree/scala
1313
#
14-
# Backported to 0.8.6 (to be usable with current Bazel):
15-
# https://github.com/gergelyfabian/jacoco/tree/0.8.6-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
18+
#
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.
1622
#
1723
# You can use this script to build a custom version of `JacocoCoverage_jarjar_deploy.jar`, including any fixes from the above list you wish
1824
# and then provide the built jar as a parameter of `java_toolchain` and/or `scala_toolchain` to use the changed behavior for coverage.
1925
#
20-
# Choose the fixes from the above list by configuring the used branch for Jacoco repo below.
26+
# Choose the fixes from the above list by configuring the used branch for Jacoco/Bazel repos below.
2127
#
2228
# Patches:
2329
#
@@ -36,7 +42,23 @@
3642

3743
set -e
3844

39-
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
51+
52+
JAVA_VERSION=$(java -version 2>&1 | head -1 \
53+
| cut -d'"' -f2 \
54+
| sed 's/^1\.//' \
55+
| cut -d'.' -f1)
56+
57+
if [ "$JAVA_VERSION" != "8" ]; then
58+
echo "Unexpected java version: $JAVA_VERSION"
59+
echo "Please ensure this script is run with Java 8"
60+
exit 1
61+
fi
4062

4163
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
4264
readlink_cmd="readlink"
@@ -57,7 +79,7 @@ jacoco_repo=$build_dir/jacoco
5779
# Take a fork for Jacoco that contains Scala fixes.
5880
jacoco_remote=https://github.com/gergelyfabian/jacoco
5981
# Take further fixes for Scala (2.11, 2.12 and 2.13) - branch in development:
60-
jacoco_branch=0.8.6-scala
82+
jacoco_branch=0.8.7-scala
6183

6284
# Choose the patches that you'd like to use:
6385
jacoco_patches=""
@@ -67,12 +89,14 @@ jacoco_patches="$jacoco_patches 0001-Build-Jacoco-for-Bazel-5.0+.patch"
6789
#jacoco_patches="$jacoco_patches 0002-Build-Jacoco-behind-proxy.patch"
6890

6991

70-
# Jacoco version should be 0.8.6 in any case as Bazel is only compatible with that at this moment.
71-
jacoco_version=0.8.6
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
7294

7395
bazel_repo=$build_dir/bazel
74-
bazel_remote=https://github.com/bazelbuild/bazel
75-
bazel_tag=6.0.0-pre.20220520.1
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
76100

77101
bazel_build_target=JacocoCoverage_jarjar_deploy.jar
78102

@@ -108,7 +132,9 @@ mvn clean install
108132
(
109133
cd $bazel_repo
110134
git remote update
111-
git checkout tags/$bazel_tag
135+
git checkout $bazel_branch
136+
137+
echo "$bazel_version" > .bazelversion
112138

113139
# Prepare Jacoco version.
114140
cd third_party/java/jacoco

0 commit comments

Comments
 (0)