Skip to content

Select Scala toolchain by version and add JVM flags to the toolchain#113

Open
jjudd wants to merge 1 commit into
lucid-masterfrom
jjudd-toolchain-changes
Open

Select Scala toolchain by version and add JVM flags to the toolchain#113
jjudd wants to merge 1 commit into
lucid-masterfrom
jjudd-toolchain-changes

Conversation

@jjudd

@jjudd jjudd commented Jun 12, 2026

Copy link
Copy Markdown

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.

@jjudd jjudd requested a review from jadenPete June 12, 2026 16:12
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.
@jjudd jjudd force-pushed the jjudd-toolchain-changes branch from d2087da to 98c2bdd Compare June 12, 2026 19:00

# Group: toolchain matches if any of the above are true
scala_version_settings_group_name = "{}-version_setting".format(name)
selects.config_setting_group(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I didn't know you could do this.

Comment thread rules/scalafmt.bzl
),
},
),
cfg = _scala_incoming_transition,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread tests/jvm_flags/BUILD

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically inaccurate as we don't use Zinc anymore. I'd say "our compilation worker" or "compiler bridge".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants