Skip to main content
Version: 5.11.x

Buildifier

Buildifier is a tool for formatting Bazel BUILD and .bzl files with a standard convention. Think of it as the formatter and lint tool for Starlark.

Setup

To add the buildifier tool as a workspace dependency see the official documentation at their GitHub README.md

In your root build file, add the following targets:

BUILD.bazel
load("@buildifier_prebuilt//:rules.bzl", "buildifier")

# Target used to fix any auto-fixable starlark formatting / linting issues.
buildifier(
name = "buildifier",
lint_mode = "fix",
mode = "fix",
)

# Target used by aspect workflows `buildifier` step to check starlark formatting and lint errors.
buildifier(
name = "buildifier.check",
lint_mode = "warn",
mode = "diff",
)

Per the Buildifier documentation, the "check" target should use mode = diff and lint_mode = warn while the "fix" target should use mode = fix and lint_mode = fix.

Finally, add the following to your aspect.yaml configuration file:

tasks:
- buildifier:
target: //buildifier.check
fix_target: //:buildifier

Note that target and fix_target are currently set to their default values and can be safely omitted if you prefer a more terse configuration.

The Workflows buildifier task runs bazel run //:buildifier.check. If it exits non-zero, then the task fails. In the case of a failure, the task prints suggested changes that the developer should apply, by running bazel run //:buildifier.