Use custom repo rule to generate @scalafmt_default #1626
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Replaces
native.new_local_repository
inscalafmt_default_config
with the newscalafmt_config
repo rule. Also updates thenative.register_toolchains
call with a stringifiedLabel
to remove the hardcoding of@io_bazel_rules_scala
.Resolves an incompatibility between Bazel 6.5.0 and 7.3.2, while creating a much smaller
@scalafmt_default
repo. The problem is that under Bazel 7.3.2, callingscalafmt_default_config
from a module extension produces this error:$ bazel test //test/scalafmt/... ERROR: Traceback (most recent call last): File ".../scala/extensions/deps.bzl", line 218, column 32, in _scala_deps_impl scalafmt_default_config() File ".../scala/scalafmt/scalafmt_repositories.bzl", line 18, column 32, in scalafmt_default_config native.new_local_repository( Error in new_local_repository: The native module can be accessed only from a BUILD thread. Wrap the function in a macro and call it from a BUILD file
Ostensibly the solution is to replace
native.new_local_repository
with:However,
local.bzl
isn't available in Bazel 6.5.0:$ bazel test //test/scalafmt/... ERROR: Error computing the main repository mapping: at .../scala/scalafmt/scalafmt_repositories.bzl:8:6: cannot load '@bazel_tools//tools/build_defs/repo:local.bzl': no such file
The new
scalafmt_config
repository rule works under both Bazel 6.5.0 and 7.3.2,WORKSPACE
and Bzlmod. Also, it symlinks only the single Scalafmt config file, instead of every top level file and directory in the project, asnew_local_repository
did.Motivation
Part of the Bzlmodification effort from #1482, to make
scalafmt_default_config
compatible with bothWORKSPACE
andMODULE.bazel
under both Bazel 6 and 7.Also, removing instances of
@io_bazel_rules_scala
withLabel
s reduces the project's dependency on this name. Bzlmod consumers will then be able to callbazel_dep(name = "rules_scala", ...)
without having to setrepo_name = "io_bazel_rules_scala"
.