rustfmt_test.bzl

rustfmt_test

load("@rules_rust//rust:rustfmt_test.bzl", "rustfmt_test")

rustfmt_test(name, platform, targets, transitive)

A test rule that runs rustfmt --check over a set of Rust targets.

By default (transitive = False), only the exact targets listed are checked. Set transitive = True to walk deps, proc_macro_deps, and crate so that listing a top-level target checks its whole crate graph.

The rustfmt actions run during the build phase, so a formatting failure fails bazel test before the test executable is invoked. The rule also exposes the collected markers under the rustfmt_checks output group, so bazel build //x:my_fmt_test --output_groups=rustfmt_checks drives the rustfmt actions without running the test.

An optional platform attribute transitions targets to the given platform before running rustfmt.

Example:

load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rustfmt_test")

rust_library(
    name = "lib",
    srcs = ["src/lib.rs"],
    edition = "2021",
)

rust_binary(
    name = "app",
    srcs = ["src/main.rs"],
    edition = "2021",
    deps = [":lib"],
)

rustfmt_test(
    name = "fmt_app_only_test",
    targets = [":app"]
)

rustfmt_test(
    name = "fmt_tree_test",
    targets = [":app"],
    transitive = True,
)

Targets tagged no_format, no_rustfmt, or norustfmt are skipped.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
platformOptional platform to transition targets to before running the aspect. When set, --platforms is switched to this label for the duration of this rule's aspect actions.LabeloptionalNone
targetsRust targets to run rustfmt --check on.List of labelsoptional[]
transitiveIf True, lint targets and every crate reachable via deps, proc_macro_deps, and crate. If False, lint only the exact targets listed.BooleanoptionalFalse