Select Scala toolchain by version and add JVM flags to the toolchain#113
Select Scala toolchain by version and add JVM flags to the toolchain#113jjudd wants to merge 1 commit into
Conversation
Currently there is no good way to provide JVM flags to our Scala toolchain. This change adds the ability to specify JVM flags used when invoking the Scala toolchain. This is particularly relevant for JDK 25 where lots of warnings about Unsafe appear without specifying certain JVM flags. Currently the Scala toolchain is selected by toolchain name. This causes challenges because the Scala toolchain from the consuming workspace may not match the toolchain name of another ruleset. This causes the Scala toolchain from that other ruleset to be used, but the JDK toolchain from the consuming workspace is used. This can cause problems where a JDK 25 toolchain is used with a Scala toolchain that doesn't provide JVM flags needed to silence warnings that appear when calling the Scala compiler. Changing the Scala toolchain to be selected by version causes the consuming workspace's Scala toolchains to be used, just like the JDK toolchain and other Bazel toolchains.
d2087da to
98c2bdd
Compare
|
|
||
| # Group: toolchain matches if any of the above are true | ||
| scala_version_settings_group_name = "{}-version_setting".format(name) | ||
| selects.config_setting_group( |
There was a problem hiding this comment.
Nice, I didn't know you could do this.
| ), | ||
| }, | ||
| ), | ||
| cfg = _scala_incoming_transition, |
| Args: | ||
| name: The name of the toolchain. | ||
| config: The Scalafmt configuration file. | ||
| scala_versions: A list of Scala version strings this toolchain is compatible with. Use the |
There was a problem hiding this comment.
Nit: I don't think it makes sense to accept multiple Scala versions here. Each version will require a different dialect setting in Scalafmt, and it's already possible with the changes you've made to express that a toolchain is compatible with e.g. all the Scala 3 versions.
https://scalameta.org/scalafmt/docs/configuration.html#scala-dialects
| scala_format_test( | ||
| name = "formatted-scala3", | ||
| srcs = ["formatted.scala"], | ||
| scala_version = "3", |
There was a problem hiding this comment.
It may be worth explicitly specifying the toolchain on the scala_format_test targets above so if we change the default Scala version in the future, this test continues to be effective.
There was a problem hiding this comment.
This seems to be testing replace_source_target_with_release, not javac. Perhaps this should be put in tests/jvm_flags and be called replace_source_target_with_release.bzl instead?
There was a problem hiding this comment.
Can we test that the JVM flags are actually applied?
|
|
||
| Use the `scala_version` attribute on `scala_library` and other Scala rules to select which | ||
| toolchain to use. Toolchains are matched hierarchically, e.g., `"2.13"` matches a toolchain | ||
| registered with `version = "2.13.14"`. |
There was a problem hiding this comment.
It would be helpful to mention that 2 also matches 2.13.14.
| ### Customizing the bootstrap toolchain | ||
|
|
||
| The bootstrap toolchain compiles annex's internal Scala targets (workers, test runners, etc.) using | ||
| `scalac` directly rather than Zinc. Because annex's internal targets depend on Maven artifacts from |
There was a problem hiding this comment.
This is technically inaccurate as we don't use Zinc anymore. I'd say "our compilation worker" or "compiler bridge".
Currently there is no good way to provide JVM flags to our Scala toolchain. This change adds the ability to specify JVM flags used when invoking the Scala toolchain. This is particularly relevant for JDK 25 where lots of warnings about Unsafe appear without specifying certain JVM flags.
Currently the Scala toolchain is selected by toolchain name. This causes challenges because the Scala toolchain from the consuming workspace may not match the toolchain name of another ruleset. This causes the Scala toolchain from that other ruleset to be used, but the JDK toolchain from the consuming workspace is used.
This can cause problems where a JDK 25 toolchain is used with a Scala toolchain that doesn't provide JVM flags needed to silence warnings that appear when calling the Scala compiler.
Changing the Scala toolchain to be selected by version causes the consuming workspace's Scala toolchains to be used, just like the JDK toolchain and other Bazel toolchains.