Skip to content

Format Module Reference

The format module provides code formatting transformations. Currently, it supports formatting Bazel BUILD and .bzl files using Buildifier.

Format BUILD files and Starlark (.bzl) files using Buildifier:

format.buildifier(
paths = glob(["**.bzl", "**/BUILD", "BUILD"]),
type = "auto",
lint = "OFF",
lint_warnings = [],
)
ParameterTypeDefaultDescription
pathsGlobglob(["**.bzl", "**/BUILD", "BUILD"])Files to format
typestring"auto"File type: auto, bzl, build, workspace
lintstring"OFF"Lint mode: OFF or FIX
lint_warningslist[]Specific warnings to enable (requires lint = "FIX")
TypeDescription
autoDetect type from filename (recommended)
bzlFormat as .bzl Starlark file
buildFormat as BUILD file
workspaceFormat as WORKSPACE file
ModeDescription
OFFOnly format, no linting (default)
FIXFormat and apply lint fixes (removes unused loads, etc.)

Format all BUILD and .bzl files with default settings:

transformations = [
format.buildifier(),
]

This matches:

  • All *.bzl files
  • All BUILD files (at any directory level)
  • The root BUILD file

Format and apply Buildifier lint fixes:

transformations = [
format.buildifier(lint = "FIX"),
]

Common fixes applied by lint = "FIX":

  • Remove unused load() statements
  • Sort and deduplicate load statements
  • Fix common style issues

Format only specific directories:

transformations = [
format.buildifier(
paths = glob(
include = ["foo/BUILD", "foo/**/BUILD"],
exclude = ["foo/bar/BUILD"],
),
),
]

This formats all BUILD files inside foo/ except for foo/bar/BUILD.

Enable specific lint warnings:

transformations = [
format.buildifier(
lint = "FIX",
lint_warnings = [
"attr-cfg",
"attr-license",
"attr-non-empty",
"attr-output-default",
"attr-single-file",
"constant-glob",
"ctx-actions",
"depset-iteration",
"depset-union",
"dict-concatenation",
"duplicated-name",
"filetype",
"git-repository",
"http-archive",
"integer-division",
"load",
"load-on-top",
"native-build",
"native-package",
"out-of-order-load",
"output-group",
"package-name",
"package-on-top",
"positional-args",
"redefined-variable",
"repository-name",
"same-origin-load",
"string-iteration",
"unused-variable",
],
),
]

Format Bazel files as part of an open-sourcing workflow:

core.workflow(
name = "export",
origin = git.origin(
url = "https://internal.example.com/repo",
ref = "main",
),
destination = git.destination(
url = "https://github.com/org/public-repo",
push = "main",
),
origin_files = glob(["**"], exclude = ["**/internal/**"]),
authoring = authoring.pass_thru("Bot <bot@example.com>"),
transformations = [
# Remove internal paths
core.remove(glob(["**/internal/**"])),
# Format all Bazel files
format.buildifier(lint = "FIX"),
# Update internal references
core.replace(
before = "@internal//",
after = "@public//",
paths = glob(["**.bzl", "**/BUILD"]),
),
],
)

Use Buildifier in a validation-only context:

core.workflow(
name = "format_check",
origin = git.origin(
url = "https://github.com/org/repo",
ref = "main",
),
destination = folder.destination(),
authoring = authoring.overwrite("CI <ci@example.com>"),
transformations = [
format.buildifier(),
],
)

Run with --dry-run to check if files need formatting without modifying them.


Buildifier behavior can be configured via CLI flags:

FlagDescription
--buildifier-pathPath to buildifier binary
--buildifier-batch-sizeFiles to process per batch (default: 100)

Example:

Terminal window
copybara migrate copy.bara.sky workflow \
--buildifier-path=/usr/local/bin/buildifier

Buildifier must be installed and accessible. Install it using:

Terminal window
# Using go
go install github.com/bazelbuild/buildtools/buildifier@latest
# Using Homebrew (macOS)
brew install buildifier
# Using apt (Debian/Ubuntu)
apt install buildifier