Skip to content

Commit 973f330

Browse files
committed
Generate toolchains root package, update README
Now that the default Scala toolchain is optional, `@rules_scala_toolchains()` may not contain any subpackages. This enables `register_toolchains("@rules_scala_toolchains//...:all")` to succeed nonetheless. This is important for the Bzlmod implementation, as the `MODULE.bazel` file from `rules_scala` will always make this `register_toolchains()` call. Also updated the `README` to show directly how to apply the protocol compiler toolchainization patch to `protobuf`. This ensures that if we remove the patch from `scala/deps.bzl`, the example will live on in the `README`.
1 parent 0ba9ab7 commit 973f330

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

README.md

+36-21
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ load("@rules_scala//:scala_config.bzl", "scala_config")
143143
# scala_config(scala_version = "2.13.16")
144144
#
145145
# You may define your own custom toolchain using Maven artifact dependencies
146-
# configured by your `WORKSPACE` file, imported using external loader like
147-
# https://github.com/bazelbuild/rules_jvm_external.
146+
# configured by your `WORKSPACE` file, imported using an external loader like
147+
# https://github.com/bazelbuild/rules_jvm_external. See docs/scala_toolchain.md.
148148
scala_config()
149149

150150
load(
@@ -153,16 +153,17 @@ load(
153153
"scala_toolchains",
154154
)
155155

156-
# Defines a default toolchain repo for the configured Scala version that
157-
# loads Maven deps like the Scala compiler and standard libs. On production
158-
# projects, you may consider defining a custom toolchain to use your project's
159-
# required dependencies instead.
156+
# Defines a default toolchain repo for the configured Scala version that loads
157+
# Maven deps like the Scala compiler and standard libs. Enable other builtin
158+
# toolchains by setting their corresponding parameters to `True`. See the
159+
# `scala_toolchains()` docstring for all builtin toolchain configuration
160+
# options.
160161
#
161-
# Optional builtin rules_scala toolchains may be configured by setting
162-
# parameters as documented in the `scala_toolchains()` docstring.
163-
scala_toolchains(
164-
scalatest = True,
165-
)
162+
# On production projects, you may consider defining a custom toolchain to use
163+
# your project's required dependencies instead. In that case, you can omit
164+
# `scala_toolchains()` or explicitly set `scala = False` if you use it to
165+
# instantiate other builtin toolchains.
166+
scala_toolchains()
166167

167168
scala_register_toolchains()
168169
```
@@ -360,12 +361,27 @@ single_version_override(
360361

361362
#### `protobuf` patch setup under `WORKSPACE`
362363

363-
[`scala/deps.bzl`](./scala/deps.bzl) already applies the `protobuf` patch by
364-
default. If you need to apply it yourself, you can also copy it to your repo as
365-
described in the Bzlmod setup above. Then follow the example in `scala/deps.bzl`
366-
to apply it in your own `http_archive` call.
364+
[`scala/deps.bzl`](./scala/deps.bzl) currently applies the `protobuf` patch to `protobuf` v30.1.
367365

368-
However, `WORKSPACE` must include the `host_platform_repo` snippet from
366+
If you need to apply the patch to a different version of `protobuf`, copy it to
367+
your repo as described in the Bzlmod setup above. Then apply it in your own
368+
`http_archive` call:
369+
370+
```py
371+
http_archive(
372+
name = "com_google_protobuf",
373+
sha256 = "1451b03faec83aed17cdc71671d1bbdfd72e54086b827f5f6fd02bf7a4041b68",
374+
strip_prefix = "protobuf-30.1",
375+
url = "https://github.com/protocolbuffers/protobuf/archive/refs/tags/v30.1.tar.gz",
376+
repo_mapping = {"@com_google_absl": "@abseil-cpp"},
377+
patches = ["//protobuf.patch"],
378+
patch_args = ["-p1"],
379+
)
380+
```
381+
382+
#### Setting up the `@host_platform` repo under `WORKSPACE`
383+
384+
`WORKSPACE` must include the `host_platform_repo` snippet from
369385
[Getting started](#getting-started) to work around bazelbuild/bazel#22558:
370386

371387
```py
@@ -744,8 +760,6 @@ load(
744760
"scala_toolchains",
745761
)
746762

747-
# The `scala_version` toolchain repos used by `scala_library` and `scala_binary`
748-
# are _always_ configured, but all others are optional.
749763
scala_toolchains(
750764
scalafmt = True,
751765
scalatest = True,
@@ -791,9 +805,10 @@ same exact call will also work in `MODULE.bazel`.
791805
### Disabling builtin Scala toolchains when defining custom Scala toolchains
792806

793807
When [defining a 'scala_toolchain()' using custom compiler JARs](
794-
docs/scala_toolchain.md#b-defining-your-own-scala_toolchain), while using
795-
`scala_toolchains()` to instantiate builtin toolchains (like the [protoc
796-
toolchain](#protoc)), set `scala = False`:
808+
docs/scala_toolchain.md#b-defining-your-own-scala_toolchain), don't use
809+
`scala_toolchains()` if you don't need any other builtin toolchains.
810+
811+
If you do need other builtin toolchains, then set `scala = False`:
797812

798813
```py
799814
# WORKSPACE

docs/scala_toolchain.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ register_toolchains("//toolchains:my_scala_toolchain")
103103

104104
#### Step 3 (optional)
105105

106-
If you use `scala_toolchains()` to instantiate other builtin toolchains like the
107-
precompiled proto compiler toolchain, set `scala = False`:
106+
If you use `scala_toolchains()` to instantiate other builtin toolchains, set
107+
`scala = False`:
108108

109109
```py
110110
# WORKSPACE
@@ -116,8 +116,8 @@ scala_toolchains(
116116

117117
Otherwise, `scala_toolchains()` will try to instantiate a default Scala
118118
toolchain and its compiler JAR dependency repositories. The build will then fail
119-
if the configured Scala version doesn't match the `scala_version` value in
120-
the corresponding `third_party/repositories/scala_*.bzl` file.
119+
if the configured Scala version doesn't match the `scala_version` value in the
120+
corresponding `third_party/repositories/scala_*.bzl` file.
121121

122122
## Configuration options
123123

scala/toolchains_repo.bzl

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def _scala_toolchains_repo_impl(repository_ctx):
7171
if repo_attr.scalafmt:
7272
toolchains["scalafmt"] = _SCALAFMT_TOOLCHAIN_BUILD
7373

74+
# Generate a root package so that the `register_toolchains` call in
75+
# `MODULE.bazel` always succeeds.
76+
repository_ctx.file("BUILD", executable = False)
77+
7478
for pkg, build in toolchains.items():
7579
repository_ctx.file(
7680
pkg + "/BUILD",

0 commit comments

Comments
 (0)