Skip to content

Commit 819449a

Browse files
committed
Replace @scalafmt_default with toolchains target
Removes the `scalafmt_config()` macro and replaces it with the new `@rules_scala_toolchains//scalafmt:config` target. The new `test/shell/test_dependency_versions.sh` test found a problem with the previous implementation. The `dev_deps` extension in `MODULE.bazel` generated `@scalafmt_default`, leaving it invisible to `rules_scala` when it's not the main module: ```txt ERROR: no such package '@@[unknown repo 'scalafmt_default' requested from @@rules_scala~]//': The repository '@@[unknown repo 'scalafmt_default' requested from @@rules_scala~]' could not be resolved: No repository visible as '@scalafmt_default' from repository '@@rules_scala~' ERROR: .../tmp/test_dependency_versions/BUILD:52:20: every rule of type scalafmt_scala_test implicitly depends upon the target '@@[unknown repo 'scalafmt_default' requested from @@rules_scala~]//:config', but this target could not be found because of: no such package '@@[unknown repo 'scalafmt_default' requested from @@rules_scala~]//': The repository '@@[unknown repo 'scalafmt_default' requested from @@rules_scala~]' could not be resolved: No repository visible as '@scalafmt_default' from repository '@@rules_scala~' Documentation for implicit attribute config of rules of type scalafmt_scala_test: The Scalafmt configuration file. ERROR: Analysis of target '//:ScalafmtTest' failed; build aborted: Analysis failed ``` The `scalafmt_default_config()` macro is already gone, and only `scala_toolchains()` invoked `scalafmt_config()`, making this a straightforward change.
1 parent 1aced65 commit 819449a

File tree

8 files changed

+43
-43
lines changed

8 files changed

+43
-43
lines changed

BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports_files([".scalafmt.conf"])

MODULE.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ use_repo(
112112
dev_deps,
113113
"scala_proto_rules_scalapb_compilerplugin",
114114
"scala_proto_rules_scalapb_protoc_bridge",
115-
"scalafmt_default",
116115
)
117116

118117
# Default versions of version specific repos needed by some of our tests. Tests

docs/phase_scalafmt.md

+22-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,21 @@ A phase extension `phase_scalafmt` can format Scala source code via [Scalafmt](h
1212

1313
## How to set up
1414

15-
Add this snippet to `WORKSPACE`
15+
Add this snippet to `MODULE.bazel`:
1616

1717
```py
18+
# MODULE.bazel
19+
scala_deps = use_extension(
20+
"@rules_scala//scala/extensions:deps.bzl",
21+
"scala_deps",
22+
)
23+
scala_deps.scalafmt()
24+
```
25+
26+
or to `WORKSPACE`:
27+
28+
```py
29+
# WORKSPACE
1830
load(
1931
"@rules_scala//scala:toolchains.bzl",
2032
"scala_register_toolchains",
@@ -59,16 +71,23 @@ bazel run <TARGET>.format-test
5971

6072
to check the format (without modifying source code).
6173

62-
The extension provides default configuration, but there are 2 ways to use custom configuration
74+
The extension provides a default configuration, but there are 2 ways to use
75+
a custom configuration:
6376

6477
- Put `.scalafmt.conf` at the root of your workspace
6578
- Pass `.scalafmt.conf` in via `scala_toolchains`:
6679

6780
```py
81+
# MODULE.bazel
82+
scala_deps.scalafmt(
83+
default_config = "//path/to/my/custom:scalafmt.conf",
84+
)
85+
86+
# WORKSPACE
6887
scala_toolchains(
6988
# Other toolchains settings...
7089
scalafmt = True,
71-
scalafmt_default_config_path = "//path/to/my/custom:scalafmt.conf",
90+
scalafmt_default_config = "//path/to/my/custom:scalafmt.conf",
7291
)
7392
```
7493

scala/extensions/deps.bzl

+4-7
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,13 @@ _compiler_srcjar_attrs = {
9090
}
9191

9292
_scalafmt_defaults = {
93-
"default_config_path": ".scalafmt.conf",
93+
"default_config": "//:.scalafmt.conf",
9494
}
9595

9696
_scalafmt_attrs = {
97-
"default_config_path": attr.string(
98-
default = _scalafmt_defaults["default_config_path"],
99-
doc = (
100-
"The relative path to the default Scalafmt config file " +
101-
"within the repository"
102-
),
97+
"default_config": attr.label(
98+
default = _scalafmt_defaults["default_config"],
99+
doc = "The default config file for Scalafmt targets",
103100
),
104101
}
105102

scala/scalafmt/phase_scalafmt_ext.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ext_scalafmt = {
1111
"attrs": {
1212
"config": attr.label(
1313
allow_single_file = [".conf"],
14-
default = "@scalafmt_default//:config",
14+
default = "@rules_scala_toolchains//scalafmt:config",
1515
doc = "The Scalafmt configuration file.",
1616
),
1717
"format": attr.bool(

scala/scalafmt/scalafmt_repositories.bzl

-19
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
load("//scala:scala_cross_version.bzl", "extract_major_version")
22
load("//scala_proto/default:repositories.bzl", "SCALAPB_COMPILE_ARTIFACT_IDS")
33

4-
def _scalafmt_config_impl(repository_ctx):
5-
config_path = repository_ctx.attr.path
6-
build = []
7-
build.append("filegroup(")
8-
build.append(" name = \"config\",")
9-
build.append(" srcs = [\"{}\"],".format(config_path.name))
10-
build.append(" visibility = [\"//visibility:public\"],")
11-
build.append(")\n")
12-
13-
repository_ctx.file("BUILD", "\n".join(build), executable = False)
14-
repository_ctx.symlink(repository_ctx.path(config_path), config_path.name)
15-
16-
scalafmt_config = repository_rule(
17-
implementation = _scalafmt_config_impl,
18-
attrs = {
19-
"path": attr.label(mandatory = True, allow_single_file = True),
20-
},
21-
)
22-
234
_SCALAFMT_DEPS = [
245
"com_lihaoyi_fansi",
256
"com_typesafe_config",

scala/toolchains.bzl

+4-12
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ load(
77
"scala_version_artifact_ids",
88
"setup_scala_compiler_sources",
99
)
10-
load(
11-
"//scala/scalafmt:scalafmt_repositories.bzl",
12-
"scalafmt_artifact_ids",
13-
"scalafmt_config",
14-
)
10+
load("//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_artifact_ids")
1511
load("//scala:scala_cross_version.bzl", "default_maven_server_urls")
1612
load("//scala:toolchains_repo.bzl", "scala_toolchains_repo")
1713
load("//scala_proto/default:repositories.bzl", "scala_proto_artifact_ids")
@@ -40,7 +36,7 @@ def scala_toolchains(
4036
junit = False,
4137
specs2 = False,
4238
scalafmt = False,
43-
scalafmt_default_config_path = ".scalafmt.conf",
39+
scalafmt_default_config = Label("//:.scalafmt.conf"),
4440
scala_proto = False,
4541
scala_proto_options = [],
4642
jmh = False,
@@ -87,8 +83,7 @@ def scala_toolchains(
8783
junit: whether to instantiate the JUnit toolchain
8884
specs2: whether to instantiate the Specs2 JUnit toolchain
8985
scalafmt: whether to instantiate the Scalafmt toolchain
90-
scalafmt_default_config_path: the relative path to the default Scalafmt
91-
config file within the repository
86+
scalafmt_default_config: the default config file for Scalafmt targets
9287
scala_proto: whether to instantiate the scala_proto toolchain
9388
scala_proto_options: protobuf options, like 'scala3_sources' or 'grpc';
9489
`scala_proto` must also be `True` for this to take effect
@@ -112,10 +107,6 @@ def scala_toolchains(
112107

113108
setup_scala_compiler_sources(scala_compiler_srcjars)
114109

115-
if scalafmt:
116-
scalafmt_conf_target = "//:" + scalafmt_default_config_path
117-
scalafmt_config(name = "scalafmt_default", path = scalafmt_conf_target)
118-
119110
if specs2:
120111
junit = True
121112

@@ -187,6 +178,7 @@ def scala_toolchains(
187178
junit = junit,
188179
specs2 = specs2,
189180
scalafmt = scalafmt,
181+
scalafmt_default_config = scalafmt_default_config,
190182
scala_proto = scala_proto,
191183
scala_proto_options = scala_proto_options,
192184
jmh = jmh,

scala/toolchains_repo.bzl

+11
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def _scala_toolchains_repo_impl(repository_ctx):
4747
format_args = {
4848
"rules_scala_repo": Label("//:all").repo_name,
4949
"proto_options": repo_attr.scala_proto_options,
50+
"scalafmt_default_config": repo_attr.scalafmt_default_config,
5051
}
5152
toolchains = {}
5253

@@ -94,6 +95,10 @@ _scala_toolchains_repo = repository_rule(
9495
"junit": attr.bool(doc = "Instantiate the JUnit toolchain"),
9596
"specs2": attr.bool(doc = "Instantiate the Specs2 toolchain"),
9697
"scalafmt": attr.bool(doc = "Instantiate the Scalafmt toolchain"),
98+
"scalafmt_default_config": attr.label(
99+
doc = "Default Scalafmt config file",
100+
allow_single_file = True,
101+
),
97102
"scala_proto": attr.bool(
98103
doc = "Instantiate the scala_proto toolchain",
99104
),
@@ -199,6 +204,12 @@ load(
199204
"setup_scalafmt_toolchains",
200205
)
201206
207+
alias(
208+
name = "config",
209+
actual = "{scalafmt_default_config}",
210+
visibility = ["//visibility:public"],
211+
)
212+
202213
setup_scalafmt_toolchains()
203214
"""
204215

0 commit comments

Comments
 (0)