Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions pkl/builtins/buildifier_format.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import "../Builtins.pkl"
import "../Config.pkl"
import "./test/helpers.pkl"

@Builtins.meta {
category = "Build Systems"
description = "Formatter for Bazel BUILD, WORKSPACE, and Starlark files"
project_indicators {
new { file = "WORKSPACE" }
new { file = "WORKSPACE.bazel" }
new { file = "MODULE.bazel" }
new { glob = "**/BUILD.bazel" }
}
}
buildifier_format = new Config.Step {
glob =
List(
"**/BUILD",
"**/BUILD.bazel",
"WORKSPACE",
"WORKSPACE.bazel",
"MODULE.bazel",
"**/*.bzl",
"**/*.star",
"**/*.sky",
)
check = "buildifier -mode=check {{ files }}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Adding check_diff allows hk to use git apply for applying formatting changes. This is generally more efficient and safer than running the full fix command when only formatting changes are needed.

  check = "buildifier -mode=check {{ files }}"
  check_diff = "buildifier -mode=diff {{ files }}"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the other similar comment are a good idea—will investigate and update.

fix = "buildifier {{ files }}"
tests {
local const testMaker = new helpers.TestMaker { filename = "BUILD.bazel" }
local const before =
"""
load("@rules_cc//cc:cc_library.bzl", "cc_library")

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"],
)

"""
["check bad file"] = testMaker.checkFail(before, 4)
["check good file"] = testMaker.checkPass(after)
["fix bad file"] = testMaker.fixPass(before, after)
["fix good file"] = testMaker.fixPass(after, after)
}
}
54 changes: 54 additions & 0 deletions pkl/builtins/buildifier_lint.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import "../Builtins.pkl"
import "../Config.pkl"
import "./test/helpers.pkl"

@Builtins.meta {
category = "Build Systems"
description = "Linter for Bazel BUILD, WORKSPACE, and Starlark files"
project_indicators {
new { file = "WORKSPACE" }
new { file = "WORKSPACE.bazel" }
new { file = "MODULE.bazel" }
new { glob = "**/BUILD.bazel" }
}
}
buildifier_lint = new Config.Step {
glob =
List(
"**/BUILD",
"**/BUILD.bazel",
"WORKSPACE",
"WORKSPACE.bazel",
"MODULE.bazel",
"**/*.bzl",
"**/*.star",
"**/*.sky",
)
check = "buildifier -mode=check -lint=warn {{ files }}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Adding check_diff with -lint=warn allows hk to apply both formatting and lint fixes via patches, which is more efficient than running the fix command.

  check = "buildifier -mode=check -lint=warn {{ files }}"
  check_diff = "buildifier -mode=diff -lint=warn {{ files }}"

fix = "buildifier -lint=fix {{ files }}"
Comment thread
greptile-apps[bot] marked this conversation as resolved.
tests {
local const testMaker = new helpers.TestMaker { filename = "BUILD.bazel" }
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"],
)

"""
Comment on lines +31 to +48

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's actually going on here is that:

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.

["check bad file"] = testMaker.checkFail(before, 4)
["check good file"] = testMaker.checkPass(after)
["fix bad file"] = testMaker.fixPass(before, after)
["fix good file"] = testMaker.fixPass(after, after)
}
}
4 changes: 4 additions & 0 deletions test/builtin_tool_stubs/buildifier
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env -S mise tool-stub

version = "8.5.1"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The version 8.5.1 appears to be a typo. The current latest stable version of buildifier (from the bazelbuild/buildtools repository) is 8.0.1. Using a non-existent version will cause installation failures in tool managers like mise.

version = "8.0.1"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tool = "buildifier"
Loading