You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tweaks to scalafmt rules/script to support bazel test wrappers (#1344)
With these changes, I can use a macro to place a wrapping `sh_test`
target around this script, and change the phase ordering to put
`phase_scalafmt` before the `runfiles` phase, and suddenly it works
as intended!
NB: If this change is acceptable to folks, then it might be worth
changing the default phase placement for `ext_scalafmt` to place
it before `runfiles` by default.
Example use:
In custom bzl file, I have:
```
_enable_format_targets_and_tests_by_default = True
_ext_scalafmt = dicts.add(
_base_ext_scalafmt,
{"attrs": dicts.add(
_base_ext_scalafmt["attrs"],
{"format": attr.bool(
default = _enable_format_targets_and_tests_by_default,
doc = "Enable the check-format and auto-format synthetic run targets (foo.format-test and foo.format respectively)",
)},
)},
{"phase_providers": [
"@//path/to/some/buildfile:phase_scalafmt",
]},
)
scala_library = make_scala_library(_ext_scalafmt) # etc for other rule types
def maybe_add_format_test(**kwargs):
if (kwargs.get("format", _enable_format_targets_and_tests_by_default)):
name = kwargs["name"]
sh_test(
name = "{}.format-test.target".format(name),
srcs = [":{}.format-test".format(name)],
data = [
":{}".format(name),
],
local = True,
)
def _scalafmt_singleton_implementation(ctx):
return [
_ScalaRulePhase(
custom_phases = [
("-", "runfiles", "scalafmt", _phase_scalafmt),
],
),
]
scalafmt_singleton = rule(
implementation = _scalafmt_singleton_implementation,
)
```
then in `path/to/some/buildfile/BUILD.bazel`, I have:
```
load("//path/to/above/my_rules.bzl", "scalafmt_singleton")
scalafmt_singleton(
name = "phase_scalafmt",
visibility = ["//visibility:public"],
)
```
0 commit comments