Skip to content

Commit fa7c661

Browse files
authored
Use custom repo rule to generate @scalafmt_default (#1626)
* Use custom repo rule to generate @scalafmt_default Part of #1482. Replaces `native.new_local_repository` in `scalafmt_default_config` with the new `scalafmt_config` repo rule. Resolves an incompatibility between Bazel 6.5.0 and 7.3.2, while creating a much smaller `@scalafmt_default` repo. Also updates the `native.register_toolchains` call with a stringified `Label` to remove the hardcoding of `@io_bazel_rules_scala`. The problem is that under Bazel 7.3.2, calling `scalafmt_default_config` from a module extension produces this error: ```txt $ 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: ```py load( "@bazel_tools//tools/build_defs/repo:local.bzl", "new_local_repository", ) ``` However, `local.bzl` isn't available in Bazel 6.5.0: ```txt $ 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. Also, it symlinks only the single Scalafmt config file, instead of every top level file and directory in the project, as `new_local_repository` did. * Fix scalafmt_repositories.bzl lint errors Broke CI again. D'oh!
1 parent 68e9987 commit fa7c661

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

scala/scalafmt/scalafmt_repositories.bzl

+21-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,27 @@ load(
77
load("//third_party/repositories:repositories.bzl", "repositories")
88
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")
99

10-
def scalafmt_default_config(path = ".scalafmt.conf"):
10+
def _scalafmt_config_impl(repository_ctx):
11+
config_path = repository_ctx.attr.path
1112
build = []
1213
build.append("filegroup(")
1314
build.append(" name = \"config\",")
14-
build.append(" srcs = [\"{}\"],".format(path))
15+
build.append(" srcs = [\"{}\"],".format(config_path.name))
1516
build.append(" visibility = [\"//visibility:public\"],")
16-
build.append(")")
17-
native.new_local_repository(name = "scalafmt_default", build_file_content = "\n".join(build), path = "")
17+
build.append(")\n")
18+
19+
repository_ctx.file("BUILD", "\n".join(build), executable = False)
20+
repository_ctx.symlink(repository_ctx.path(config_path), config_path.name)
21+
22+
scalafmt_config = repository_rule(
23+
implementation = _scalafmt_config_impl,
24+
attrs = {
25+
"path": attr.label(mandatory = True, allow_single_file = True),
26+
},
27+
)
28+
29+
def scalafmt_default_config(path = ".scalafmt.conf", **kwargs):
30+
scalafmt_config(name = "scalafmt_default", path = "//:" + path, **kwargs)
1831

1932
_SCALAFMT_DEPS = [
2033
"org_scalameta_common",
@@ -60,6 +73,7 @@ def scalafmt_repositories(
6073

6174
def _register_scalafmt_toolchains():
6275
for scala_version in SCALA_VERSIONS:
63-
native.register_toolchains(
64-
"@io_bazel_rules_scala//scala/scalafmt:scalafmt_toolchain" + version_suffix(scala_version),
65-
)
76+
native.register_toolchains(str(Label(
77+
"//scala/scalafmt:scalafmt_toolchain" +
78+
version_suffix(scala_version),
79+
)))

0 commit comments

Comments
 (0)