feat(builtins): add buildifier format and lint built-ins#896
Conversation
Add buildifier as two built-in linters for Bazel/Starlark files: - buildifier_format: formatting check/fix using -mode=check - buildifier_lint: lint warnings + formatting using -lint=warn/-lint=fix Covers BUILD, BUILD.bazel, WORKSPACE, WORKSPACE.bazel, MODULE.bazel, *.bzl, *.star, and *.sky files.
There was a problem hiding this comment.
Code Review
This pull request introduces built-in formatting and linting steps for Bazel using the buildifier tool, along with a corresponding tool stub. The review feedback identifies a logical error in the linting test case, which incorrectly expects buildifier to resolve missing load statements. Additionally, the reviewer suggests expanding the file glob patterns to include more Bazel-specific extensions and subdirectories, adding check_diff commands for better efficiency, and correcting a version typo in the tool stub.
| local const before = | ||
| """ | ||
| cc_library( | ||
| name = "foo", | ||
| srcs = ["Foo.cc"], | ||
| ) | ||
|
|
||
| """ | ||
| local const after = | ||
| """ | ||
| load("@rules_cc//cc:cc_library.bzl", "cc_library") | ||
|
|
||
| cc_library( | ||
| name = "foo", | ||
| srcs = ["Foo.cc"], | ||
| ) | ||
|
|
||
| """ |
There was a problem hiding this comment.
This test case expects buildifier -lint=fix to automatically add a missing load statement. However, buildifier is a formatter and linter; it does not have the capability to resolve and add missing dependencies (that is typically handled by tools like gazelle). This test will likely fail as the fix command won't produce the expected after content.
Consider using a fixable lint issue such as out-of-order-load or unsorted-dict-items for this test.
There was a problem hiding this comment.
What's actually going on here is that:
- bazel has traditionally had built-in rules for C-family languages
- as built-ins, these did not need to be loaded—they were just "always there"
- recent bazel versions have migrated these built-in rules to external rules (that must then be loaded)
- the built-ins are maintained for backwards-compatibility, but the external, loadable rules are preferred
- there's a rule in buildifer that spots use of the legacy built-ins and adds any necessary load statements
Or, tl;dr: this test example looks like a (nonexistant) generic automatic-import-finder thing, but is just a narrow-purpose rule targeting a list of known rules.
| glob = List( | ||
| "**/BUILD", | ||
| "**/BUILD.bazel", | ||
| "WORKSPACE", | ||
| "WORKSPACE.bazel", | ||
| "MODULE.bazel", | ||
| "**/*.bzl", | ||
| "**/*.star", | ||
| "**/*.sky" | ||
| ) |
There was a problem hiding this comment.
The glob list is missing some common Bazel/Starlark file patterns and some entries are restricted to the root directory. It is better to include **/WORKSPACE, **/MODULE.bazel, **/REPO.bazel, and **/*.BUILD to ensure all relevant files are covered regardless of their location in the repository.
glob = List(
"**/BUILD",
"**/BUILD.bazel",
"**/WORKSPACE",
"**/WORKSPACE.bazel",
"**/MODULE.bazel",
"**/REPO.bazel",
"**/*.bzl",
"**/*.star",
"**/*.sky",
"**/*.BUILD"
)
There was a problem hiding this comment.
This and the other similar comment are a good idea—will expand the list.
| "**/*.star", | ||
| "**/*.sky" | ||
| ) | ||
| check = "buildifier -mode=check {{ files }}" |
There was a problem hiding this comment.
There was a problem hiding this comment.
This and the other similar comment are a good idea—will investigate and update.
| glob = List( | ||
| "**/BUILD", | ||
| "**/BUILD.bazel", | ||
| "WORKSPACE", | ||
| "WORKSPACE.bazel", | ||
| "MODULE.bazel", | ||
| "**/*.bzl", | ||
| "**/*.star", | ||
| "**/*.sky" | ||
| ) |
There was a problem hiding this comment.
The glob list should be expanded to include all relevant Bazel and Starlark files, matching the improvements suggested for the formatter builtin.
glob = List(
"**/BUILD",
"**/BUILD.bazel",
"**/WORKSPACE",
"**/WORKSPACE.bazel",
"**/MODULE.bazel",
"**/REPO.bazel",
"**/*.bzl",
"**/*.star",
"**/*.sky",
"**/*.BUILD"
)
| "**/*.star", | ||
| "**/*.sky" | ||
| ) | ||
| check = "buildifier -mode=check -lint=warn {{ files }}" |
There was a problem hiding this comment.
| @@ -0,0 +1,4 @@ | |||
| #!/usr/bin/env -S mise tool-stub | |||
|
|
|||
| version = "8.5.1" | |||
There was a problem hiding this comment.
I suspect this is an LLM hallucination because I can literally verify that there's an 8.5.1 version of buildifier available here.
Greptile SummaryThis PR adds Confidence Score: 5/5Safe to merge — no logic errors or behavioral bugs found. All commands match buildifier's documented interface, exit codes in tests are correct (4 for both formatting and lint issues in warn mode), and the implementation cleanly mirrors the established buf_format/buf_lint pattern. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Changed Starlark files\n BUILD / .bzl / .star etc.] --> B{Step type}
B -->|buildifier_format check| C["buildifier -mode=check {{ files }}"]
B -->|buildifier_format fix| D["buildifier {{ files }}"]
B -->|buildifier_lint check| E["buildifier -mode=check -lint=warn {{ files }}"]
B -->|buildifier_lint fix| F["buildifier -lint=fix {{ files }}"]
C -->|exit 0| G[Files already formatted ✓]
C -->|exit 4| H[Formatting needed ✗]
D -->|exit 0| I[Files reformatted ✓]
E -->|exit 0| J[No format or lint issues ✓]
E -->|exit 4| K[Format or lint issues ✗]
F -->|exit 0| L[Lint issues fixed ✓]
Reviews (2): Last reviewed commit: "[autofix.ci] apply automated fixes" | Re-trigger Greptile |
### 🚀 Features - **(builtins)** add `buildifier` format and lint built-ins by [@plx](https://github.com/plx) in [#896](#896) ### 🐛 Bug Fixes - **(step)** only auto-batch when rendered command exceeds ARG_MAX by [@jdx](https://github.com/jdx) in [#901](#901) ### 📚 Documentation - thank Namespace for GitHub Actions runner support by [@jdx](https://github.com/jdx) in [#895](#895) ### 🔍 Other Changes - **(ci)** use !cancelled() instead of always() for final job by [@jdx](https://github.com/jdx) in [#906](#906) - **(docs)** remove shrill.en.dev analytics script by [@jdx](https://github.com/jdx) in [#903](#903) - remove rust-cache from release jobs by [@jdx](https://github.com/jdx) in [#893](#893) - invert CLAUDE.md/AGENTS.md so AGENTS.md is canonical by [@jdx](https://github.com/jdx) in [#905](#905) - set dev profile debug to 1 by [@jdx](https://github.com/jdx) in [#907](#907) ### 📦️ Dependency Updates - update anthropics/claude-code-action digest to fefa07e by [@renovate[bot]](https://github.com/renovate[bot]) in [#897](#897) - update jdx/mise-action digest to 1648a78 by [@renovate[bot]](https://github.com/renovate[bot]) in [#898](#898) - update apple-actions/import-codesign-certs action to v7 by [@renovate[bot]](https://github.com/renovate[bot]) in [#900](#900) - update autofix-ci/action action to v1.3.4 by [@renovate[bot]](https://github.com/renovate[bot]) in [#899](#899) - lock file maintenance by [@renovate[bot]](https://github.com/renovate[bot]) in [#908](#908) ### New Contributors - @plx made their first contribution in [#896](#896) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Low risk release bookkeeping: primarily version string updates across manifests and docs with no functional code changes in this diff. > > **Overview** > Updates the project for the `v1.45.0` release by bumping the crate/CLI version (`Cargo.toml`, `Cargo.lock`, `hk.usage.kdl`, generated CLI docs) and adding the `1.45.0` entry to `CHANGELOG.md`. > > Refreshes documentation and example configs to reference the new versioned Pkl package URLs (`docs/*.md`, `docs/public/*.pkl`, `hk-example.pkl`). > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit cfe2da5. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-authored-by: mise-en-dev <[email protected]>
I added support for
buildifier, which is a tool for formatting and linting starlark (e.g. Bazel'sBUILDfiles,.bzlfiles, and so on).I modeled the two new additions on
buf, which is also present asbuf_formatandbuf_lint.I ran
hk fix --allbefore opening the PR, and have made some exploratory usage of these new built-ins as part of evaluatinghkadoption for a large bazel project.Appreciate your attention (and
hk!), and am happy to make any adjustments you need.