Skip to content

Update docs for version 7.0.0, remove old APIs #1703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 28, 2025
2 changes: 1 addition & 1 deletion .github/workflows/workspace_snippet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Paste this snippet into your \`WORKSPACE\` file:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_scala",
name = "rules_scala", # Can be "io_bazel_rules_scala" if you still need it.
sha256 = "${SHA}",
strip_prefix = "${PREFIX}",
url = "https://github.com/bazelbuild/rules_scala/releases/download/${TAG}/${ARCHIVE}",
Expand Down
5 changes: 5 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"default": true,
"MD013": false,
"MD033": false
}
730 changes: 587 additions & 143 deletions README.md

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ load("//scala:deps.bzl", "rules_scala_dependencies")

rules_scala_dependencies()

load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains")
load(
"@rules_java//java:repositories.bzl",
"rules_java_dependencies",
"rules_java_toolchains",
)

rules_java_dependencies()

Expand Down Expand Up @@ -51,9 +55,11 @@ load("//scala:toolchains.bzl", "scala_register_toolchains", "scala_toolchains")
scala_toolchains(
fetch_sources = True,
jmh = True,
junit = True,
scala_proto = True,
scalafmt = True,
testing = True,
scalatest = True,
specs2 = True,
twitter_scrooge = True,
)

Expand Down
94 changes: 73 additions & 21 deletions docs/coverage.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
## Coverage support
# Coverage support

### Running tests with coverage
## Running tests with coverage

rules_scala supports coverage:

```
```txt
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:

```
```txt
bazel coverage \
--combined_report=lcov \
--coverage_report_generator="@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main" \
Expand All @@ -21,18 +21,18 @@ bazel coverage \

This should produce a single `bazel-out/_coverage/_coverage_report.dat` from all coverage files that are generated.

### Processing coverage reports
## Processing coverage reports

You can install `lcov` package (that supports the format Bazel uses for coverage reports) to have access to additional tools:
You can install the [`lcov`](https://github.com/linux-test-project/lcov) package (that supports the format Bazel uses for coverage reports) to have access to additional tools:

```
```txt
# Use your system package manager. E.g. on Ubuntu:
sudo apt install lcov
```

Having `lcov` package installed you can extract information from your coverage reports:

```
```txt
# For a summary:
lcov --summary your-coverage-report.dat
# For details:
Expand All @@ -55,26 +55,78 @@ echo "coverage report at file://${destdir}/index.html"

```

### Support for testing frameworks
## Support for testing frameworks

Coverage support has been only tested with [ScalaTest](http://www.scalatest.org/).

### Working around missing lambda coverage with Scala 2.12+
## JaCoCo

The current Jacoco version in Bazel (0.8.3) has missing coverage for lambdas
(including for comprehensions; see issue https://github.com/bazelbuild/rules_scala/issues/1056). Also, the support for
filtering out code generated by the Scala compiler is quite reduced in Jacoco.
`rules_scala` uses the [JaCoCo](https://www.jacoco.org/jacoco/) library imported
by the underlying [`rules_java`](https://github.com/bazelbuild/rules_java)
module to generate code coverage. `rules_java`, in turn, imports JaCoCo via the
[`java_tools`](https://github.com/bazelbuild/java_tools) repository, built from
the [tools/jdk/BUILD.java_tools](
https://github.com/bazelbuild/bazel/blob/master/tools/jdk/BUILD.java_tools) file
and [third_party/java/jacoco](
https://github.com/bazelbuild/bazel/blob/master/third_party/java/jacoco/BUILD)
package in the Bazel source. `java_tools` and `rules_java` are released more
frequently than Bazel itself, decoupling them from the Bazel release cycle.

This can be worked around by building a fixed version of Jacoco yourselves (including backported fixes from 0.8.5) and reconfiguring your
build to use that one instead of the default `jacocorunner`.
To check the version of JaCoCo used by a specific version of `rules_java`:

You can build jacocorunner with a script in `scripts/build_jacocorunner/build_jacocorunner.sh` (see comments there for more explanation and options).
- Open [`java/repositories.bzl` in the `rules_java` source](
https://github.com/bazelbuild/rules_java/blob/master/java/repositories.bzl).

- Select a specific version of `rules_java` using the **Switch branches/tabs**
dropdown menu.

- Alternatively, replace `master` with the `rules_java` version in the
`java/repositories.bzl` URL.

- Make note of the version of `java_tools` specified in the `JAVA_TOOLS_CONFIG`
dictionary. For example, [`rules_java` 8.9.0 uses `java_tools` v.13.16](
https://github.com/bazelbuild/rules_java/blob/8.9.0/java/repositories.bzl#L49).

- Download the `java_tools` archive using the archive URL:

```txt
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 `BUILD` file:

An older version of this script (for Bazel 4) can be found in `scripts/build_jacocorunner/build_jacocorunner_bazel_4.0.sh`.
```sh
$ 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](https://github.com/bazelbuild/rules_java/blob/8.9.0/java/repositories.bzl#L49) | v13.16 | [0.8.11][JaCoCo version] |
| [7.12.4](https://github.com/bazelbuild/rules_java/blob/7.12.4/java/repositories.bzl#L49) | v13.9 | [0.8.11][JaCoCo version] |

For information on updating the [JaCoCo version][] used by `rules_java`,
`java_tools`, and Bazel, see [Bazel's "Upgrading Jacoco version"
README](
https://github.com/bazelbuild/bazel/blob/master/third_party/java/jacoco/README.md).

[JaCoCo version]: https://www.jacoco.org/jacoco/trunk/doc/changes.html

## Working around missing JaCoCo features

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:

```
```py
# Example contents of coverage_local_jacocorunner/BUILD
scala_toolchain(
name = "local_jacocorunner_toolchain_impl",
Expand All @@ -85,7 +137,7 @@ scala_toolchain(
toolchain(
name = "local_jacocorunner_scala_toolchain",
toolchain = "local_jacocorunner_toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//scala:toolchain_type",
toolchain_type = "@rules_scala//scala:toolchain_type",
visibility = ["//visibility:public"],
)

Expand All @@ -100,15 +152,15 @@ keeping binaries in your repository).

Finally, provide the `scala_toolchain` in your `.bazelrc` or as an option to `bazel coverage`:

```
```txt
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`.

#### Notes
## Notes

Please ensure these scripts use Java 8.

Expand Down
Loading