Skip to content

Commit f2adbac

Browse files
authored
Fix Scalafmt default_config docs for WORKSPACE (#1734)
Makes it clear that `WORKSPACE` users must use a target `Label` string with the `default_config` option for the `scalafmt` argument of `scala_toolchains`. Notes that the specified config file must exist within a package, including but not limited to having a `BUILD` file in the repository root. --- @gergelyfabian noticed this while building gergelyfabian/bazel-scala-example after #1730 landed. This isn't fixable in code. With this call: ```py scala_toolchains( # Other toolchains... scalafmt = {"default_config": ".scalafmt.conf"}, ) ``` - `scala_toolchains` looks for the file under `external/.scalafmt.conf`. - Updating `scala_toolchains` to wrap `default_config` in a `Label` makes it relative to `@rules_scala//scala`. This because `scala_toolchains` resides in `@rules_scala//scala:toolchains.bzl`. - `native.package_relative_label` is only available when called from a `BUILD` file. Hence `default_config` values under `WORKSPACE` must be valid target labels. The problem doesn't exist under Bzlmod; plain file paths will work fine, so long as a `BUILD` file exists at the repository root.
1 parent bc9aa1c commit f2adbac

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

docs/phase_scalafmt.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,21 @@ bazel run <TARGET>.format-test
7272
to check the format (without modifying source code).
7373

7474
The extension provides a default configuration. To use a custom configuration,
75-
pass its path or target Label to the toolchain configuration:
75+
pass its path or target `Label` string to the toolchain configuration. The file
76+
must exist within a package; the following examples presume a `BUILD` file
77+
exists in the repository root:
7678

7779
```py
7880
# MODULE.bazel
7981
scala_deps.scalafmt(
80-
default_config = "path/to/my/custom/scalafmt.conf",
82+
default_config = "path/to/my/custom/.scalafmt.conf",
8183
)
8284

8385
# WORKSPACE
8486
scala_toolchains(
8587
# Other toolchains settings...
86-
scalafmt = {"default_config": "path/to/my/custom/scalafmt.conf"},
88+
# This _must_ be a target Label under WORKSPACE.
89+
scalafmt = {"default_config": "//:path/to/my/custom/.scalafmt.conf"},
8790
)
8891
```
8992

0 commit comments

Comments
 (0)