Skip to content

Commit eadc090

Browse files
authored
Rename {io_bazel_,}rules_scala_config (bazel-contrib#1709)
Shortens `@io_bazel_rules_scala_config` to `@rules_scala_config` in light of the `@io_bazel_rules_scala` removal in bazel-contrib#1696. Part of bazel-contrib#1482. Adds a section to `README.md` indicating this as a breaking change, with advice on working around any breakages that aren't immediately fixable. Includes updates the "Builtin repositories no longer visible by default under Bzlmod" section to improve clarity. Per @simuons's advice in bazel-contrib#1703 (comment).
1 parent e75d7f1 commit eadc090

File tree

26 files changed

+146
-76
lines changed

26 files changed

+146
-76
lines changed

README.md

+93-18
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,80 @@ extension. However, you must register explicitly in your `MODULE.bazel` file any
495495
toolchains that you want to take precedence over the toolchains configured by
496496
`scala_deps`.
497497

498+
### `@io_bazel_rules_scala_config` is now `@rules_scala_config`
499+
500+
Since `@io_bazel_rules_scala` is no longer hardcoded in `rules_scala` internals,
501+
we've shortened `@io_bazel_rules_scala_config` to `@rules_scala_config`. This
502+
shouldn't affect most users, but it may break some builds using
503+
`@io_bazel_rules_scala_config` to define custom [cross-compilation targets](
504+
./docs/cross-compilation.md).
505+
506+
If you can't fix uses of `@io_bazel_rules_scala_config` in your own project
507+
immediately, you can remap `@rules_scala_config` via [`use_repo()`]:
508+
509+
[`use_repo()`]: https://bazel.build/rules/lib/globals/module#use_repo
510+
511+
```py
512+
scala_config = use_extension(
513+
"@rules_scala//scala/extensions:config.bzl",
514+
"scala_config",
515+
)
516+
517+
use_repo(scala_config, io_bazel_rules_scala_config = "rules_scala_config")
518+
```
519+
520+
If any of your dependencies still require `@io_bazel_rules_scala_config`, use
521+
one of the following mechanisms to override it with `@rules_scala_config`:
522+
523+
#### Bzlmod
524+
525+
For [`bazel_dep()`][] dependencies, use [`override_repo()`][] to
526+
override `@io_bazel_rules_scala_config` with `@rules_scala_config`:
527+
528+
```py
529+
bazel_dep(name = "foo", version = "1.0.0")
530+
531+
foo_ext = use_extension("@foo//:ext.bzl", "foo_ext")
532+
override_repo(foo_ext, io_bazel_rules_scala_config = "rules_scala_config")
533+
```
534+
535+
[`bazel_dep()`]: https://bazel.build/rules/lib/globals/module#bazel_dep
536+
[`override_repo()`]: https://bazel.build/rules/lib/globals/module#override_repo
537+
538+
For [`archive_override()`][] and [`git_override()`][] dependencies, use the
539+
`repo_mapping` attribute passed through to the underlying [`http_archive()`][]
540+
and [`git_repository()`][] rules:
541+
542+
```py
543+
archive_override(
544+
...
545+
repo_mapping = {
546+
"@io_bazel_rules_scala_config": "@rules_scala_config",
547+
}
548+
...
549+
)
550+
```
551+
552+
[`archive_override()`]: https://bazel.build/rules/lib/globals/module#archive_override
553+
[`git_override()`]: https://bazel.build/rules/lib/globals/module#git_override
554+
[`http_archive()`]: https://bazel.build/rules/lib/repo/http#http_archive-repo_mapping
555+
[`git_repository()`]: https://bazel.build/rules/lib/repo/git#git_repository-repo_mapping
556+
557+
#### `WORKSPACE`
558+
559+
Use the `repo_mapping` attribute of [`http_archive()`][] or
560+
[`git_repository()`][]:
561+
562+
```py
563+
http_archive(
564+
...
565+
repo_mapping = {
566+
"@io_bazel_rules_scala_config": "@rules_scala_config",
567+
}
568+
...
569+
)
570+
```
571+
498572
### Bzlmod configuration (coming soon!)
499573

500574
The upcoming Bzlmod implementation will funnel through the `scala_toolchains()`
@@ -565,7 +639,7 @@ now, as there's not yet a corresponding [`toolchain_type()`](
565639
https://bazel.build/versions/6.1.0/reference/be/platform#toolchain_type) target
566640
in `@rules_java`.
567641

568-
### Builtin repositories no longer visible without `use_repo()` under Bzlmod
642+
### Builtin repositories no longer visible by default under Bzlmod
569643

570644
Under Bzlmod, repos are only visible to the module extension that creates them,
571645
unless the `MODULE.bazel` file brings them into scope with
@@ -587,9 +661,12 @@ setup_scala_toolchain(
587661
)
588662
```
589663

590-
This worked under `WORKSPACE`, but broke under Bzlmod, the error message
591-
indicating that the builtin `@org_scala_sbt_compiler_interface` toolchain jar
592-
isn't visible:
664+
`setup_scala_toolchains` is a macro that can take user specified classpath
665+
targets as described in [docs/scala_toolchain.md](./docs/scala_toolchain.md).
666+
Without explicit `*_classpath` or `*_deps` arguments, `setup_scala_toolchain()`
667+
defaults to using dependency repositories generated by `rules_scala` itself.
668+
This worked under `WORKSPACE`, but breaks under Bzlmod, because the builtin
669+
toolchain dependency repos are no longer in the project's scope by default:
593670

594671
```txt
595672
ERROR: no such package
@@ -600,20 +677,12 @@ ERROR: no such package
600677
No repository visible as '@org_scala_sbt_compiler_interface_3_3_5'
601678
```
602679

603-
`setup_scala_toolchains` is a macro that can take user specified classpath
604-
targets as described in [docs/scala_toolchain.md](./docs/scala_toolchain.md).
605-
Otherwise, it _generates new classpath targets_ using the builtin `rules_scala`
606-
repositories, but these repositories are no longer in the global scope, causing
607-
the breakage. (A big part of the Bzlmodification work involved enabling
608-
`rules_scala` to generate and register toolchains _without_ forcing users to
609-
bring their dependencies into scope.)
610-
611-
One way to fix this specific problem is to call `use_repo` for every such
612-
repository needed by the toolchain. Another fix, in this case, is to [use
613-
`scala_toolchain` directly instead](
614-
https://github.com/michalbogacz/scala-bazel-monorepo/blob/2cac860f386dcaa1c3be56cd25a84b247d335743/BUILD.bazel).
615-
Its underlying `BUILD` rule uses the builtin toolchain dependencies via existing
616-
targets visible within `rules_scala`, without forcing users to import them:
680+
In this case, where the toolchain only sets different compiler options, the best
681+
fix is to [use `scala_toolchain` directly instead][scala_tc_direct]. Its
682+
underlying `BUILD` rule uses builtin toolchain dependencies via existing targets
683+
visible within `rules_scala`, without forcing users to import them:
684+
685+
[scala_tc_direct]: https://github.com/michalbogacz/scala-bazel-monorepo/blob/2cac860f386dcaa1c3be56cd25a84b247d335743/BUILD.bazel
617686

618687
```py
619688
load("@rules_scala//scala:scala_toolchain.bzl", "scala_toolchain")
@@ -635,6 +704,12 @@ toolchain(
635704
)
636705
```
637706

707+
A big part of the Bzlmodification work involved enabling `rules_scala` to
708+
generate and register toolchains _without_ forcing users to bring their
709+
dependencies into scope. However, another way to fix this specific problem is to
710+
call `use_repo` for every builtin repository needed by the
711+
`setup_scala_toolchain()` call.
712+
638713
## Breaking changes coming in `rules_scala` 8.x
639714

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

docs/cross-compilation.md

+12-17
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ especially for toolchain and rule developers.
66

77
## Quick start
88

9-
The `scala_config` module extension (or`WORKSPACE` macro) creates the
10-
`@io_bazel_rules_scala_config` repository. It accepts two parameters that
11-
specify the the Scala versions supported within the project:
9+
The `scala_config` module extension (or`WORKSPACE` macro) accepts two parameters
10+
that specify the the Scala versions supported within the project:
1211

1312
- `scala_version` – a single default version
1413
- `scala_versions` – a list of versions supported or required by the project
@@ -54,8 +53,6 @@ scala_config(
5453
)
5554
```
5655

57-
58-
5956
The first parameter, `scala_version`, defines the default version of Scala to
6057
use when building the project. Values from `scala_versions` can override the
6158
default in one of two ways:
@@ -90,8 +87,8 @@ one.
9087
## Version configuration
9188

9289
The `scala_config` module extension (or `WORKSPACE` macro) creates the
93-
`@io_bazel_rules_scala_config` repository. Its generated `config.bzl` file
94-
contains several variables, including:
90+
`@rules_scala_config` repository. Its generated `config.bzl` file contains
91+
several variables, including:
9592

9693
- `SCALA_VERSION` – representing the default Scala version, e.g., `"3.3.1"`
9794
- `SCALA_VERSIONS` – representing all configured Scala versions, e.g.,
@@ -105,8 +102,8 @@ value.
105102

106103
### `scala_version`
107104

108-
The root package of `@io_bazel_rules_scala_config` defines the following build
109-
setting (specifically, a ['string_setting()' from '@bazel_skylib'](
105+
The root package of `@rules_scala_config` defines the following build setting
106+
(specifically, a ['string_setting()' from '@bazel_skylib'](
110107
https://github.com/bazelbuild/bazel-skylib/blob/1.7.1/docs/common_settings_doc.md#string_setting)):
111108

112109
```py
@@ -199,7 +196,7 @@ See the complete documentation in the [scala_cross_version_select.bzl](
199196

200197
```py
201198
deps = select({
202-
"@io_bazel_rules_scala_config//:scala_version_3_3_1": [...],
199+
"@rules_scala_config//:scala_version_3_3_1": [...],
203200
...
204201
})
205202
```
@@ -218,15 +215,13 @@ and then `load()` the macro in a `BUILD` file:
218215

219216
```py
220217
load(":my_macros.bzl", "srcs")
221-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")
222218
load("@rules_scala//:scala_cross_version.bzl", "version_suffix")
223-
224-
_SCALA_VERSION_SETTING_PREFIX = "@io_bazel_rules_scala_config//:scala_version"
219+
load("@rules_scala_config//:config.bzl", "SCALA_VERSIONS")
225220

226221
scala_library(
227222
...
228223
srcs = select({
229-
SCALA_VERSION_SETTING_PREFIX + version_suffix(v): srcs(v)
224+
"@rules_scala_config//:scala_version" + version_suffix(v): srcs(v)
230225
for v in SCALA_VERSIONS
231226
}),
232227
...
@@ -244,14 +239,14 @@ configured.
244239
```py
245240
def _scala_version_transition_impl(settings, attr):
246241
if attr.scala_version:
247-
return {"@io_bazel_rules_scala_config//:scala_version": attr.scala_version}
242+
return {"@rules_scala_config//:scala_version": attr.scala_version}
248243
else:
249244
return {}
250245

251246
scala_version_transition = transition(
252247
implementation = _scala_version_transition_impl,
253248
inputs = [],
254-
outputs = ["@io_bazel_rules_scala_config//:scala_version"],
249+
outputs = ["@rules_scala_config//:scala_version"],
255250
)
256251
```
257252

@@ -328,7 +323,7 @@ https://bazel.build/reference/be/platforms-and-toolchains#toolchain) rule:
328323
```py
329324
toolchain(
330325
...
331-
target_settings = ["@io_bazel_rules_scala_config//:scala_version_3_3_1"],
326+
target_settings = ["@rules_scala_config//:scala_version_3_3_1"],
332327
...
333328
)
334329
```

dt_patches/compiler_sources/extensions.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
21
load(
32
"@rules_scala//scala:scala_cross_version.bzl",
43
"default_maven_server_urls",
@@ -15,6 +14,7 @@ load(
1514
"@rules_scala//third_party/repositories:scala_3_5.bzl",
1615
_scala_3_version = "scala_version",
1716
)
17+
load("@rules_scala_config//:config.bzl", "SCALA_VERSION")
1818

1919
_IS_SCALA_2 = SCALA_VERSION.startswith("2.")
2020
_IS_SCALA_3 = SCALA_VERSION.startswith("3.")

examples/testing/multi_frameworks_toolchain/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ load(
33
"@rules_scala//testing:testing.bzl",
44
"setup_scala_testing_toolchain",
55
)
6-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
6+
load("@rules_scala_config//:config.bzl", "SCALA_VERSION")
77

88
# This example uses the same toolchain deps you'd get from using
99
# `scala_toolchains(junit = True, scalatest = True, specs2 = True)`. It's a

jmh/toolchain/toolchain.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ load(
44
"//scala:scala_cross_version.bzl",
55
_versioned_repositories = "repositories",
66
)
7-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
7+
load("@rules_scala_config//:config.bzl", "SCALA_VERSION")
88

99
DEP_PROVIDERS = [
1010
"jmh_classpath",

scala/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("//scala:scala_cross_version.bzl", "version_suffix")
22
load("//scala:scala_toolchain.bzl", "scala_toolchain")
3-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
43
load("@rules_java//java:defs.bzl", "java_import", "java_library")
4+
load("@rules_scala_config//:config.bzl", "SCALA_VERSION")
55

66
toolchain_type(
77
name = "toolchain_type",

scala/private/macros/scala_repositories.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ load(
55
"version_suffix",
66
)
77
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
8-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")
8+
load("@rules_scala_config//:config.bzl", "SCALA_VERSIONS")
99

1010
def _dt_patched_compiler_impl(rctx):
1111
# Need to give the file a .zip extension so rctx.extract knows what type of archive it is
@@ -31,7 +31,7 @@ _COMPILER_SOURCE_ALIAS_TEMPLATE = """alias(
3131
"""
3232

3333
_COMPILER_SOURCES_ENTRY_TEMPLATE = """
34-
"@io_bazel_rules_scala_config//:scala_version{scala_version_suffix}":
34+
"@rules_scala_config//:scala_version{scala_version_suffix}":
3535
"@scala_compiler_source{scala_version_suffix}//:src","""
3636

3737
def _compiler_sources_repo_impl(rctx):

scala/private/macros/setup_scala_toolchain.bzl

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("//scala:scala_toolchain.bzl", "scala_toolchain")
22
load("//scala:providers.bzl", "declare_deps_provider")
33
load("//scala:scala_cross_version.bzl", "repositories", "version_suffix")
4-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
4+
load("@rules_scala_config//:config.bzl", "SCALA_VERSION")
55

66
def setup_scala_toolchain(
77
name,
@@ -98,7 +98,10 @@ def setup_scala_toolchain(
9898
name = name,
9999
toolchain = ":%s_impl" % name,
100100
toolchain_type = Label("//scala:toolchain_type"),
101-
target_settings = ["@io_bazel_rules_scala_config//:scala_version" + version_suffix(scala_version)],
101+
target_settings = [
102+
"@rules_scala_config//:scala_version" +
103+
version_suffix(scala_version),
104+
],
102105
visibility = visibility,
103106
)
104107

scala/scala_cross_version.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ def repositories(scala_version, repos):
7474

7575
def _scala_version_transition_impl(settings, attr):
7676
if attr.scala_version:
77-
return {"@io_bazel_rules_scala_config//:scala_version": attr.scala_version}
77+
return {"@rules_scala_config//:scala_version": attr.scala_version}
7878
else:
7979
return {}
8080

8181
scala_version_transition = transition(
8282
implementation = _scala_version_transition_impl,
8383
inputs = [],
84-
outputs = ["@io_bazel_rules_scala_config//:scala_version"],
84+
outputs = ["@rules_scala_config//:scala_version"],
8585
)
8686

8787
toolchain_transition_attr = {

scala/scala_cross_version_select.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")
21
load(":scala_cross_version.bzl", "version_suffix")
2+
load("@rules_scala_config//:config.bzl", "SCALA_VERSIONS")
33

44
def select_for_scala_version(default = [], **kwargs):
55
"""
@@ -41,7 +41,7 @@ def select_for_scala_version(default = [], **kwargs):
4141
"""
4242

4343
return select({
44-
"@io_bazel_rules_scala_config//:scala_version" + version_suffix(scala_version): _matches_for_version(scala_version, kwargs, default)
44+
"@rules_scala_config//:scala_version" + version_suffix(scala_version): _matches_for_version(scala_version, kwargs, default)
4545
for scala_version in SCALA_VERSIONS
4646
})
4747

scala/scala_toolchain.bzl

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
load("//scala:providers.bzl", _DepsInfo = "DepsInfo")
2+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
23
load(
3-
"@io_bazel_rules_scala_config//:config.bzl",
4+
"@rules_scala_config//:config.bzl",
45
"ENABLE_COMPILER_DEPENDENCY_TRACKING",
56
"SCALA_MAJOR_VERSION",
67
)
7-
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
88

99
def _compute_strict_deps_mode(input_strict_deps_mode, dependency_mode):
1010
if dependency_mode == "direct":
@@ -175,7 +175,9 @@ _scala_toolchain = rule(
175175
default = False,
176176
doc = "Changes java binaries scripts (including tests) to use argument files and not classpath jars to improve performance, requires java > 8",
177177
),
178-
"_scala_version": attr.label(default = "@io_bazel_rules_scala_config//:scala_version"),
178+
"_scala_version": attr.label(
179+
default = Label("@rules_scala_config//:scala_version"),
180+
),
179181
},
180182
fragments = ["java"],
181183
)

scala/scalafmt/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ load("//scala/scalafmt:phase_scalafmt_ext.bzl", "scalafmt_singleton")
33
load("//scala:scala.bzl", "scala_binary")
44
load("//scala:scala_cross_version.bzl", "version_suffix")
55
load("//scala:scala_cross_version_select.bzl", "select_for_scala_version")
6-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
6+
load("@rules_scala_config//:config.bzl", "SCALA_VERSION")
77

88
filegroup(
99
name = "runner",

0 commit comments

Comments
 (0)