- ✅ Centralized logic without Bazel module extensions
├── BUILD.bazel
├── macros.bzl
└── README.mdDefines the main macro to use in projects:
def use_format_targets(fix_name = "format.fix", check_name = "format.check", languages = None):
...This sets up:
format.fix— a multi-run rule that applies formatting toolsformat.check— a test rule that checks formatting
The languages parameter selects which formatters are wired up. Supported values:
| Language | Formatter |
|---|---|
python |
@aspect_rules_lint//format:ruff |
rust |
@score_tooling//format_checker:rustfmt_with_policies |
starlark |
@buildifier_prebuilt//:buildifier |
yaml |
@aspect_rules_lint//format:yamlfmt |
cpp |
@llvm_toolchain//:clang-format |
When languages is omitted, every language except cpp is enabled. C++ is
opt-in so that projects without C++ code do not depend on the LLVM toolchain.
Declares this module and includes required dependencies:
module(name = "score_format_checker", version = "0.1.1")
bazel_dep(name = "aspect_rules_lint", version = "1.0.3")
bazel_dep(name = "buildifier_prebuilt", version = "7.3.1")
bazel_dep(name = "score_rust_policies", version = "0.0.2")bazel_dep(name = "score_format_checker", version = "0.1.1")
# If using local source:
local_path_override(
module_name = "score_format_checker",
path = "../tooling/format",
)
# Explicit dependencies required by the macro
bazel_dep(name = "aspect_rules_lint", version = "1.0.3")
bazel_dep(name = "buildifier_prebuilt", version = "7.3.1")
bazel_dep(name = "score_rust_policies", version = "0.0.2")load("@score_format_checker//:macros.bzl", "use_format_targets")
use_format_targets()This will register two Bazel targets:
bazel run //:format.fix— fixes format issuesbazel test //:format.check— fails on unformatted files
To control which formatters run, pass an explicit languages list:
# Only check Python and Starlark.
use_format_targets(languages = ["python", "starlark"])Add the following entry to .vscode/settings.json:
"rust-analyzer.rustfmt.overrideCommand": [
"${workspaceFolder}/.vscode/rustfmt.sh"
]Add .vscode/rustfmt.sh file with +x permissions:
#!/usr/bin/env bash
bazel run @score_tooling//format_checker:rustfmt_with_policies- Default formatter label:
@score_tooling//format_checker:rustfmt_with_policies, which wraps the upstreamrustfmtbinary with the sharedrustfmt.tomlpolicies fromscore_rust_policies.
C++ formatting is opt-in. Enable it by adding cpp to the languages list:
use_format_targets(languages = ["python", "rust", "starlark", "yaml", "cpp"])- Formatter label:
@llvm_toolchain//:clang-format, which requires the LLVM toolchain (toolchains_llvm) to be available in the consuming module. - clang-format runs with
--style=file, so a.clang-formatconfiguration file is expected in the repository tree. - Because enabling
cppintroduces the LLVM dependency, it is excluded from the default language set.
✅ Centralized formatting config with local file scope ✅ Consistent developer experience across repositories ✅ Easily pluggable in CI pipelines or Git pre-commit hooks