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# 
5542
5643set  -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
6952JAVA_VERSION=$( java -version 2>&1  |  head -1 \
7053                                  |  cut -d' "'  
@@ -77,41 +60,60 @@ if [ "$JAVA_VERSION" != "8" ]; then
7760  exit  1
7861fi 
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+ 
8072source_path=$( $readlink_cmd  -f $( dirname ${BASH_SOURCE[0]} ) ) 
8173
8274build_dir=/tmp/bazel_jacocorunner_build
8375
8476mkdir -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+ 
8689jacoco_repo=$build_dir /jacoco
8790#  Take a fork for Jacoco that contains Scala fixes.
8891jacoco_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:
9696jacoco_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
106106bazel_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
116118bazel_build_target=JacocoCoverage_jarjar_deploy.jar
117119
@@ -147,10 +149,10 @@ mvn clean install
147149(
148150cd  $bazel_repo 
149151git 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.
156158cd  third_party/java/jacoco
0 commit comments