Skip to content

Rename {io_bazel_,}rules_scala_config #1709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 93 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,80 @@ extension. However, you must register explicitly in your `MODULE.bazel` file any
toolchains that you want to take precedence over the toolchains configured by
`scala_deps`.

### `@io_bazel_rules_scala_config` is now `@rules_scala_config`

Since `@io_bazel_rules_scala` is no longer hardcoded in `rules_scala` internals,
we've shortened `@io_bazel_rules_scala_config` to `@rules_scala_config`. This
shouldn't affect most users, but it may break some builds using
`@io_bazel_rules_scala_config` to define custom [cross-compilation targets](
./docs/cross-compilation.md).

If you can't fix uses of `@io_bazel_rules_scala_config` in your own project
immediately, you can remap `@rules_scala_config` via [`use_repo()`]:

[`use_repo()`]: https://bazel.build/rules/lib/globals/module#use_repo

```py
scala_config = use_extension(
"@rules_scala//scala/extensions:config.bzl",
"scala_config",
)

use_repo(scala_config, io_bazel_rules_scala_config = "rules_scala_config")
```

If any of your dependencies still require `@io_bazel_rules_scala_config`, use
one of the following mechanisms to override it with `@rules_scala_config`:

#### Bzlmod

For [`bazel_dep()`][] dependencies, use [`override_repo()`][] to
override `@io_bazel_rules_scala_config` with `@rules_scala_config`:

```py
bazel_dep(name = "foo", version = "1.0.0")

foo_ext = use_extension("@foo//:ext.bzl", "foo_ext")
override_repo(foo_ext, io_bazel_rules_scala_config = "rules_scala_config")
```

[`bazel_dep()`]: https://bazel.build/rules/lib/globals/module#bazel_dep
[`override_repo()`]: https://bazel.build/rules/lib/globals/module#override_repo

For [`archive_override()`][] and [`git_override()`][] dependencies, use the
`repo_mapping` attribute passed through to the underlying [`http_archive()`][]
and [`git_repository()`][] rules:

```py
archive_override(
...
repo_mapping = {
"@io_bazel_rules_scala_config": "@rules_scala_config",
}
...
)
```

[`archive_override()`]: https://bazel.build/rules/lib/globals/module#archive_override
[`git_override()`]: https://bazel.build/rules/lib/globals/module#git_override
[`http_archive()`]: https://bazel.build/rules/lib/repo/http#http_archive-repo_mapping
[`git_repository()`]: https://bazel.build/rules/lib/repo/git#git_repository-repo_mapping

#### `WORKSPACE`

Use the `repo_mapping` attribute of [`http_archive()`][] or
[`git_repository()`][]:

```py
http_archive(
...
repo_mapping = {
"@io_bazel_rules_scala_config": "@rules_scala_config",
}
...
)
```

### Bzlmod configuration (coming soon!)

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

### Builtin repositories no longer visible without `use_repo()` under Bzlmod
### Builtin repositories no longer visible by default under Bzlmod

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

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

```txt
ERROR: no such package
Expand All @@ -600,20 +677,12 @@ ERROR: no such package
No repository visible as '@org_scala_sbt_compiler_interface_3_3_5'
```

`setup_scala_toolchains` is a macro that can take user specified classpath
targets as described in [docs/scala_toolchain.md](./docs/scala_toolchain.md).
Otherwise, it _generates new classpath targets_ using the builtin `rules_scala`
repositories, but these repositories are no longer in the global scope, causing
the breakage. (A big part of the Bzlmodification work involved enabling
`rules_scala` to generate and register toolchains _without_ forcing users to
bring their dependencies into scope.)

One way to fix this specific problem is to call `use_repo` for every such
repository needed by the toolchain. Another fix, in this case, is to [use
`scala_toolchain` directly instead](
https://github.com/michalbogacz/scala-bazel-monorepo/blob/2cac860f386dcaa1c3be56cd25a84b247d335743/BUILD.bazel).
Its underlying `BUILD` rule uses the builtin toolchain dependencies via existing
targets visible within `rules_scala`, without forcing users to import them:
In this case, where the toolchain only sets different compiler options, the best
fix is to [use `scala_toolchain` directly instead][scala_tc_direct]. Its
underlying `BUILD` rule uses builtin toolchain dependencies via existing targets
visible within `rules_scala`, without forcing users to import them:

[scala_tc_direct]: https://github.com/michalbogacz/scala-bazel-monorepo/blob/2cac860f386dcaa1c3be56cd25a84b247d335743/BUILD.bazel

```py
load("@rules_scala//scala:scala_toolchain.bzl", "scala_toolchain")
Expand All @@ -635,6 +704,12 @@ toolchain(
)
```

A big part of the Bzlmodification work involved enabling `rules_scala` to
generate and register toolchains _without_ forcing users to bring their
dependencies into scope. However, another way to fix this specific problem is to
call `use_repo` for every builtin repository needed by the
`setup_scala_toolchain()` call.

## Breaking changes coming in `rules_scala` 8.x

__The main objective of 8.x will be to enable existing users to migrate to Bazel
Expand Down
29 changes: 12 additions & 17 deletions docs/cross-compilation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ especially for toolchain and rule developers.

## Quick start

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

- `scala_version` – a single default version
- `scala_versions` – a list of versions supported or required by the project
Expand Down Expand Up @@ -54,8 +53,6 @@ scala_config(
)
```



The first parameter, `scala_version`, defines the default version of Scala to
use when building the project. Values from `scala_versions` can override the
default in one of two ways:
Expand Down Expand Up @@ -90,8 +87,8 @@ one.
## Version configuration

The `scala_config` module extension (or `WORKSPACE` macro) creates the
`@io_bazel_rules_scala_config` repository. Its generated `config.bzl` file
contains several variables, including:
`@rules_scala_config` repository. Its generated `config.bzl` file contains
several variables, including:

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

### `scala_version`

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

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

```py
deps = select({
"@io_bazel_rules_scala_config//:scala_version_3_3_1": [...],
"@rules_scala_config//:scala_version_3_3_1": [...],
...
})
```
Expand All @@ -218,15 +215,13 @@ and then `load()` the macro in a `BUILD` file:

```py
load(":my_macros.bzl", "srcs")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")
load("@rules_scala//:scala_cross_version.bzl", "version_suffix")

_SCALA_VERSION_SETTING_PREFIX = "@io_bazel_rules_scala_config//:scala_version"
load("@rules_scala_config//:config.bzl", "SCALA_VERSIONS")

scala_library(
...
srcs = select({
SCALA_VERSION_SETTING_PREFIX + version_suffix(v): srcs(v)
"@rules_scala_config//:scala_version" + version_suffix(v): srcs(v)
for v in SCALA_VERSIONS
}),
...
Expand All @@ -244,14 +239,14 @@ configured.
```py
def _scala_version_transition_impl(settings, attr):
if attr.scala_version:
return {"@io_bazel_rules_scala_config//:scala_version": attr.scala_version}
return {"@rules_scala_config//:scala_version": attr.scala_version}
else:
return {}

scala_version_transition = transition(
implementation = _scala_version_transition_impl,
inputs = [],
outputs = ["@io_bazel_rules_scala_config//:scala_version"],
outputs = ["@rules_scala_config//:scala_version"],
)
```

Expand Down Expand Up @@ -328,7 +323,7 @@ https://bazel.build/reference/be/platforms-and-toolchains#toolchain) rule:
```py
toolchain(
...
target_settings = ["@io_bazel_rules_scala_config//:scala_version_3_3_1"],
target_settings = ["@rules_scala_config//:scala_version_3_3_1"],
...
)
```
Expand Down
2 changes: 1 addition & 1 deletion dt_patches/compiler_sources/extensions.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
load(
"@rules_scala//scala:scala_cross_version.bzl",
"default_maven_server_urls",
Expand All @@ -15,6 +14,7 @@ load(
"@rules_scala//third_party/repositories:scala_3_5.bzl",
_scala_3_version = "scala_version",
)
load("@rules_scala_config//:config.bzl", "SCALA_VERSION")

_IS_SCALA_2 = SCALA_VERSION.startswith("2.")
_IS_SCALA_3 = SCALA_VERSION.startswith("3.")
Expand Down
2 changes: 1 addition & 1 deletion examples/testing/multi_frameworks_toolchain/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load(
"@rules_scala//testing:testing.bzl",
"setup_scala_testing_toolchain",
)
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
load("@rules_scala_config//:config.bzl", "SCALA_VERSION")

# This example uses the same toolchain deps you'd get from using
# `scala_toolchains(junit = True, scalatest = True, specs2 = True)`. It's a
Expand Down
2 changes: 1 addition & 1 deletion jmh/toolchain/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load(
"//scala:scala_cross_version.bzl",
_versioned_repositories = "repositories",
)
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
load("@rules_scala_config//:config.bzl", "SCALA_VERSION")

DEP_PROVIDERS = [
"jmh_classpath",
Expand Down
2 changes: 1 addition & 1 deletion scala/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("//scala:scala_cross_version.bzl", "version_suffix")
load("//scala:scala_toolchain.bzl", "scala_toolchain")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
load("@rules_java//java:defs.bzl", "java_import", "java_library")
load("@rules_scala_config//:config.bzl", "SCALA_VERSION")

toolchain_type(
name = "toolchain_type",
Expand Down
4 changes: 2 additions & 2 deletions scala/private/macros/scala_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load(
"version_suffix",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")
load("@rules_scala_config//:config.bzl", "SCALA_VERSIONS")

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

_COMPILER_SOURCES_ENTRY_TEMPLATE = """
"@io_bazel_rules_scala_config//:scala_version{scala_version_suffix}":
"@rules_scala_config//:scala_version{scala_version_suffix}":
"@scala_compiler_source{scala_version_suffix}//:src","""

def _compiler_sources_repo_impl(rctx):
Expand Down
7 changes: 5 additions & 2 deletions scala/private/macros/setup_scala_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("//scala:scala_toolchain.bzl", "scala_toolchain")
load("//scala:providers.bzl", "declare_deps_provider")
load("//scala:scala_cross_version.bzl", "repositories", "version_suffix")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
load("@rules_scala_config//:config.bzl", "SCALA_VERSION")

def setup_scala_toolchain(
name,
Expand Down Expand Up @@ -98,7 +98,10 @@ def setup_scala_toolchain(
name = name,
toolchain = ":%s_impl" % name,
toolchain_type = Label("//scala:toolchain_type"),
target_settings = ["@io_bazel_rules_scala_config//:scala_version" + version_suffix(scala_version)],
target_settings = [
"@rules_scala_config//:scala_version" +
version_suffix(scala_version),
],
visibility = visibility,
)

Expand Down
4 changes: 2 additions & 2 deletions scala/scala_cross_version.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ def repositories(scala_version, repos):

def _scala_version_transition_impl(settings, attr):
if attr.scala_version:
return {"@io_bazel_rules_scala_config//:scala_version": attr.scala_version}
return {"@rules_scala_config//:scala_version": attr.scala_version}
else:
return {}

scala_version_transition = transition(
implementation = _scala_version_transition_impl,
inputs = [],
outputs = ["@io_bazel_rules_scala_config//:scala_version"],
outputs = ["@rules_scala_config//:scala_version"],
)

toolchain_transition_attr = {
Expand Down
4 changes: 2 additions & 2 deletions scala/scala_cross_version_select.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")
load(":scala_cross_version.bzl", "version_suffix")
load("@rules_scala_config//:config.bzl", "SCALA_VERSIONS")

def select_for_scala_version(default = [], **kwargs):
"""
Expand Down Expand Up @@ -41,7 +41,7 @@ def select_for_scala_version(default = [], **kwargs):
"""

return select({
"@io_bazel_rules_scala_config//:scala_version" + version_suffix(scala_version): _matches_for_version(scala_version, kwargs, default)
"@rules_scala_config//:scala_version" + version_suffix(scala_version): _matches_for_version(scala_version, kwargs, default)
for scala_version in SCALA_VERSIONS
})

Expand Down
8 changes: 5 additions & 3 deletions scala/scala_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
load("//scala:providers.bzl", _DepsInfo = "DepsInfo")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load(
"@io_bazel_rules_scala_config//:config.bzl",
"@rules_scala_config//:config.bzl",
"ENABLE_COMPILER_DEPENDENCY_TRACKING",
"SCALA_MAJOR_VERSION",
)
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

def _compute_strict_deps_mode(input_strict_deps_mode, dependency_mode):
if dependency_mode == "direct":
Expand Down Expand Up @@ -175,7 +175,9 @@ _scala_toolchain = rule(
default = False,
doc = "Changes java binaries scripts (including tests) to use argument files and not classpath jars to improve performance, requires java > 8",
),
"_scala_version": attr.label(default = "@io_bazel_rules_scala_config//:scala_version"),
"_scala_version": attr.label(
default = Label("@rules_scala_config//:scala_version"),
),
},
fragments = ["java"],
)
Expand Down
2 changes: 1 addition & 1 deletion scala/scalafmt/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("//scala/scalafmt:phase_scalafmt_ext.bzl", "scalafmt_singleton")
load("//scala:scala.bzl", "scala_binary")
load("//scala:scala_cross_version.bzl", "version_suffix")
load("//scala:scala_cross_version_select.bzl", "select_for_scala_version")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
load("@rules_scala_config//:config.bzl", "SCALA_VERSION")

filegroup(
name = "runner",
Expand Down
Loading