From b7ebef5a258d040712f0114c0144a04bffb3a32c Mon Sep 17 00:00:00 2001
From: Matt Brown <mattbrown@spotify.com>
Date: Mon, 17 Nov 2025 22:23:47 -0500
Subject: [PATCH] add buf, buildifier formatters

---
 .bazelrc                                              |  2 ++
 BUILD.bazel                                           |  6 ++++++
 MODULE.bazel                                          | 11 +++++++++++
 MODULE.bazel.lock                                     |  2 +-
 buf.yaml                                              |  4 ++++
 hello-world/java/BUILD.bazel                          |  2 +-
 hello-world/proto/mattnworb/helloworld/v1/BUILD.bazel |  1 -
 .../proto/mattnworb/helloworld/v1/service.proto       |  4 ++--
 tools/BUILD.bazel                                     |  3 ++-
 tools/format/BUILD.bazel                              |  2 ++
 10 files changed, 31 insertions(+), 6 deletions(-)
 create mode 100644 BUILD.bazel
 create mode 100644 buf.yaml

diff --git a/.bazelrc b/.bazelrc
index 97df590..8e328e9 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -1,2 +1,4 @@
 common --java_language_version=25
 common --java_runtime_version=remotejdk_25
+
+common --experimental_proto_descriptor_sets_include_source_info
diff --git a/BUILD.bazel b/BUILD.bazel
new file mode 100644
index 0000000..892f10d
--- /dev/null
+++ b/BUILD.bazel
@@ -0,0 +1,6 @@
+alias(
+    name = "format",
+    actual = "//tools/format:format",
+)
+
+exports_files(["buf.yaml"])
diff --git a/MODULE.bazel b/MODULE.bazel
index 40a8aab..871e158 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -1,9 +1,18 @@
+# main dependencies
 bazel_dep(name = "grpc-java", version = "1.75.0")
 bazel_dep(name = "protobuf", version = "33.1")
+bazel_dep(name = "rules_buf", version = "0.5.2")
 bazel_dep(name = "rules_java", version = "9.0.3")
 bazel_dep(name = "rules_jvm_external", version = "6.9")
 bazel_dep(name = "rules_multitool", version = "1.9.0")
 
+# rules_buf setup
+buf = use_extension("@rules_buf//buf:extensions.bzl", "buf")
+
+# make `bazel run @rules_buf_toolchains//:buf` available to run buf.
+use_repo(buf, "rules_buf_toolchains")
+
+# rules_jvm_external setup
 maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
 use_repo(maven, "maven")
 
@@ -26,10 +35,12 @@ maven.install(
     strict_visibility = True,
 )
 
+# rules_multitool setup
 multitool = use_extension("@rules_multitool//multitool:extension.bzl", "multitool")
 multitool.hub(lockfile = "//tools:multitool.lock.json")
 use_repo(multitool, "multitool")
 
+# dev dependencies
 bazel_dep(name = "aspect_rules_lint", version = "1.10.2", dev_dependency = True)
 bazel_dep(name = "bazel_env.bzl", version = "0.5.0", dev_dependency = True)
 bazel_dep(name = "buildifier_prebuilt", version = "8.2.1", dev_dependency = True)
diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock
index 8d750a1..66645b2 100644
--- a/MODULE.bazel.lock
+++ b/MODULE.bazel.lock
@@ -1134,7 +1134,7 @@
     "@@rules_buf+//buf:extensions.bzl%buf": {
       "general": {
         "bzlTransitiveDigest": "nziVPz62Pjv2JecWM9NXmhpU/OjKhPz1MT9++4ObLUw=",
-        "usagesDigest": "vxN6C2h72rUERbAmd1476FWpxdxo1NhYoY5JSFXJT3g=",
+        "usagesDigest": "11x2wclsDFQ43OPTO9I925qwQ+m7IX4RP8Q7scyxoqQ=",
         "recordedFileInputs": {},
         "recordedDirentsInputs": {},
         "envVariables": {},
diff --git a/buf.yaml b/buf.yaml
new file mode 100644
index 0000000..056dd75
--- /dev/null
+++ b/buf.yaml
@@ -0,0 +1,4 @@
+version: v2
+lint:
+  use:
+    - STANDARD
diff --git a/hello-world/java/BUILD.bazel b/hello-world/java/BUILD.bazel
index c14c5db..b300b10 100644
--- a/hello-world/java/BUILD.bazel
+++ b/hello-world/java/BUILD.bazel
@@ -9,8 +9,8 @@ java_binary(
         "//hello-world/proto/mattnworb/helloworld/v1:service_java_proto",
         "@grpc-java//api",
         "@grpc-java//netty",
-        "@protobuf//java/core",
         "@maven//:net_sourceforge_argparse4j_argparse4j",
+        "@protobuf//java/core",
     ],
 )
 
diff --git a/hello-world/proto/mattnworb/helloworld/v1/BUILD.bazel b/hello-world/proto/mattnworb/helloworld/v1/BUILD.bazel
index 9319d0e..72c5bd7 100644
--- a/hello-world/proto/mattnworb/helloworld/v1/BUILD.bazel
+++ b/hello-world/proto/mattnworb/helloworld/v1/BUILD.bazel
@@ -22,4 +22,3 @@ java_grpc_library(
     srcs = [":service_proto"],
     deps = [":service_java_proto"],
 )
-
diff --git a/hello-world/proto/mattnworb/helloworld/v1/service.proto b/hello-world/proto/mattnworb/helloworld/v1/service.proto
index d5e452b..ca68236 100644
--- a/hello-world/proto/mattnworb/helloworld/v1/service.proto
+++ b/hello-world/proto/mattnworb/helloworld/v1/service.proto
@@ -4,7 +4,7 @@ package mattnworb.helloworld.v1;
 option java_package = "com.mattnworb.helloworld.v1";
 
 service HelloWorldService {
-  rpc SayHello (SayHelloRequest) returns (SayHelloResponse);
+  rpc SayHello(SayHelloRequest) returns (SayHelloResponse);
 }
 
 message SayHelloRequest {
@@ -13,4 +13,4 @@ message SayHelloRequest {
 
 message SayHelloResponse {
   string message = 1;
-}
\ No newline at end of file
+}
diff --git a/tools/BUILD.bazel b/tools/BUILD.bazel
index ecca5b4..be989ed 100644
--- a/tools/BUILD.bazel
+++ b/tools/BUILD.bazel
@@ -6,9 +6,10 @@ bazel_env(
         "jdk": "@rules_java//toolchains:current_host_java_runtime",
     },
     tools = {
+        "buf": "@rules_buf_toolchains//:buf",
         "buildifier": "@buildifier_prebuilt//:buildifier",
         "format": "//tools/format",
         "jar": "$(JAVABASE)/bin/jar",
         "java": "$(JAVA)",
     },
-)
\ No newline at end of file
+)
diff --git a/tools/format/BUILD.bazel b/tools/format/BUILD.bazel
index 9223d62..63fba5f 100644
--- a/tools/format/BUILD.bazel
+++ b/tools/format/BUILD.bazel
@@ -3,5 +3,7 @@ load("@aspect_rules_lint//format:defs.bzl", "format_multirun")
 format_multirun(
     name = "format",
     java = "@multitool//tools/google-java-format",
+    protocol_buffer = "@rules_buf_toolchains//:buf",
+    starlark = "@buildifier_prebuilt//:buildifier",
     visibility = ["//visibility:public"],
 )
