Skip to content

Commit be73da7

Browse files
committed
Add Bzlmod repo scope change, scala_toolchain
Explicitly notes that some `rules_scala` APIs may break, specifically `setup_scala_toolchain`. Part of bazel-contrib#1482. Brought to my attention by @michalbogacz in a `#scala` thread in the Bazel Slack workspace, which was in the context of michalbogacz/scala-bazel-monorepo#26. - https://bazelbuild.slack.com/archives/CDCKJ2KFZ/p1740144886159909
1 parent b294878 commit be73da7

File tree

2 files changed

+153
-46
lines changed

2 files changed

+153
-46
lines changed

README.md

+70
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,76 @@ now, as there's not yet a corresponding [`toolchain_type()`](
554554
https://bazel.build/versions/6.1.0/reference/be/platform#toolchain_type) target
555555
in `@rules_java`.
556556

557+
### Builtin repositories no longer visible without `use_repo()` under Bzlmod
558+
559+
Under Bzlmod, repos are only visible to the module extension that creates them,
560+
unless the `MODULE.bazel` file brings them into scope with
561+
[`use_repo()`](https://bazel.build/rules/lib/globals/module#use_repo). This can
562+
lead to errors like those from the following example, which [originally called
563+
`setup_scala_toolchain()` under Bzlmod](
564+
https://github.com/michalbogacz/scala-bazel-monorepo/blob/17f0890a4345529e09b9ce83bcb2e3d15687c522/BUILD.bazel):
565+
566+
```py
567+
load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_toolchain")
568+
569+
setup_scala_toolchain(
570+
name = "custom_scala_toolchain",
571+
scalacopts = [
572+
"-Wunused:all",
573+
],
574+
strict_deps_mode = "error",
575+
unused_dependency_checker_mode = "warn",
576+
)
577+
```
578+
579+
This worked under `WORKSPACE`, but broke under Bzlmod, the error message
580+
indicating that the builtin `@org_scala_sbt_compiler_interface` toolchain jar
581+
isn't visible:
582+
583+
```txt
584+
ERROR: no such package
585+
'@@[unknown repo 'org_scala_sbt_compiler_interface_3_3_5'
586+
requested from @@]//':
587+
The repository '@@[unknown repo 'org_scala_sbt_compiler_interface_3_3_5'
588+
requested from @@]' could not be resolved:
589+
No repository visible as '@org_scala_sbt_compiler_interface_3_3_5'
590+
```
591+
592+
`setup_scala_toolchains` is a macro that can take user specified classpath
593+
targets as described in [docs/scala_toolchain.md](./docs/scala_toolchain.md).
594+
Otherwise, it _generates new classpath targets_ using the builtin `rules_scala`
595+
repositories, but these repositories are no longer in the global scope, causing
596+
the breakage. (A big part of the Bzlmodification work involved enabling
597+
`rules_scala` to generate and register toolchains _without_ forcing users to
598+
bring their dependencies into scope.)
599+
600+
One way to fix this specific problem is to call `use_repo` for every such
601+
repository needed by the toolchain. Another fix, in this case, is to [use
602+
`scala_toolchain` directly instead](
603+
https://github.com/michalbogacz/scala-bazel-monorepo/blob/2cac860f386dcaa1c3be56cd25a84b247d335743/BUILD.bazel).
604+
Its underlying `BUILD` rule uses the builtin toolchain dependencies via existing
605+
targets visible within `rules_scala`, without forcing users to import them:
606+
607+
```py
608+
load("@rules_scala//scala:scala_toolchain.bzl", "scala_toolchain")
609+
610+
scala_toolchain(
611+
name = "custom_scala_toolchain_impl",
612+
scalacopts = [
613+
"-Ywarn-unused",
614+
],
615+
strict_deps_mode = "error",
616+
unused_dependency_checker_mode = "warn",
617+
)
618+
619+
toolchain(
620+
name = "custom_scala_toolchain",
621+
toolchain = ":custom_scala_toolchain_impl",
622+
toolchain_type = "@rules_scala//scala:toolchain_type",
623+
visibility = ["//visibility:public"],
624+
)
625+
```
626+
557627
## Breaking changes coming in `rules_scala` 8.x
558628

559629
__The main objective of 8.x will be to enable existing users to migrate to Bazel

docs/scala_toolchain.md

+83-46
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# scala_toolchain
22

3-
`scala_toolchain` allows you to define global configuration to all Scala targets.
3+
`scala_toolchain` allows you to define the global configuration for all Scala
4+
targets.
45

5-
**Some scala_toolchain must be registered!**
6+
**Some `scala_toolchain` must be registered!**
67

7-
## Several options to configure `scala_toolchain`
8+
## Options to configure `scala_toolchain`
89

910
### A) Use the builtin Scala toolchain via `scala_toolchains`
1011

11-
In your workspace file add the following lines:
12+
Add the following lines to `WORKSPACE`:
1213

1314
```py
1415
# WORKSPACE
@@ -26,48 +27,84 @@ scala_register_toolchains()
2627

2728
### B) Defining your own `scala_toolchain` requires 2 steps
2829

29-
1. Add your own definition of `scala_toolchain` to a `BUILD` file:
30-
Example assumes external libraries are resolved with [rules_jvm_external](https://github.com/bazelbuild/rules_jvm_external)
31-
32-
```py
33-
# //toolchains/BUILD
34-
load("@rules_scala//scala:scala.bzl", "setup_scala_toolchain")
35-
36-
setup_scala_toolchain(
37-
name = "my_toolchain",
38-
# configure toolchain dependecies
39-
parser_combinators_deps = [
40-
"@maven//:org_scala_lang_modules_scala_parser_combinators_2_12",
41-
],
42-
scala_compile_classpath = [
43-
"@maven//:org_scala_lang_scala_compiler",
44-
"@maven//:org_scala_lang_scala_library",
45-
"@maven//:org_scala_lang_scala_reflect",
46-
],
47-
scala_library_classpath = [
48-
"@maven//:org_scala_lang_scala_library",
49-
"@maven//:org_scala_lang_scala_reflect",
50-
],
51-
scala_macro_classpath = [
52-
"@maven//:org_scala_lang_scala_library",
53-
"@maven//:org_scala_lang_scala_reflect",
54-
],
55-
scala_xml_deps = [
56-
"@maven//:org_scala_lang_modules_scala_xml_2_12",
57-
],
58-
# example of setting attribute values
59-
scalacopts = ["-Ywarn-unused"],
60-
unused_dependency_checker_mode = "off",
61-
visibility = ["//visibility:public"]
62-
)
63-
```
64-
65-
2. Register your custom toolchain from `WORKSPACE`:
66-
67-
```py
68-
# WORKSPACE
69-
register_toolchains("//toolchains:my_scala_toolchain")
70-
```
30+
#### Step 1
31+
32+
You can add your own `scala_toolchain` definition to a `BUILD` file in one of
33+
two ways. If you only want to set different [configuration
34+
options](#configuration-options), but rely on the builtin toolchain JARs, use
35+
`scala_toolchain` directly. This example is inspired by [`BUILD.bazel` from michalbogacz/scala-bazel-monorepo/](
36+
https://github.com/michalbogacz/scala-bazel-monorepo/blob/2cac860f386dcaa1c3be56cd25a84b247d335743/BUILD.bazel)):
37+
38+
```py
39+
load("@rules_scala//scala:scala_toolchain.bzl", "scala_toolchain")
40+
41+
scala_toolchain(
42+
name = "my_toolchain_impl",
43+
scalacopts = [
44+
"-Wunused:all",
45+
],
46+
strict_deps_mode = "error",
47+
unused_dependency_checker_mode = "warn",
48+
)
49+
50+
toolchain(
51+
name = "my_toolchain",
52+
toolchain = ":my_toolchain_impl",
53+
toolchain_type = "@rules_scala//scala:toolchain_type",
54+
visibility = ["//visibility:public"],
55+
)
56+
```
57+
58+
If you want to use your own compiler JARs, use `setup_scala_toolchain()`
59+
instead. This example assumes the external libraries are resolved with
60+
[rules_jvm_external](https://github.com/bazelbuild/rules_jvm_external)
61+
62+
```py
63+
# //toolchains/BUILD
64+
load("@rules_scala//scala:scala.bzl", "setup_scala_toolchain")
65+
66+
setup_scala_toolchain(
67+
name = "my_toolchain",
68+
# configure toolchain dependecies
69+
parser_combinators_deps = [
70+
"@maven//:org_scala_lang_modules_scala_parser_combinators_2_12",
71+
],
72+
scala_compile_classpath = [
73+
"@maven//:org_scala_lang_scala_compiler",
74+
"@maven//:org_scala_lang_scala_library",
75+
"@maven//:org_scala_lang_scala_reflect",
76+
],
77+
scala_library_classpath = [
78+
"@maven//:org_scala_lang_scala_library",
79+
"@maven//:org_scala_lang_scala_reflect",
80+
],
81+
scala_macro_classpath = [
82+
"@maven//:org_scala_lang_scala_library",
83+
"@maven//:org_scala_lang_scala_reflect",
84+
],
85+
scala_xml_deps = [
86+
"@maven//:org_scala_lang_modules_scala_xml_2_12",
87+
],
88+
# example of setting attribute values
89+
scalacopts = ["-Ywarn-unused"],
90+
unused_dependency_checker_mode = "off",
91+
visibility = ["//visibility:public"]
92+
)
93+
```
94+
95+
#### Step 2
96+
97+
Register your custom toolchain:
98+
99+
```py
100+
# MODULE.bazel or WORKSPACE
101+
register_toolchains("//toolchains:my_scala_toolchain")
102+
```
103+
104+
## Configuration options
105+
106+
The following attributes apply to both `scala_toolchain` and
107+
`setup_scala_toolchain`.
71108

72109
<table class="table table-condensed table-bordered table-params">
73110
<colgroup>

0 commit comments

Comments
 (0)