Skip to content

Commit c75cc9f

Browse files
committed
Expose scala_protoc_toolchains as a repo rule
Enables greater consistency between Bzlmod and `WORKSPACE` while reducing implementation complexity. There's now only the repo rule, not a wrapper macro and an extension. The user was always going to need to register this toolchain in the main module, even under Bzlmod. Exposing the repo rule puts the `protoc` toolchain's name under the user's control, instead of making it part of the `rules_scala` public API. Uses `rules_proto` as an intermediary to `protobuf` APIs for detecting `--incompatible_enable_proto_toolchain_resolution`. This enables users to use older versions of `protobuf` and `rules_proto` without breaking `rules_scala`. Also updated the `scala_proto_toolchain` implementation to access the precompiled `protoc` via mechanisms moved to `protoc/private/protoc_toolchain.bzl`.
1 parent 101494c commit c75cc9f

File tree

19 files changed

+217
-228
lines changed

19 files changed

+217
-228
lines changed

README.md

+48-27
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ load("@platforms//host:extension.bzl", "host_platform_repo")
7575
host_platform_repo(name = "host_platform")
7676

7777
# This is optional, but still safe to include even when not using
78-
# `--incompatible_enable_proto_toolchain_resolution`. Requires calling
79-
# `scala_protoc_toolchains()` as seen below.
80-
register_toolchains("@rules_scala//protoc:all")
78+
# `--incompatible_enable_proto_toolchain_resolution`. Requires invoking the
79+
# `scala_protoc_toolchains` repo rule. Register this toolchain before any
80+
# others. See the "Using a precompiled protocol compiler" section below.
81+
register_toolchains("@rules_scala_protoc_toolchains//...:all")
8182

8283
load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
8384

@@ -125,12 +126,13 @@ load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")
125126

126127
rules_proto_toolchains()
127128

128-
# Include this after loading `platforms` and `com_google_protobuf` to enable the
129-
# `//protoc` precompiled protocol compiler toolchains. See the "Using a
130-
# precompiled protocol compiler" section below.
129+
# Include this after loading `platforms`, `com_google_protobuf`, and
130+
# `rules_proto` to enable the `//protoc` precompiled protocol compiler
131+
# toolchains. See the "Using a precompiled protocol compiler" section below.
131132
load("@rules_scala//protoc:toolchains.bzl", "scala_protoc_toolchains")
132133

133-
scala_protoc_toolchains()
134+
# This name can be anything, but we recommend `rules_scala_protoc_toolchains`.
135+
scala_protoc_toolchains(name = "rules_scala_protoc_toolchains")
134136

135137
load("@rules_scala//:scala_config.bzl", "scala_config")
136138

@@ -224,41 +226,57 @@ To set the flag in your `.bazelrc` file:
224226
common --incompatible_enable_proto_toolchain_resolution
225227
```
226228

227-
In both `MODULE.bazel` and `WORKSPACE`, add the following statement _before_ any
228-
other toolchain registrations. It's safe to include even when not using
229-
`--incompatible_enable_proto_toolchain_resolution`.
229+
In both `MODULE.bazel` and `WORKSPACE`, add the following `register_toolchains`
230+
statement _before_ any other toolchain registrations. It's safe to include even
231+
when not using `--incompatible_enable_proto_toolchain_resolution`.
232+
233+
The repository name of the target is determined by the
234+
`scala_protoc_toolchains()` repo rule invocation, as illustrated below. It is OK
235+
to call `register_toolchains` before the repo rule.
230236

231237
```py
232-
# MODULE.bazel or WORKSPACE
233-
register_toolchains("@rules_scala//protoc:all")
238+
# MODULE.bazel
239+
register_toolchains(
240+
"@rules_scala_protoc_toolchains//...:all",
241+
dev_dependency = True,
242+
)
243+
244+
# WORKSPACE
245+
register_toolchains("@rules_scala_protoc_toolchains//...:all")
234246
```
235247

236-
#### Using `scala_protoc` in `MODULE.bazel`
248+
#### Invoking the `scala_protoc_toolchains()` repo rule
237249

238-
The `scala_protoc` extension instantiates the protocol compiler toolchain
239-
binaries under Bzlmod:
250+
The `scala_protoc_toolchains` repo rule instantiates the protocol compiler
251+
toolchain. The repo name can be anything, but we recommend `rules_scala_protoc_toolchains`.
252+
253+
Under Bzlmod, this looks like:
240254

241255
```py
242256
# MODULE.bazel
243257

244-
scala_protoc = use_extension(
245-
"@rules_scala//scala/extensions:protoc.bzl",
246-
"scala_protoc",
258+
scala_protoc_toolchains = use_repo_rule(
259+
"@rules_scala//protoc:toolchains.bzl",
260+
"scala_protoc_toolchains",
247261
)
248-
```
249262

250-
#### Calling `scala_protoc_toolchains()` in `WORKSPACE`
263+
# This name can be anything, but we recommend `rules_scala_protoc_toolchains`.
264+
scala_protoc_toolchains(
265+
name = "rules_scala_protoc_toolchains",
266+
dev_dependency = True,
267+
)
268+
```
251269

252-
The `scala_protoc_toolchains` macro instantiates the protocol compiler toolchain
253-
binaries under `WORKSPACE`:
270+
Under `WORKSPACE`, this looks like:
254271

255272
```py
256273
# WORKSPACE
257274

258-
# Include this after loading `platforms` and `com_google_protobuf`.
275+
# Include this after loading `platforms`, `com_google_protobuf`, and
276+
# `rules_proto`.
259277
load("@rules_scala//protoc:toolchains.bzl", "scala_protoc_toolchains")
260278

261-
scala_protoc_toolchains()
279+
scala_protoc_toolchains(name = "rules_scala_protoc_toolchains")
262280
```
263281

264282
#### Specifying additional `protoc` platforms
@@ -280,8 +298,10 @@ the remote execution platform is Linux running on an x86 processor.
280298
```py
281299
# MODULE.bazel
282300

283-
scala_protoc.toolchains(
301+
scala_protoc_toolchains(
302+
name = "rules_scala_protoc_toolchains",
284303
platforms = ["linux-x86_64"],
304+
dev_dependency = True,
285305
)
286306
```
287307

@@ -291,6 +311,7 @@ In `WORKSPACE` you would include:
291311
# WORKSPACE
292312

293313
scala_protoc_toolchains(
314+
name = "rules_scala_protoc_toolchains",
294315
platforms = ["linux-x86_64"],
295316
)
296317
```
@@ -308,7 +329,7 @@ transitive dependency on `@com_google_protobuf//:protoc` remains, causing
308329
If and when `protobuf` merges that pull request, or applies an equivalent fix,
309330
this patch will no longer be necessary.
310331

311-
#### Bzlmod setup
332+
#### `protobuf` patch setup under Bzlmod
312333

313334
Applying the `protobuf` patch requires using [`single_version_override`][],
314335
which also requires that the patch be a regular file in your own repo. In other
@@ -339,7 +360,7 @@ single_version_override(
339360
)
340361
```
341362

342-
#### `WORKSPACE` setup
363+
#### `protobuf` patch setup under `WORKSPACE`
343364

344365
[`scala/deps.bzl`](./scala/deps.bzl) already applies the `protobuf` patch by
345366
default. If you need to apply it yourself, you can also copy it to your repo as

WORKSPACE

+10-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ load("@platforms//host:extension.bzl", "host_platform_repo")
1414
host_platform_repo(name = "host_platform")
1515

1616
# This is optional, but still safe to include even when not using
17-
# `--incompatible_enable_proto_toolchain_resolution`.
18-
register_toolchains("//protoc:all")
17+
# `--incompatible_enable_proto_toolchain_resolution`. Requires invoking the
18+
# `scala_protoc_toolchains` repo rule. Register this toolchain before any
19+
# others.
20+
register_toolchains("@rules_scala_protoc_toolchains//...:all")
1921

2022
load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
2123

@@ -56,10 +58,13 @@ load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")
5658

5759
rules_proto_toolchains()
5860

59-
# Must come after loading `platforms` and `com_google_protobuf`.
60-
load("//protoc:toolchains.bzl", "scala_protoc_toolchains")
61+
# Include this after loading `platforms`, `com_google_protobuf`, and
62+
# `rules_proto` to enable the `//protoc` precompiled protocol compiler
63+
# toolchains.
64+
load("@rules_scala//protoc:toolchains.bzl", "scala_protoc_toolchains")
6165

62-
scala_protoc_toolchains()
66+
# This name can be anything, but we recommend `rules_scala_protoc_toolchains`.
67+
scala_protoc_toolchains(name = "rules_scala_protoc_toolchains")
6368

6469
load("//:scala_config.bzl", "scala_config")
6570

dt_patches/test_dt_patches/WORKSPACE

+2-9
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@ load("@rules_scala//scala:deps.bzl", "rules_scala_dependencies")
1111

1212
rules_scala_dependencies()
1313

14-
# Only include the next two statements if using
15-
# `--incompatible_enable_proto_toolchain_resolution`.
1614
load("@platforms//host:extension.bzl", "host_platform_repo")
1715

18-
# Instantiates the `@host_platform` repo to work around:
19-
# - https://github.com/bazelbuild/bazel/issues/22558
2016
host_platform_repo(name = "host_platform")
2117

22-
# This is optional, but still safe to include even when not using
23-
# `--incompatible_enable_proto_toolchain_resolution`.
24-
register_toolchains("@rules_scala//protoc:all")
18+
register_toolchains("@rules_scala_protoc_toolchains//...:all")
2519

2620
load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
2721

@@ -62,10 +56,9 @@ load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")
6256

6357
rules_proto_toolchains()
6458

65-
# Must come after loading `platforms` and `com_google_protobuf`.
6659
load("@rules_scala//protoc:toolchains.bzl", "scala_protoc_toolchains")
6760

68-
scala_protoc_toolchains()
61+
scala_protoc_toolchains(name = "rules_scala_protoc_toolchains")
6962

7063
load("@rules_scala//:scala_config.bzl", "scala_config")
7164

dt_patches/test_dt_patches_user_srcjar/WORKSPACE

+2-9
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@ load("@rules_scala//scala:deps.bzl", "rules_scala_dependencies")
1111

1212
rules_scala_dependencies()
1313

14-
# Only include the next two statements if using
15-
# `--incompatible_enable_proto_toolchain_resolution`.
1614
load("@platforms//host:extension.bzl", "host_platform_repo")
1715

18-
# Instantiates the `@host_platform` repo to work around:
19-
# - https://github.com/bazelbuild/bazel/issues/22558
2016
host_platform_repo(name = "host_platform")
2117

22-
# This is optional, but still safe to include even when not using
23-
# `--incompatible_enable_proto_toolchain_resolution`.
24-
register_toolchains("@rules_scala//protoc:all")
18+
register_toolchains("@rules_scala_protoc_toolchains//...:all")
2519

2620
load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
2721

@@ -62,10 +56,9 @@ load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")
6256

6357
rules_proto_toolchains()
6458

65-
# Must come after loading `platforms` and `com_google_protobuf`.
6659
load("@rules_scala//protoc:toolchains.bzl", "scala_protoc_toolchains")
6760

68-
scala_protoc_toolchains()
61+
scala_protoc_toolchains(name = "rules_scala_protoc_toolchains")
6962

7063
load("@rules_scala//:scala_config.bzl", "scala_config")
7164

examples/crossbuild/WORKSPACE

+2-9
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@ load("@rules_scala//scala:deps.bzl", "rules_scala_dependencies")
1111

1212
rules_scala_dependencies()
1313

14-
# Only include the next two statements if using
15-
# `--incompatible_enable_proto_toolchain_resolution`.
1614
load("@platforms//host:extension.bzl", "host_platform_repo")
1715

18-
# Instantiates the `@host_platform` repo to work around:
19-
# - https://github.com/bazelbuild/bazel/issues/22558
2016
host_platform_repo(name = "host_platform")
2117

22-
# This is optional, but still safe to include even when not using
23-
# `--incompatible_enable_proto_toolchain_resolution`.
24-
register_toolchains("@rules_scala//protoc:all")
18+
register_toolchains("@rules_scala_protoc_toolchains//...:all")
2519

2620
load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
2721

@@ -62,10 +56,9 @@ load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")
6256

6357
rules_proto_toolchains()
6458

65-
# Must come after loading `platforms` and `com_google_protobuf`.
6659
load("@rules_scala//protoc:toolchains.bzl", "scala_protoc_toolchains")
6760

68-
scala_protoc_toolchains()
61+
scala_protoc_toolchains(name = "rules_scala_protoc_toolchains")
6962

7063
load("@rules_scala//:scala_config.bzl", "scala_config")
7164

examples/overridden_artifacts/WORKSPACE

+2-9
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@ load("@rules_scala//scala:deps.bzl", "rules_scala_dependencies")
1111

1212
rules_scala_dependencies()
1313

14-
# Only include the next two statements if using
15-
# `--incompatible_enable_proto_toolchain_resolution`.
1614
load("@platforms//host:extension.bzl", "host_platform_repo")
1715

18-
# Instantiates the `@host_platform` repo to work around:
19-
# - https://github.com/bazelbuild/bazel/issues/22558
2016
host_platform_repo(name = "host_platform")
2117

22-
# This is optional, but still safe to include even when not using
23-
# `--incompatible_enable_proto_toolchain_resolution`.
24-
register_toolchains("@rules_scala//protoc:all")
18+
register_toolchains("@rules_scala_protoc_toolchains//...:all")
2519

2620
load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
2721

@@ -62,10 +56,9 @@ load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")
6256

6357
rules_proto_toolchains()
6458

65-
# Must come after loading `platforms` and `com_google_protobuf`.
6659
load("@rules_scala//protoc:toolchains.bzl", "scala_protoc_toolchains")
6760

68-
scala_protoc_toolchains()
61+
scala_protoc_toolchains(name = "rules_scala_protoc_toolchains")
6962

7063
load("@rules_scala//:scala_config.bzl", "scala_config")
7164

examples/scala3/WORKSPACE

+2-9
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@ load("@rules_scala//scala:deps.bzl", "rules_scala_dependencies")
1111

1212
rules_scala_dependencies()
1313

14-
# Only include the next two statements if using
15-
# `--incompatible_enable_proto_toolchain_resolution`.
1614
load("@platforms//host:extension.bzl", "host_platform_repo")
1715

18-
# Instantiates the `@host_platform` repo to work around:
19-
# - https://github.com/bazelbuild/bazel/issues/22558
2016
host_platform_repo(name = "host_platform")
2117

22-
# This is optional, but still safe to include even when not using
23-
# `--incompatible_enable_proto_toolchain_resolution`.
24-
register_toolchains("@rules_scala//protoc:all")
18+
register_toolchains("@rules_scala_protoc_toolchains//...:all")
2519

2620
load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
2721

@@ -62,10 +56,9 @@ load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")
6256

6357
rules_proto_toolchains()
6458

65-
# Must come after loading `platforms` and `com_google_protobuf`.
6659
load("@rules_scala//protoc:toolchains.bzl", "scala_protoc_toolchains")
6760

68-
scala_protoc_toolchains()
61+
scala_protoc_toolchains(name = "rules_scala_protoc_toolchains")
6962

7063
load("@rules_scala//:scala_config.bzl", "scala_config")
7164

examples/semanticdb/WORKSPACE

+2-9
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@ load("@rules_scala//scala:deps.bzl", "rules_scala_dependencies")
1111

1212
rules_scala_dependencies()
1313

14-
# Only include the next two statements if using
15-
# `--incompatible_enable_proto_toolchain_resolution`.
1614
load("@platforms//host:extension.bzl", "host_platform_repo")
1715

18-
# Instantiates the `@host_platform` repo to work around:
19-
# - https://github.com/bazelbuild/bazel/issues/22558
2016
host_platform_repo(name = "host_platform")
2117

22-
# This is optional, but still safe to include even when not using
23-
# `--incompatible_enable_proto_toolchain_resolution`.
24-
register_toolchains("@rules_scala//protoc:all")
18+
register_toolchains("@rules_scala_protoc_toolchains//...:all")
2519

2620
load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
2721

@@ -62,10 +56,9 @@ load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")
6256

6357
rules_proto_toolchains()
6458

65-
# Must come after loading `platforms` and `com_google_protobuf`.
6659
load("@rules_scala//protoc:toolchains.bzl", "scala_protoc_toolchains")
6760

68-
scala_protoc_toolchains()
61+
scala_protoc_toolchains(name = "rules_scala_protoc_toolchains")
6962

7063
load("@rules_scala//:scala_config.bzl", "scala_config")
7164

examples/testing/multi_frameworks_toolchain/WORKSPACE

+2-9
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@ load("@rules_scala//scala:deps.bzl", "rules_scala_dependencies")
1111

1212
rules_scala_dependencies()
1313

14-
# Only include the next two statements if using
15-
# `--incompatible_enable_proto_toolchain_resolution`.
1614
load("@platforms//host:extension.bzl", "host_platform_repo")
1715

18-
# Instantiates the `@host_platform` repo to work around:
19-
# - https://github.com/bazelbuild/bazel/issues/22558
2016
host_platform_repo(name = "host_platform")
2117

22-
# This is optional, but still safe to include even when not using
23-
# `--incompatible_enable_proto_toolchain_resolution`.
24-
register_toolchains("@rules_scala//protoc:all")
18+
register_toolchains("@rules_scala_protoc_toolchains//...:all")
2519

2620
load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
2721

@@ -62,10 +56,9 @@ load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")
6256

6357
rules_proto_toolchains()
6458

65-
# Must come after loading `platforms` and `com_google_protobuf`.
6659
load("@rules_scala//protoc:toolchains.bzl", "scala_protoc_toolchains")
6760

68-
scala_protoc_toolchains()
61+
scala_protoc_toolchains(name = "rules_scala_protoc_toolchains")
6962

7063
load("@rules_scala//:scala_config.bzl", "scala_config")
7164

0 commit comments

Comments
 (0)