Skip to content

Commit

Permalink
Revert "Use multirun (#160)" (#171)
Browse files Browse the repository at this point in the history
* Revert "Use multirun (#160)"

This reverts commit 6df95c6.

* chore: minimize delta from main

* chore: docs update
  • Loading branch information
alexeagle authored Mar 14, 2024
1 parent d77295e commit 1fbcfee
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 232 deletions.
1 change: 0 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ bazel_dep(name = "aspect_bazel_lib", version = "1.38.0")
bazel_dep(name = "aspect_rules_js", version = "1.33.1")
bazel_dep(name = "bazel_skylib", version = "1.4.2")
bazel_dep(name = "platforms", version = "0.0.7")
bazel_dep(name = "rules_multirun", version = "0.7.0")
bazel_dep(name = "rules_multitool", version = "0.4.0")

# Needed in the root because we dereference ProtoInfo in our aspect impl
Expand Down
55 changes: 41 additions & 14 deletions docs/format.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ If you don't use pre-commit, you can just wire directly into the git hook, howev
this option will always run the formatter over all files, not just changed files.
```bash
$ echo "bazel run //:format.check" >> .git/hooks/pre-commit
$ echo "bazel run //:format -- --mode check" >> .git/hooks/pre-commit
$ chmod u+x .git/hooks/pre-commit
```

### Check that files are already formatted

This will exit non-zero if formatting is needed. You would typically run the check mode on CI.

`bazel run //tools/format:format.check`
`bazel run //:format -- --mode check`
2 changes: 1 addition & 1 deletion example/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ fetch_vale()
# Optional: multitool provides defaults for some tools such as yamlfmt
# If you do not set up multitool, you will be forced to either set an explicit tool
# for languages that have a default, or set explicit False value, e.g.
# multi_formatter_binary(jsonnet = False)
# format_multirun(jsonnet = False)
load("@rules_multitool//multitool:multitool.bzl", "multitool")

multitool(
Expand Down
5 changes: 1 addition & 4 deletions format/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ bzl_library(
name = "defs",
srcs = ["defs.bzl"],
visibility = ["//visibility:public"],
deps = [
"//format/private:formatter_binary",
"@rules_multirun//:defs",
],
deps = ["//format/private:formatter_binary"],
)

bzl_library(
Expand Down
83 changes: 16 additions & 67 deletions format/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Some formatter tools are automatically provided by default in rules_lint.
These are listed as defaults in the API docs below.
Other formatter binaries may be declared in your repository.
You can test that they work by running them directly with `bazel run`.
Other formatter binaries may be declared in your repository, and you can test them by running
them with Bazel.
For example, to add prettier, your `BUILD.bazel` file should contain:
Expand All @@ -28,86 +28,35 @@ load("@aspect_rules_lint//format:defs.bzl", "format_multirun")
format_multirun(
name = "format",
javascript = ":prettier",
...
)
```
"""

load("@rules_multirun//:defs.bzl", "command", "multirun")
load("//format/private:formatter_binary.bzl", "CHECK_FLAGS", "DEFAULT_TOOL_LABELS", "FIX_FLAGS", "TOOLS", "to_attribute_name")
load("//format/private:formatter_binary.bzl", _fmt = "format_multirun")

def format_multirun(name, **kwargs):
"""Create a multirun binary for the given formatters.
Intended to be used with `bazel run` to update source files in-place.
format_multirun_rule = _fmt

Also produces a target `[name].check` which does not edit files, rather it exits non-zero
if any sources require formatting.
def format_multirun(name, **kwargs):
"""Wrapper macro around format_multirun_rule that sets defaults for some languages.
Tools are provided by default for some languages.
These come from the `@multitool` repo.
Under --enable_bzlmod, rules_lint creates this automatically.
WORKSPACE users will have to set this up manually. See the release install snippet for an example.
Set any attribute to `False` to turn off that language altogether, rather than use a default tool.
Note that `javascript` is a special case which also formats TypeScript, TSX, JSON, CSS, and HTML.
Args:
name: name of the resulting target, typically "format"
**kwargs: attributes named for each language, providing Label of a tool that formats it
"""
commands = []

for lang, toolname in TOOLS.items():
lang_attribute = to_attribute_name(lang)

_fmt(
name = name,
# Logic:
# - if there's no value for this key, the user omitted it, so use our default if we have one
# - if there is a value, and it's False, then skip this language
# - if there's no value for this key, the user omitted it, so use our default
# - if there is a value, and it's False, then pass None to the underlying rule
# (and make sure we don't eagerly reference @multitool in case it isn't defined)
# - otherwise use the user-supplied value
tool_label = False
if lang_attribute in kwargs.keys():
tool_label = kwargs.pop(lang_attribute)
elif lang in DEFAULT_TOOL_LABELS.keys():
tool_label = Label(DEFAULT_TOOL_LABELS[lang])
if not tool_label:
continue

target_name = "_".join([name, lang.replace(" ", "_"), "with", toolname])

for mode in ["check", "fix"]:
command(
name = target_name + (".check" if mode == "check" else ""),
command = "@aspect_rules_lint//format/private:format",
description = "Formatting {} with {}...".format(lang, toolname),
environment = {
# NB: can't use str(Label(target_name)) here because bzlmod makes it
# the apparent repository, starts with @@aspect_rules_lint~override
"FIX_TARGET": "//{}:{}".format(native.package_name(), target_name),
"tool": "$(rlocationpaths %s)" % tool_label,
"lang": lang,
"flags": FIX_FLAGS[toolname] if mode == "fix" else CHECK_FLAGS[toolname],
"mode": mode,
},
data = [tool_label],
)
commands.append(target_name)

# Error checking in case some user keys were unmatched and therefore not pop'ed
for attr in kwargs.keys():
fail("""Unknown language "{}". Valid values: {}""".format(attr, [to_attribute_name(lang) for lang in TOOLS.keys()]))

multirun(
name = name,
buffer_output = True,
commands = commands,
# Run up to 4 formatters at the same time. This is an arbitrary choice, based on some idea that 4-core machines are typical.
jobs = 4,
keep_going = True,
)

multirun(
name = name + ".check",
commands = [c + ".check" for c in commands],
jsonnet = kwargs.pop("jsonnet", Label("@multitool//tools/jsonnetfmt")) or None,
go = kwargs.pop("go", Label("@multitool//tools/gofumpt")) or None,
sh = kwargs.pop("sh", Label("@multitool//tools/shfmt")) or None,
yaml = kwargs.pop("yaml", Label("@multitool//tools/yamlfmt")) or None,
**kwargs
)
7 changes: 1 addition & 6 deletions format/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

sh_binary(
name = "format",
srcs = ["format.sh"],
visibility = ["//visibility:public"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
exports_files(["format.sh"])

bzl_library(
name = "formatter_binary",
Expand Down
Loading

0 comments on commit 1fbcfee

Please sign in to comment.