Skip to content

Commit 21fb78d

Browse files
committed
Update docs for rules_scala 7.0.0
Describes the new `scala_toolchains()` API, breaking changes, Bazel compatibility matrix, and breaking of the `io_bazel_rules_scala` repo name dependency. Part of bazelbuild#1482, bazelbuild#1647, bazelbuild#1652, and bazelbuild#1699. Much of the README text regarding `WORKSPACE` configuration, Bazel compatibility, and breaking changes comes from: - bazelbuild#1482 (comment) Contains information about the upcoming Bzlmod implementation and Bazel 8 compatibility changes. With this information already in place, very little will need to change when these changes land. Those doc updates will land in the same commit as their respective implementations. Contains a light editing pass over almost every Markdown file to resolve `markdownlint` warnings when editing in VSCode. Also added `.markdownlint.json` to silence rule `MD033/no-inline-html`, since several docs contain `<br/>` and/or `<table>` elements. - https://github.com./DavidAnson/vscode-markdownlint?tab=readme-ov-file#markdownlintconfig Performed other opportunistic grammar edits and added new information, particularly to: - `docs/coverage.md`: Describes how to determine the default JaCoCo version used by `rules_java` - `scripts/README.md`: Updates `create_repository.py` docs extensively, and adds a section for `sync_bazelversion.sh`
1 parent 62a3117 commit 21fb78d

26 files changed

+1121
-600
lines changed

.markdownlint.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"default": true,
3+
"MD013": false,
4+
"MD033": false
5+
}

README.md

+437-132
Large diffs are not rendered by default.

docs/coverage.md

+73-21
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
## Coverage support
1+
# Coverage support
22

3-
### Running tests with coverage
3+
## Running tests with coverage
44

55
rules_scala supports coverage:
66

7-
```
7+
```txt
88
bazel coverage //...
99
```
1010

1111
It will produce several .dat files with results for your targets.
1212

1313
You can also add more options to receive a combined coverage report:
1414

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

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

24-
### Processing coverage reports
24+
## Processing coverage reports
2525

26-
You can install `lcov` package (that supports the format Bazel uses for coverage reports) to have access to additional tools:
26+
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:
2727

28-
```
28+
```txt
2929
# Use your system package manager. E.g. on Ubuntu:
3030
sudo apt install lcov
3131
```
3232

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

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

5656
```
5757

58-
### Support for testing frameworks
58+
## Support for testing frameworks
5959

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

62-
### Working around missing lambda coverage with Scala 2.12+
62+
## JaCoCo
6363

64-
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). Also, the support for
66-
filtering out code generated by the Scala compiler is quite reduced in Jacoco.
64+
`rules_scala` uses the [JaCoCo](https://www.jacoco.org/jacoco/) library imported
65+
by the underlying [`rules_java`](https://github.com./bazelbuild/rules_java)
66+
module to generate code coverage. `rules_java`, in turn, imports JaCoCo via the
67+
[`java_tools`](https://github.com./bazelbuild/java_tools) repository, built from
68+
the [tools/jdk/BUILD.java_tools](
69+
https://github.com./bazelbuild/bazel/blob/master/tools/jdk/BUILD.java_tools) file
70+
and [third_party/java/jacoco](
71+
https://github.com./bazelbuild/bazel/blob/master/third_party/java/jacoco/BUILD)
72+
package in the Bazel source. `java_tools` and `rules_java` are released more
73+
frequently than Bazel itself, decoupling them from the Bazel release cycle.
6774

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

71-
You can build jacocorunner with a script in `scripts/build_jacocorunner/build_jacocorunner.sh` (see comments there for more explanation and options).
77+
- Open [`java/repositories.bzl` in the `rules_java` source](
78+
https://github.com./bazelbuild/rules_java/blob/master/java/repositories.bzl).
79+
80+
- Select a specific version of `rules_java` using the **Switch branches/tabs**
81+
dropdown menu.
82+
83+
- Alternatively, replace `master` with the `rules_java` version in the
84+
`java/repositories.bzl` URL.
85+
86+
- Make note of the version of `java_tools` specified in the `JAVA_TOOLS_CONFIG`
87+
dictionary. For example, [`rules_java` 8.9.0 uses `java_tools` v.13.16](
88+
https://github.com./bazelbuild/rules_java/blob/8.9.0/java/repositories.bzl#L49).
89+
90+
- Download the `java_tools` archive using the archive URL:
91+
92+
```txt
93+
curl -LO https://mirror.bazel.build/bazel_java_tools/releases/java/v13.16/java_tools-v13.16.zip
94+
```
95+
96+
- Unzip the archive, then inspect the top level `BUILD` file:
7297
73-
An older version of this script (for Bazel 4) can be found in `scripts/build_jacocorunner/build_jacocorunner_bazel_4.0.sh`.
98+
```sh
99+
$ grep 'jacoco\.core-' BUILD
100+
101+
jars = ["java_tools/third_party/java/jacoco/org.jacoco.core-0.8.11.jar"],
102+
srcjar = "java_tools/third_party/java/jacoco/org.jacoco.core-0.8.11-sources.jar",
103+
srcs = ["java_tools/third_party/java/jacoco/org.jacoco.core-0.8.11.jar"],
104+
```
105+
106+
| `rules_java` version | `java_tools` version | JaCoCo version |
107+
| :-: | :-: | :-: |
108+
| [8.9.0](https://github.com./bazelbuild/rules_java/blob/8.9.0/java/repositories.bzl#L49) | v13.16 | [0.8.11][JaCoCo version] |
109+
| [7.12.4](https://github.com./bazelbuild/rules_java/blob/7.12.4/java/repositories.bzl#L49) | v13.9 | [0.8.11][JaCoCo version] |
110+
111+
For information on updating the [JaCoCo version][] used by `rules_java`,
112+
`java_tools`, and Bazel, see [Bazel's &quot;Upgrading Jacoco version&quot;
113+
README](
114+
https://github.com./bazelbuild/bazel/blob/master/third_party/java/jacoco/README.md).
115+
116+
[JaCoCo version]: https://www.jacoco.org/jacoco/trunk/doc/changes.html
117+
118+
## Working around missing JaCoCo features
119+
120+
The version of JaCoCo that ships with `rules_java` may lack support for newer
121+
Scala features. To work around this, build an updated version of JaCoCo and
122+
configure the project to use this new artifact instead of the default
123+
`jacocorunner`.
124+
125+
You can build jacocorunner with a script in `scripts/build_jacocorunner/build_jacocorunner.sh` (see comments there for more explanation and options).
74126
75127
Then, you can use the `jacocorunner` property of `scala_toolchain` to provide the jacocorunner you have built:
76128
77-
```
129+
```py
78130
# Example contents of coverage_local_jacocorunner/BUILD
79131
scala_toolchain(
80132
name = "local_jacocorunner_toolchain_impl",
@@ -85,7 +137,7 @@ scala_toolchain(
85137
toolchain(
86138
name = "local_jacocorunner_scala_toolchain",
87139
toolchain = "local_jacocorunner_toolchain_impl",
88-
toolchain_type = "@io_bazel_rules_scala//scala:toolchain_type",
140+
toolchain_type = "@rules_scala//scala:toolchain_type",
89141
visibility = ["//visibility:public"],
90142
)
91143
@@ -100,15 +152,15 @@ keeping binaries in your repository).
100152

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

103-
```
155+
```txt
104156
coverage --extra_toolchains="//coverage_local_jacocorunner:local_jacocorunner_scala_toolchain"
105157
```
106158

107159
You could also register the toolchain in your `WORKSPACE`.
108160

109161
You can verify that the locally built `jacocorunner` works with `manual_test/coverage_local_jacocorunner/test.sh`.
110162

111-
#### Notes
163+
## Notes
112164

113165
Please ensure these scripts use Java 8.
114166

docs/cross-compilation.md

+63-33
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@ Read *Quick start* for an information on how to use cross compilation.
44
The remaining sections contain more detailed information, useful especially for toolchain & rule developers.
55

66
## Quick start
7+
78
`scala_config` repository rule accepts two parameters related to Scala version:
8-
* `scala_version` – a single, default version;
9-
* `scala_versions` – a list of versions to make available for use.
9+
10+
- `scala_version` – a single, default version;
11+
- `scala_versions` – a list of versions to make available for use.
1012

1113
The first one, `scala_version`, will be used as a default, but it can be overridden for specific targets for any version from the `scala_versions`.
1214

1315
Multiple rules, such as:
16+
1417
- [scala_library](/scala/private/rules/scala_library.bzl)
1518
- [scala_binary](/scala/private/rules/scala_binary.bzl)
1619
- [scala_repl](/scala/private/rules/scala_repl.bzl)
1720
- [scala_test](/scala/private/rules/scala_test.bzl)
1821

1922
support such override via the `scala_version` attribute, e.g.:
20-
```starlark
23+
24+
```py
2125
scala_library(
2226
name = ...
2327
...
@@ -32,16 +36,19 @@ For this library and all its dependencies 2.12.18 compiler will be used, unless
3236

3337
`scala_config` creates the repository `@io_bazel_rules_scala_config`.
3438
File created there, `config.bzl`, consists of many variables. In particular:
35-
* `SCALA_VERSION` – representing the default Scala version, e.g. `"3.3.1"`;
36-
* `SCALA_VERSIONS` – representing all configured Scala versions, e.g. `["2.12.18", "3.3.1"]`.
3739

40+
- `SCALA_VERSION` – representing the default Scala version, e.g. `"3.3.1"`;
41+
- `SCALA_VERSIONS` – representing all configured Scala versions, e.g. `["2.12.18", "3.3.1"]`.
3842

3943
## Build settings
44+
4045
Configured `SCALA_VERSIONS` correspond to allowed values of [build setting](https://bazel.build/extending/config#user-defined-build-setting).
4146

4247
### `scala_version`
48+
4349
`@io_bazel_rules_scala_config` in its root package defines the following build setting:
44-
```starlark
50+
51+
```py
4552
string_setting(
4653
name = "scala_version",
4754
build_setting_default = "3.3.1",
@@ -50,45 +57,56 @@ string_setting(
5057
)
5158
...
5259
```
60+
5361
This build setting can be subject of change by [transitions](https://bazel.build/extending/config#user-defined-transitions) (within allowed `values`).
5462

5563
### Config settings
64+
5665
Then for each Scala version we have a [config setting](https://bazel.build/extending/config#build-settings-and-select):
57-
```starlark
66+
67+
```py
5868
config_setting(
5969
name = "scala_version_3_3_1",
6070
flag_values = {":scala_version": "3.3.1"},
6171
)
6272
...
6373
```
74+
6475
The `name` of `config_setting` corresponds to `"scala_version" + version_suffix(scala_version)`.
6576
One may use this config setting in `select()` e.g. to provide dependencies relevant to a currently used Scala version.
6677

67-
6878
## Version-dependent behavior
79+
6980
Don't rely on `SCALA_VERSION` as it represents the default Scala version, not necessarily the one that is currently requested.
7081

7182
If you need to customize the behavior for specific Scala version, there are two scenarios.
7283

7384
### From toolchain
74-
If you have an access to the Scala toolchain (`@io_bazel_rules_scala//scala:toolchain_type`), there is `scala_version` field provided in there:
75-
```starlark
85+
86+
`@rules_scala//scala:toolchain_type` provides the `scala_version` field:
87+
88+
```py
7689
def _rule_impl(ctx):
7790
...
78-
ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_version
91+
ctx.toolchains["@rules_scala//scala:toolchain_type"].scala_version
7992
...
8093
```
8194

8295
### From config setting
96+
8397
In BUILD files, you need to use the config settings with `select()`.
8498
Majority of use cases is covered by the `select_for_scala_version` utility macro.
8599
If more flexibility is needed, you can always write the select manually.
86100

87101
#### With select macro
102+
88103
See example usage of the `select_for_scala_version`:
89104

90-
```starlark
91-
load("@io_bazel_rules_scala//:scala_cross_version_select.bzl", "select_for_scala_version")
105+
```py
106+
load(
107+
"@rules_scala//:scala_cross_version_select.bzl",
108+
"select_for_scala_version",
109+
)
92110

93111
scala_library(
94112
...
@@ -113,43 +131,49 @@ scala_library(
113131
See complete documentation in the [scala_cross_version_select.bzl](/scala/scala_cross_version_select.bzl) file
114132

115133
#### Manually
134+
116135
An example usage of `select()` to provide custom dependency for specific Scala version:
117-
```starlark
136+
137+
```py
118138
deps = select({
119139
"@io_bazel_rules_scala_config//:scala_version_3_3_1": [...],
120140
...
121141
})
122142
```
123143

124144
For more complex logic, you can extract it to a `.bzl` file:
125-
```starlark
145+
146+
```py
126147
def srcs(scala_version):
127148
if scala_version.startswith("2"):
128149
...
129150
...
130151
```
152+
131153
and then in the `BUILD` file:
132-
```starlark
133-
load("@io_bazel_rules_scala//:scala_cross_version.bzl", "version_suffix")
134-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")
154+
155+
```py
135156
load("....bzl", "srcs")
157+
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")
158+
load("@rules_scala//:scala_cross_version.bzl", "version_suffix")
136159

137160
scala_library(
138161
...
139162
srcs = select({
140163
"@io_bazel_rules_scala_config//:scala_version" + version_suffix(v): srcs(v)
141164
for v in SCALA_VERSIONS
142-
}),
165+
}),
143166
...
144167
)
145168
```
146169

147-
148170
## Requesting specific version
171+
149172
To use other than default version of Scala, you need to change the current `@io_bazel_rules_scala_config//:scala_version` build setting.
150173

151174
Simple transition, setting the Scala version to one found in `scala_version` attribute:
152-
```starlark
175+
176+
```py
153177
def _scala_version_transition_impl(settings, attr):
154178
if attr.scala_version:
155179
return {"@io_bazel_rules_scala_config//:scala_version": attr.scala_version}
@@ -164,8 +188,13 @@ scala_version_transition = transition(
164188
```
165189

166190
To use it in a rule, use the `scala_version_transition` as `cfg` and use `toolchain_transition_attr` in `attrs`:
167-
```starlark
168-
load("@io_bazel_rules_scala//scala:scala_cross_version.bzl", "scala_version_transition", "toolchain_transition_attr")
191+
192+
```py
193+
load(
194+
"@rules_scala//scala:scala_cross_version.bzl",
195+
"scala_version_transition",
196+
"toolchain_transition_attr",
197+
)
169198

170199
_scala_library_attrs.update(toolchain_transition_attr)
171200

@@ -183,13 +212,13 @@ def make_scala_library(*extras):
183212
)
184213
```
185214

186-
187215
## Toolchains
216+
188217
Standard [toolchain resolution](https://bazel.build/extending/toolchains#toolchain-resolution) procedure determines which toolchain to use for Scala targets.
189218

190219
Toolchain should declare its compatibility with Scala version by using `target_settings` attribute of the `toolchain` rule:
191220

192-
```starlark
221+
```py
193222
toolchain(
194223
...
195224
target_settings = ["@io_bazel_rules_scala_config//:scala_version_3_3_1"],
@@ -198,16 +227,17 @@ toolchain(
198227
```
199228

200229
### Cross-build support tiers
230+
201231
`rules_scala` consists of many toolchains implementing various toolchain types.
202232
Their support level for cross-build setup varies.
203233

204234
We can distinguish following tiers:
205235

206-
* No `target_settings` set – not migrated, will work on the default `SCALA_VERSION`; undefined behavior on other versions.
207-
* (all toolchains not mentioned elsewhere)
208-
* `target_settings` set to the `SCALA_VERSION` – not fully migrated; will work only on the default `SCALA_VERSION` and will fail the toolchain resolution on other versions.
209-
* (no development in progress)
210-
* Multiple toolchain instances with `target_settings` corresponding to each of `SCALA_VERSIONS` – fully migrated; will work in cross-build setup.
211-
* [the main Scala toolchain](/scala/BUILD)
212-
* [Scalafmt](/scala/scalafmt/BUILD)
213-
* [Scalatest](/testing/testing.bzl)
236+
- No `target_settings` set – not migrated, will work on the default `SCALA_VERSION`; undefined behavior on other versions.
237+
- (all toolchains not mentioned elsewhere)
238+
- `target_settings` set to the `SCALA_VERSION` – not fully migrated; will work only on the default `SCALA_VERSION` and will fail the toolchain resolution on other versions.
239+
- (no development in progress)
240+
- Multiple toolchain instances with `target_settings` corresponding to each of `SCALA_VERSIONS` – fully migrated; will work in cross-build setup.
241+
- [the main Scala toolchain](/scala/BUILD)
242+
- [Scalafmt](/scala/scalafmt/BUILD)
243+
- [Scalatest](/testing/testing.bzl)

0 commit comments

Comments
 (0)