rules_scala supports coverage:
bazel coverage //...It will produce several .dat files with results for your targets.
You can also add more options to receive a combined coverage report:
bazel coverage \
--combined_report=lcov \
--coverage_report_generator="@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main" \
//...This should produce a single bazel-out/_coverage/_coverage_report.dat from all coverage files that are generated.
You can install the lcov package (that supports the format Bazel uses for coverage reports) to have access to additional tools:
# Use your system package manager. E.g. on Ubuntu:
sudo apt install lcovHaving lcov package installed you can extract information from your coverage reports:
# For a summary:
lcov --summary your-coverage-report.dat
# For details:
lcov --list your-coverage-report.datIf you prefer an HTML report, then you can use genhtml provided also by the lcov package.
An example with a bit of ceremony:
# Output html reports to a new directory.
destdir="my-coverage-reports"
mkdir -p ${destdir}
# Generate HTML report from the results.
genhtml -o ${destdir} --ignore-errors source bazel-out/_coverage/_coverage_report.dat
echo "coverage report at file://${destdir}/index.html"
Coverage support has been only tested with ScalaTest.
rules_scala uses the JaCoCo library imported
by the underlying rules_java
module to generate code coverage. rules_java, in turn, imports JaCoCo via the
java_tools repository, built from
the tools/jdk/BUILD.java_tools file
and third_party/java/jacoco
package in the Bazel source. java_tools and rules_java are released more
frequently than Bazel itself, decoupling them from the Bazel release cycle.
To check the version of JaCoCo used by a specific version of rules_java:
-
Select a specific version of
rules_javausing the Switch branches/tabs dropdown menu.- Alternatively, replace
masterwith therules_javaversion in thejava/repositories.bzlURL.
- Alternatively, replace
-
Make note of the version of
java_toolsspecified in theJAVA_TOOLS_CONFIGdictionary. For example,rules_java8.9.0 usesjava_toolsv.13.16. -
Download the
java_toolsarchive using the archive URL:curl -LO https://mirror.bazel.build/bazel_java_tools/releases/java/v13.16/java_tools-v13.16.zip
-
Unzip the archive, then inspect the top level
BUILDfile:$ grep 'jacoco\.core-' BUILD jars = ["java_tools/third_party/java/jacoco/org.jacoco.core-0.8.11.jar"], srcjar = "java_tools/third_party/java/jacoco/org.jacoco.core-0.8.11-sources.jar", srcs = ["java_tools/third_party/java/jacoco/org.jacoco.core-0.8.11.jar"],
rules_java version |
java_tools version |
JaCoCo version |
|---|---|---|
| 8.9.0 | v13.16 | 0.8.11 |
| 7.12.4 | v13.9 | 0.8.11 |
For information on updating the JaCoCo version used by rules_java,
java_tools, and Bazel, see Bazel's "Upgrading Jacoco version"
README.
The version of JaCoCo that ships with rules_java may lack support for newer
Scala features. To work around this, build an updated version of JaCoCo and
configure the project to use this new artifact 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).
Then, you can use the jacocorunner property of scala_toolchain to provide the jacocorunner you have built:
# Example contents of coverage_local_jacocorunner/BUILD
scala_toolchain(
name = "local_jacocorunner_toolchain_impl",
jacocorunner = ":local_jacocorunner",
visibility = ["//visibility:public"],
)
toolchain(
name = "local_jacocorunner_scala_toolchain",
toolchain = "local_jacocorunner_toolchain_impl",
toolchain_type = "@rules_scala//scala:toolchain_type",
visibility = ["//visibility:public"],
)
filegroup(
name = "local_jacocorunner",
srcs = ["JacocoCoverage_jarjar_deploy.jar"],
)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
keeping binaries in your repository).
Finally, provide the scala_toolchain in your .bazelrc or as an option to bazel coverage:
coverage --extra_toolchains="//coverage_local_jacocorunner:local_jacocorunner_scala_toolchain"You could also register the toolchain in your WORKSPACE.
You can verify that the locally built jacocorunner works with manual_test/coverage_local_jacocorunner/test.sh.
bazel coverage reports each source's coverage under the on-disk path Bazel
gave it, matched by file name against the classes compiled from the same
target. When a source's file name (without extension) is unique in the target
and matches the outer class name of a class in the target, rules_scala maps
that source to that class's package directory, even when the source's own
directory doesn't mirror it. For example, another/utils/Widget.scala, which
declares package another.utils.nested and class Widget, still gets mapped
to another/utils/nested/Widget.scala.
This mapping is skipped, and coverage falls back to a plain suffix match
instead, whenever the file name leaves more than one pairing equally
plausible: another source in the target shares the same file name, or a
class of that name exists in more than one package. It's also skipped when
no compiled class has that name at all. The suffix match then requires the
source's on-disk directory to end with its package path. See
pathsForCoverageContent in
src/java/io/bazel/rulesscala/coverage/instrumenter/JacocoInstrumenter.java.
JaCoCo's probes add bytecode, so a method that is already close to the JVM's
64KB per-method limit goes over it once instrumented. ASM then throws
MethodTooLargeException, and JacocoInstrumenter fails. In a coverage build,
the target with this class and every target that depends on it then fail to
build.
Set coverage_skip_oversized_methods on scala_toolchain to pass such a class
through to the output jar uninstrumented instead of failing the build:
scala_toolchain(
name = "coverage_skip_oversized_methods_impl",
coverage_skip_oversized_methods = True,
visibility = ["//visibility:public"],
)
toolchain(
name = "coverage_skip_oversized_methods",
toolchain = "coverage_skip_oversized_methods_impl",
toolchain_type = "@rules_scala//scala:toolchain_type",
visibility = ["//visibility:public"],
)coverage --extra_toolchains="//path/to:coverage_skip_oversized_methods"The class is omitted from coverage reports altogether rather than reported as
uncovered, and JacocoInstrumenter prints a warning naming it. The default is
False, which keeps the original behaviour of failing the action.
In a coverage build, Bazel selects targets for instrumentation. For each
selected Scala target with sources, rules_scala instruments the runtime jar.
Dependents compile against the classes before instrumentation, so the Scala
inliner (-opt:inline) can copy methods from them. rules_scala also supplies
these classes when scalac runs macros. The exception is a Scala library that a
macro gets only through a target of another rule, such as java_library or
scala_import: scalac then reads its instrumented classes. To inline from such
a library, remove it from instrumentation with --instrumentation_filter. The
coverage report then omits that library. When the inliner copies a method into
a dependent, the hits of the copy count for the dependent. The original method
gets hits only from the calls that the inliner leaves in place. Scaladoc runs
macros from the instrumented jars.
scala_library_for_plugin_bootstrapping keeps its uninstrumented jar. An
instrumented class calls the JaCoCo runtime when it runs. rules_scala adds this
runtime to scala_binary, scala_repl, the deploy jars and runfiles of
scala_library and scala_macro_library, and its own build tools, such as
scalac for macros that call instrumented Java code.
Some tools load the runtime jar of a Scala target during the build. An example is a code generator that loads its generator classes from a target in the target configuration. Such a tool needs the JaCoCo runtime too. Use one of these fixes:
- Build the jars that the tool loads in the exec configuration
(
cfg = "exec"). For agenrule, put them intools. Bazel turns coverage off in the exec configuration, so these jars keep their uninstrumented classes. - Remove the target from instrumentation.
--instrumentation_filtertakes regular expressions, for example--instrumentation_filter=^//,-^//path/to/generators[/:]. - Add
@bazel_tools//tools/jdk:JacocoCoverageto theruntime_depsof the tool. Add-Djacoco-agent.output=noneto its JVM flags. This flag stops the runtime from writing ajacoco.execfile into the working directory.
When you run a deploy jar from a coverage build with java -jar, add
-Djacoco-agent.output=none.
build_jacocorunner.sh checks the Java version it runs with: Java 17 for 7 and 8, Java 17 or 21 for 9. The script header shows how to point it at a specific JDK.