Formatting and Linting
Workflows recommends using rules_lint
to setup linting and formatting rules.
Follow the instructions in that repository to install it.
Formatting
The simplest configuration for a format check looks like this:
tasks:
- format:
Workflows will run bazel run //:format
and verify that no changes are made to any source files.
If changes are made, the task will fail, and will recommend that the developer remediate it by running bazel run //:format
.
Support for linting is coming in a future Workflows release.
Warning vs Error
Getting a red CI for formatting nits can be frustrating for developers, as there is nothing wrong with the code. Formatting is fast enough to be performed as a pre-commit hook, which ensures files are formatted before the CI system sees them. See Install as a pre-commit hook for information on setting this up.
If you have a pre-commit hook then we recommend setting this to be a warning instead, using soft_fail
:
tasks:
- format:
soft_fail: true
Specify targets
The two format targets may be customized in the configuration:
tasks:
- format:
# Workflows will 'bazel run' this target to format the files
target: //path/to:format-check
# Developers will be asked to 'bazel run' this target to format their files
fix_target: //path/to:formater
Linting
A lint
task will be included in the next release of Aspect Workflows.
Buildifier
Buildifier is a formatter and linter for Bazel code (BUILD
and *.bzl
files).
Buildifier is a special case, because rules_lint only runs linters over *_library
targets,
while BUILD
files are not included in any such target.
The simplest configuration looks like this:
tasks:
- buildifier:
Workflows will bazel run //:buildifier.check
, and it exits non-zero, then the task will fail.
It will print the suggested changes that the developer should apply to fix it, by running
bazel run //:buildifier
.
Specify targets
You can choose specific targets to use for running Buildifier:
tasks:
- buildifier:
# Workflows will 'bazel run' this target to verify that buildifier is "happy"
target: //path/to:buildifier_check
# Developers will be asked to 'bazel run' this target to apply Buildifier suggestions
fix_target: //path/to:buildifier
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
.