Skip to content

Commit 5e15e88

Browse files
committed
Update phase_scalafmt.md, check scalafmt conf path
Updates the Scalafmt documentation to reflect the current API. Adds a check to `scala_toolchains_repo` to `fail` if the Scalafmt `default_config` file doesn't exist. The previous commit doesn't actually restore the exact pre-bazel-contrib#1725 API. It eliminates the `exports_files` requirement, but still requires a `Label`, not just a relative path string or a `.scalafmt.conf` in the root directory. After experimenting a bit and thinking this through, it seems the explicit `Label` requirement provides the most robust and reliable interface. Specifically, supporting the previously optional `.scalafmt.conf` in the root directory requires detecting whether it actually exists before falling back to the default. Having users explicitly specify their own config seems a small burden to impose for a more straightforward and correct implementation. At the same time, I saw the opportunity to provide the user with explicit feedback if the specified config file doesn't exist. Hence the new check and `fail()` message. Also renamed the generated `.scalafmt.conf` file in `@rules_scala_toolchains//scalafmt` to `scalafmt.conf`. No need for it to be a hidden file in that context.
1 parent 7575df7 commit 5e15e88

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

docs/phase_scalafmt.md

+15-18
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,21 @@ bazel run <TARGET>.format-test
7171

7272
to check the format (without modifying source code).
7373

74-
The extension provides a default configuration, but there are 2 ways to use
75-
a custom configuration:
76-
77-
- Put `.scalafmt.conf` at the root of your workspace
78-
- Pass `.scalafmt.conf` into the toolchain configuration:
79-
80-
```py
81-
# MODULE.bazel
82-
scala_deps.scalafmt(
83-
default_config = "//path/to/my/custom:scalafmt.conf",
84-
)
85-
86-
# WORKSPACE
87-
scala_toolchains(
88-
# Other toolchains settings...
89-
scalafmt = {"default_config": "//path/to/my/custom:scalafmt.conf"},
90-
)
91-
```
74+
The extension provides a default configuration. To use a custom configuration,
75+
pass its path or target Label to the toolchain configuration:
76+
77+
```py
78+
# MODULE.bazel
79+
scala_deps.scalafmt(
80+
default_config = "path/to/my/custom/scalafmt.conf",
81+
)
82+
83+
# WORKSPACE
84+
scala_toolchains(
85+
# Other toolchains settings...
86+
scalafmt = {"default_config": "path/to/my/custom/scalafmt.conf"},
87+
)
88+
```
9289

9390
When using Scala 3, you must append `runner.dialect = scala3` to
9491
`.scalafmt.conf`.

scala/toolchains_repo.bzl

+6-5
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ def _scala_toolchains_repo_impl(repository_ctx):
6868

6969
if repo_attr.scalafmt:
7070
toolchains["scalafmt"] = _SCALAFMT_TOOLCHAIN_BUILD
71-
repository_ctx.symlink(
72-
repository_ctx.path(repo_attr.scalafmt_default_config),
73-
"scalafmt/.scalafmt.conf",
74-
)
71+
config_path = repository_ctx.path(repo_attr.scalafmt_default_config)
72+
73+
if not config_path.exists:
74+
fail("Scalafmt default config file doesn't exist:", config_path)
75+
repository_ctx.symlink(config_path, "scalafmt/scalafmt.conf")
7576

7677
# Generate a root package so that the `register_toolchains` call in
7778
# `MODULE.bazel` always succeeds.
@@ -190,7 +191,7 @@ load(
190191
191192
filegroup(
192193
name = "config",
193-
srcs = [":.scalafmt.conf"],
194+
srcs = [":scalafmt.conf"],
194195
visibility = ["//visibility:public"],
195196
)
196197

0 commit comments

Comments
 (0)