Skip to content

Commit

Permalink
Toolchainize //scala:toolchain_type (#1633)
Browse files Browse the repository at this point in the history
* Toolchainize //scala:toolchain_type

Moves the `toolchain` targets for `//scala:toolchain_type` to a new
`@io_bazel_rules_scala_toolchains` repository as a step towards
Bzlmodification. Part of #1482.

Instantiating toolchains in their own repository enables module
extensions to define the the repositories required by those toolchains
within the extension's own namespace. Bzlmod users can then register the
toolchains from this repository without having to import all the
toolchains' dependencies into their own namespace via `use_repo()`.

---

The `scala_toolchains_repo()` macro wraps the underlying repository rule
and assigns it the standard name `io_bazel_rules_scala_toolchains`.
Right now it's only instantiating the main Scala toolchain via the
default `scala = True` parameter. Future changes will expand this
macro and repository rule with more boolean parameters to instantiate
other toolchains, specifically:

- `scalatest`
- `junit`
- `specs2`
- `twitter_scrooge`
- `jmh`
- `scala_proto` and `scala_proto_enable_all_options`
- `testing` (includes all of `scalatest`, `junit`, and `specs2`)
- `scalafmt`

---

`WORKSPACE` users will now have to import and call the
`scala_toolchains_repo()` macro to instantiate
`@io_bazel_rules_scala_toolchains`.

```py
load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config()

load(
    "//scala:scala.bzl",
    "rules_scala_setup",
    "rules_scala_toolchain_deps_repositories",
    "scala_toolchains_repo",
)

rules_scala_setup()

rules_scala_toolchain_deps_repositories()

scala_toolchains_repo()

register_toolchains("@io_bazel_rules_scala_toolchains//...:all")
```

This is what the corresponding `MODULE.bazel` setup would look like:

```py
module(name = "rules_scala", version = "7.0.0")

scala_config = use_extension(
    "//scala/extensions:config.bzl", "scala_config"
)
scala_config.settings(scala_version = "2.13.14")

scala_deps = use_extension("//scala/extensions:deps.bzl", "scala_deps")
scala_deps.toolchains()
```

The `register_toolchains()` call in `WORKSPACE` isn't strictly required
at this point, but is recommended. However, all the `WORKSPACE` files in
this repo already register their required toolchains using existing
macros, which have been updated in this change.

In fact, calling `register_toolchains()` right after
`scala_toolchains_repo()` as shown above breaks two tests that depend on
the existing `WORKSPACE` toolchain registration:

- `test_compilation_fails_with_plus_one_deps_undefined` from
  `test/shell/test_compilation.sh` depends on
  `scala_register_unused_deps_toolchains()` setting up its toolchain to
  resolve first. `//scala:unused_dependency_checker_error_toolchain`
  sets the `scala_toolchain()` parameters `dependency_tracking_method =
  "ast-plus"` and `unused_dependency_checker_mode = "error"`, and the
  `@io_bazel_rules_scala_toolchains//scala` toolchains don't.

- `test_scala_binary_allows_opt_in_to_use_of_argument_file_in_runner_for_improved_performance`
  from `test/shell/test_scala_binary.sh` depends on the
  `use_argument_file_in_runner` parameter of `scala_toolchain` being
  `False`. This is the default, but the
  `@io_bazel_rules_scala_toolchains//scala` toolchains explicitly set
  this to `True` instead.

In the Bzlmod case, the `register_toolchains()` call isn't necessary at
all. This is because `@io_bazel_rules_scala_toolchains` includes one
package per set of toolchains, and the rules_scala `MODULE.bazel` calls
`register_toolchains("@io_bazel_rules_scala_toolchains//...:all")`. This
will automatically register all configured rules_scala toolchains, while
allowing users to override any of them using `register_toolchains()` in
their own `MODULE.bazel` files.

Technically, the `scala_deps.toolchains()` call isn't required when only
using the default `scala = True` parameter; the rules_scala
`MODULE.bazel` will instantiate this automatically, too.

* Add scala_toolchains macro for WORKSPACE, Bzlmod

Extracted a single `scala_toolchains` macro to share between `WORKSPACE`
and the `deps.bzl` module extension. This will make it easier to ensure
`WORKSPACE` compatibility, and to add a Bazel module extension as a thin
layer on top. Part of #1482.

This change includes updates to `rules_scala_setup` and
`scala_repositories` to support this, while preserving compatibility
with existing `WORKSPACE` calls. The next commit will replace many
existing `WORKSPACE` calls with `scala_toolchains`.

* Replace WORKSPACE calls with scala_toolchains()

Also added `@io_bazel_rules_scala_toolchains//...:all` to
`register_toolchains()` calls everywhere, even when not specifically
necessary. This proves the mechanism is safe and works with `WORKSPACE`
now, and will make future updates to consolidate other toolchains less
noisy.

* Remove obsolete WORKSPACE toolchain calls

Should've been included in the previous commit. All tests still pass.

* Replace scala_register_unused_deps_toolchains call

Missed this one in third_party/test/proto earlier. Also removed
unnecessary `USE_BAZEL_VERSION` expression in
test_scala_proto_library.sh. If `USE_BAZEL_VERSION` is set, it takes
precedence to begin with, and third_party/test/proto/.bazelversion
exists now after #1629.

* Give scala_toolchains_repo a default name argument

This turns out to be helpful when adapting
`test_version/WORKSPACE.template` to the toolchainized version of
`twitter_scrooge` for testing. In this case, we want `twitter_scooge` to
be in its own customized repo, separate from the one generated by
`scala_toolchains`.

This seems like it might be generally useful when writing module
extensions for alternative toolchains.

* Update Scala toolchainizaion per @simuons in #1633

- Removes an extraneous `compiler_sources_repo` call.

- `scala_toolchains` will now _always_ create the main Scala toolchain
  repository (there's no longer an option to turn it off).

- Registers `@io_bazel_rules_scala_toolchains//...:all` in
  `scala_register_toolchains.

* Extract load_rules_dependencies macro

Requested by @simuons in #1633 to make `rules_scala_setup` and
`scala_repositories` more readable while maintaining the existing APIs.
  • Loading branch information
mbland authored Nov 27, 2024
1 parent 23ae356 commit 43146ea
Show file tree
Hide file tree
Showing 28 changed files with 317 additions and 241 deletions.
19 changes: 8 additions & 11 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config(enable_compiler_dependency_tracking = True)

load("//scala:scala.bzl", "rules_scala_setup", "rules_scala_toolchain_deps_repositories")
load("//scala:scala.bzl", "scala_toolchains")

rules_scala_setup()
scala_toolchains(fetch_sources = True)

rules_scala_toolchain_deps_repositories(fetch_sources = True)
register_toolchains(
"//testing:testing_toolchain",
"//scala:unused_dependency_checker_error_toolchain",
"//test/proto:scalapb_toolchain",
"@io_bazel_rules_scala_toolchains//...:all",
)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

Expand Down Expand Up @@ -82,8 +87,6 @@ load("//specs2:specs2_junit.bzl", "specs2_junit_repositories")

specs2_junit_repositories()

register_toolchains("//testing:testing_toolchain")

load("//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_default_config", "scalafmt_repositories")

scalafmt_default_config()
Expand Down Expand Up @@ -115,12 +118,6 @@ local_repository(
path = "third_party/test/example_external_workspace",
)

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_unused_deps_toolchains")

scala_register_unused_deps_toolchains()

register_toolchains("@io_bazel_rules_scala//test/proto:scalapb_toolchain")

load("//scala:scala_maven_import_external.bzl", "java_import_external")

# bazel's java_import_external has been altered in rules_scala to be a macro based on jvm_import_external
Expand Down
15 changes: 6 additions & 9 deletions dt_patches/test_dt_patches/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config(enable_compiler_dependency_tracking = True)

load(
"@io_bazel_rules_scala//scala:scala.bzl",
"rules_scala_setup",
"rules_scala_toolchain_deps_repositories",
)
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")
load(
"@io_bazel_rules_scala//scala:scala_cross_version.bzl",
"default_maven_server_urls",
Expand Down Expand Up @@ -104,14 +100,15 @@ scala_maven_import_external(
server_urls = default_maven_server_urls(),
)

rules_scala_setup()

rules_scala_toolchain_deps_repositories(
scala_toolchains(
fetch_sources = True,
validate_scala_version = False,
)

register_toolchains(":dt_scala_toolchain")
register_toolchains(
":dt_scala_toolchain",
"@io_bazel_rules_scala_toolchains//...:all",
)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

Expand Down
20 changes: 7 additions & 13 deletions dt_patches/test_dt_patches_user_srcjar/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config(enable_compiler_dependency_tracking = True)

load(
"@io_bazel_rules_scala//scala:scala.bzl",
"rules_scala_setup",
"rules_scala_toolchain_deps_repositories",
)
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")
load(
"@io_bazel_rules_scala//scala:scala_cross_version.bzl",
"default_maven_server_urls",
Expand Down Expand Up @@ -182,14 +178,16 @@ srcjars_by_version = {
},
}

rules_scala_setup(scala_compiler_srcjar = srcjars_by_version[SCALA_VERSION])

rules_scala_toolchain_deps_repositories(
scala_toolchains(
fetch_sources = True,
scala_compiler_srcjars = srcjars_by_version,
validate_scala_version = False,
)

register_toolchains(":dt_scala_toolchain")
register_toolchains(
":dt_scala_toolchain",
"@io_bazel_rules_scala_toolchains//...:all",
)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

Expand All @@ -206,7 +204,3 @@ rules_proto_toolchains()
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")

scala_register_toolchains()
14 changes: 3 additions & 11 deletions examples/crossbuild/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ scala_config(
],
)

load(
"@io_bazel_rules_scala//scala:scala.bzl",
"rules_scala_setup",
"rules_scala_toolchain_deps_repositories",
)
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")

rules_scala_setup()
scala_toolchains()

rules_scala_toolchain_deps_repositories()
register_toolchains("@io_bazel_rules_scala_toolchains//...:all")

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

Expand All @@ -64,10 +60,6 @@ load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")

scala_register_toolchains()

load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain")

scalatest_repositories()
Expand Down
14 changes: 3 additions & 11 deletions examples/scala3/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config(scala_version = "3.5.2")

load(
"@io_bazel_rules_scala//scala:scala.bzl",
"rules_scala_setup",
"rules_scala_toolchain_deps_repositories",
)
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")

rules_scala_setup()
scala_toolchains(fetch_sources = True)

rules_scala_toolchain_deps_repositories(fetch_sources = True)
register_toolchains("@io_bazel_rules_scala_toolchains//...:all")

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

Expand All @@ -56,7 +52,3 @@ rules_proto_toolchains()
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")

scala_register_toolchains()
19 changes: 7 additions & 12 deletions examples/semanticdb/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config(scala_version = "2.13.15")

load(
"@io_bazel_rules_scala//scala:scala.bzl",
"rules_scala_setup",
"rules_scala_toolchain_deps_repositories",
)
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")

rules_scala_setup()
scala_toolchains(fetch_sources = True)

rules_scala_toolchain_deps_repositories(fetch_sources = True)
register_toolchains(
#Register and use the custom toolchain that has semanticdb enabled
"//:semanticdb_toolchain",
"@io_bazel_rules_scala_toolchains//...:all",
)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

Expand All @@ -59,8 +59,3 @@ rules_proto_toolchains()
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

#Register and use the custom toolchain that has semanticdb enabled
register_toolchains(
"//:semanticdb_toolchain",
)
19 changes: 6 additions & 13 deletions examples/testing/multi_frameworks_toolchain/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config()

load(
"@io_bazel_rules_scala//scala:scala.bzl",
"rules_scala_setup",
"rules_scala_toolchain_deps_repositories",
)
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")

rules_scala_setup()
scala_toolchains(fetch_sources = True)

rules_scala_toolchain_deps_repositories(fetch_sources = True)
register_toolchains(
":testing_toolchain",
"@io_bazel_rules_scala_toolchains//...:all",
)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

Expand All @@ -57,10 +56,6 @@ load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")

scala_register_toolchains()

load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories")
load("@io_bazel_rules_scala//testing:junit.bzl", "junit_repositories")
load("@io_bazel_rules_scala//testing:specs2_junit.bzl", "specs2_junit_repositories")
Expand All @@ -70,5 +65,3 @@ scalatest_repositories()
junit_repositories()

specs2_junit_repositories()

register_toolchains(":testing_toolchain")
14 changes: 3 additions & 11 deletions examples/testing/scalatest_repositories/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config()

load(
"@io_bazel_rules_scala//scala:scala.bzl",
"rules_scala_setup",
"rules_scala_toolchain_deps_repositories",
)
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")

rules_scala_setup()
scala_toolchains(fetch_sources = True)

rules_scala_toolchain_deps_repositories(fetch_sources = True)
register_toolchains("@io_bazel_rules_scala_toolchains//...:all")

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

Expand All @@ -57,10 +53,6 @@ load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")

scala_register_toolchains()

load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain")

scalatest_repositories()
Expand Down
14 changes: 3 additions & 11 deletions examples/testing/specs2_junit_repositories/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config()

load(
"@io_bazel_rules_scala//scala:scala.bzl",
"rules_scala_setup",
"rules_scala_toolchain_deps_repositories",
)
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")

rules_scala_setup()
scala_toolchains(fetch_sources = True)

rules_scala_toolchain_deps_repositories(fetch_sources = True)
register_toolchains("@io_bazel_rules_scala_toolchains//...:all")

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

Expand All @@ -57,10 +53,6 @@ load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")

scala_register_toolchains()

load("@io_bazel_rules_scala//testing:specs2_junit.bzl", "specs2_junit_repositories", "specs2_junit_toolchain")

specs2_junit_repositories()
Expand Down
3 changes: 2 additions & 1 deletion jmh/jmh.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//scala:scala.bzl", "scala_binary", "scala_library")
load("//scala/private:rules/scala_binary.bzl", "scala_binary")
load("//scala/private:rules/scala_library.bzl", "scala_library")
load(
"//scala:scala_cross_version.bzl",
"default_maven_server_urls",
Expand Down
Loading

0 comments on commit 43146ea

Please sign in to comment.