Skip to content

Commit b5f3d90

Browse files
authored
[ABLD-174] Compose buildifier from upstream & multitool (#41336)
### Motivation Currently, `buildifier` targets don't work out of the box on Windows - credits to @ofek for reporting. Sadly, it turns out that two separate attempts have already been made to remedy this: 1. In the upstream `bazelbuild/buildtools` (`go` source, but only `workspace`-suitable): bazelbuild/buildtools#1230 (merged) 2. `keith/buildifier-prebuilt` (`bzlmod`-suitable, and providing -slightly outdated- `go` binaries published by `bazelbuild/buildtools`): keith/buildifier-prebuilt#89 (well tested, but pending CI feedback... for months!) ### What does this PR do? Inspired by some of @aiuto's [tips](https://datadoghq.atlassian.net/browse/ABLD-174?focusedCommentId=2644283): > [1.b.] instead of pointing to the toolchain type defined in buildifier_prebuilt, point to buildifier via multitools. > [2.a.] revert prebuilt_buildtools.json to 5fd6373 The present change proposes the "least intrusive" change so that: - we don't need to start a third effort (own rules, `.bash` and `.bat` scripts), - we benefit from the validated and merged upstream behavior, - we don't depend on the goodwill of `keith/buildifier-prebuilt` to publish the version `8.2.1` of the binaries (nor subsequent versions), - we don't have to manage a complex patch, but instead simply inhibit the upstream lines that we do not need, given `buildifier` implementations are provided by `multitool` (again). In short, this change combines the best of both worlds through a simple indirection made possible by the configurability of upstream rules which, unlike `keith/buildifier-prebuilt`, allow us to define which implementation of `buildifier` to use. ### Describe how you validated your changes I used another branch to verify this works under Windows as well: https://github.com/DataDog/datadog-agent/blob/026042234ee811fd1641a2417893c08008860186/.gitlab/bazel/build-deps.yaml#L39
1 parent 453e513 commit b5f3d90

File tree

5 files changed

+225
-4
lines changed

5 files changed

+225
-4
lines changed

MODULE.bazel

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@ bazel_dep(name = "rules_pkg", version = "1.1.0")
1111
## Prebuilt binaries ##
1212
#########################
1313

14-
bazel_dep(name = "buildifier_prebuilt", version = "8.2.0.2", dev_dependency = True)
14+
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
15+
16+
http_archive(
17+
name = "com_github_bazelbuild_buildtools",
18+
patch_strip = 1,
19+
patches = ["//bazel/patches:buildifier.patch"],
20+
sha256 = "53119397bbce1cd7e4c590e117dcda343c2086199de62932106c80733526c261",
21+
strip_prefix = "buildtools-8.2.1",
22+
urls = ["https://github.com/bazelbuild/buildtools/archive/refs/tags/v8.2.1.tar.gz"],
23+
)
1524

1625
bazel_dep(name = "rules_multitool", version = "1.9.0")
1726

MODULE.bazel.lock

Lines changed: 140 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bazel/buildifier/BUILD.bazel

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@buildifier_prebuilt//:rules.bzl", "buildifier", "buildifier_test")
1+
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier", "buildifier_test")
22

33
exclude_patterns = [
44
"./.bazelbsp/**",
@@ -7,6 +7,7 @@ exclude_patterns = [
77

88
buildifier(
99
name = "buildifier",
10+
buildifier = "@multitool//tools/buildifier",
1011
exclude_patterns = exclude_patterns,
1112
lint_mode = "fix",
1213
mode = "fix",
@@ -19,6 +20,7 @@ buildifier(
1920
# - requires additional arguments: `no_sandbox` and `workspace`
2021
buildifier_test(
2122
name = "test",
23+
buildifier = "@multitool//tools/buildifier",
2224
exclude_patterns = exclude_patterns,
2325
lint_mode = "warn",
2426
lint_warnings = ["-module-docstring"],

bazel/patches/buildifier.patch

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
diff --git a/buildifier/BUILD.bazel b/buildifier/BUILD.bazel
2+
index 1f1cd67..2f2a5b1 100644
3+
--- a/buildifier/BUILD.bazel
4+
+++ b/buildifier/BUILD.bazel
5+
@@ -1,3 +1,4 @@
6+
+INHIBITED_IN_FAVOR_OF_PREBUILT = """
7+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
8+
load("@rules_shell//shell:sh_test.bzl", "sh_test")
9+
10+
@@ -108,6 +109,7 @@ go_library(
11+
"//wspace",
12+
],
13+
)
14+
+"""
15+
16+
exports_files(
17+
[

bazel/prebuilt_buildtools.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,59 @@
11
{
2+
"buildifier": {
3+
"version": "v8.2.1",
4+
"binaries": [
5+
{
6+
"kind": "file",
7+
"url": "https://github.com/bazelbuild/buildtools/releases/download/v8.2.1/buildifier-darwin-amd64",
8+
"sha256": "9f8cffceb82f4e6722a32a021cbc9a5344b386b77b9f79ee095c61d087aaea06",
9+
"os": "macos",
10+
"cpu": "x86_64",
11+
"size": 7776816,
12+
"published_at": "2025-06-10T13:25:35Z",
13+
"asset_url": "https://api.github.com/repos/bazelbuild/buildtools/releases/assets/262636654"
14+
},
15+
{
16+
"kind": "file",
17+
"url": "https://github.com/bazelbuild/buildtools/releases/download/v8.2.1/buildifier-darwin-arm64",
18+
"sha256": "cfab310ae22379e69a3b1810b433c4cd2fc2c8f4a324586dfe4cc199943b8d5a",
19+
"os": "macos",
20+
"cpu": "arm64",
21+
"size": 7717890,
22+
"published_at": "2025-06-10T13:25:35Z",
23+
"asset_url": "https://api.github.com/repos/bazelbuild/buildtools/releases/assets/262636658"
24+
},
25+
{
26+
"kind": "file",
27+
"url": "https://github.com/bazelbuild/buildtools/releases/download/v8.2.1/buildifier-linux-amd64",
28+
"sha256": "6ceb7b0ab7cf66fceccc56a027d21d9cc557a7f34af37d2101edb56b92fcfa1a",
29+
"os": "linux",
30+
"cpu": "x86_64",
31+
"size": 7882884,
32+
"published_at": "2025-06-10T13:25:35Z",
33+
"asset_url": "https://api.github.com/repos/bazelbuild/buildtools/releases/assets/262636637"
34+
},
35+
{
36+
"kind": "file",
37+
"url": "https://github.com/bazelbuild/buildtools/releases/download/v8.2.1/buildifier-linux-arm64",
38+
"sha256": "3baa1cf7eb41d51f462fdd1fff3a6a4d81d757275d05b2dd5f48671284e9a1a5",
39+
"os": "linux",
40+
"cpu": "arm64",
41+
"size": 7755744,
42+
"published_at": "2025-06-10T13:25:35Z",
43+
"asset_url": "https://api.github.com/repos/bazelbuild/buildtools/releases/assets/262636641"
44+
},
45+
{
46+
"kind": "file",
47+
"url": "https://github.com/bazelbuild/buildtools/releases/download/v8.2.1/buildifier-windows-amd64.exe",
48+
"sha256": "802104da0bcda0424a397ac5be0004c372665a70289a6d5146e652ee497c0dc6",
49+
"os": "windows",
50+
"cpu": "x86_64",
51+
"size": 8020480,
52+
"published_at": "2025-06-10T13:25:35Z",
53+
"asset_url": "https://api.github.com/repos/bazelbuild/buildtools/releases/assets/262636662"
54+
}
55+
]
56+
},
257
"buildozer": {
358
"version": "v8.2.1",
459
"binaries": [

0 commit comments

Comments
 (0)