Skip to content

Latest commit

 

History

History
129 lines (84 loc) · 3.06 KB

File metadata and controls

129 lines (84 loc) · 3.06 KB

Source Format Targets for Bazel Projects

This module provides reusable Bazel macros for formatting Starlark, Python, YAML, and Rust files using aspect-build/rules_lint and buildifier. Rust formatting uses the shared S-CORE policies via @score_tooling//format_checker:rustfmt_with_policies.


Features

  • ✅ Supports Python (ruff), YAML (yamlfmt), Starlark (buildifier) and Rust (policy-backed rustfmt by default)
  • ✅ Provides format.fix and format.check targets
  • ✅ Simple macro-based usage for local formatting scope
  • ✅ Centralized logic without Bazel module extensions

Directory Structure

├── BUILD.bazel
├── macros.bzl
└── README.md

Key Files

macros.bzl

Defines the main macro to use in projects:

def use_format_targets(fix_name = "format.fix", check_name = "format.check"):
    ...

This sets up:

  • format.fix — a multi-run rule that applies formatting tools
  • format.check — a test rule that checks formatting

MODULE.bazel

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")

Usage

1️⃣ Declare the dependency in your project’s MODULE.bazel:

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")

2️⃣ In your project’s BUILD.bazel:

load("@score_format_checker//:macros.bzl", "use_format_targets")

use_format_targets()

This will register two Bazel targets:

  • bazel run //:format.fix — fixes format issues
  • bazel test //:format.check — fails on unformatted files

3️⃣ In VS Code settings:

⚠️ First formatting run can be slow!

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

Rust support

  • Default formatter label: @score_tooling//format_checker:rustfmt_with_policies, which wraps the upstream rustfmt binary with the shared rustfmt.toml policies from score_rust_policies.

Benefits

✅ Centralized formatting config with local file scope ✅ Consistent developer experience across repositories ✅ Easily pluggable in CI pipelines or Git pre-commit hooks