Skip to content

Add buf_format rule #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2025
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
1 change: 1 addition & 0 deletions buf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bzl_library(
srcs = ["defs.bzl"],
deps = [
"//buf/internal:breaking",
"//buf/internal:format",
"//buf/internal:lint",
"//buf/internal:repo",
],
Expand Down
2 changes: 2 additions & 0 deletions buf/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Use [gazelle](/gazelle/buf) to auto generate all of these rules based on `buf.ya
load("//buf/internal:breaking.bzl", _buf_breaking_test = "buf_breaking_test")
load("//buf/internal:lint.bzl", _buf_lint_test = "buf_lint_test")
load("//buf/internal:repo.bzl", _buf_dependencies = "buf_dependencies")
load("//buf/internal:format.bzl", _buf_format = "buf_format")

buf_format = _buf_format
buf_dependencies = _buf_dependencies

def buf_breaking_test(timeout = "short", **kwargs):
Expand Down
5 changes: 5 additions & 0 deletions buf/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

package(default_visibility = ["//:__subpackages__"])

bzl_library(
name = "format",
srcs = ["format.bzl"],
)

bzl_library(
name = "breaking",
srcs = ["breaking.bzl"],
Expand Down
38 changes: 38 additions & 0 deletions buf/internal/format.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
_DOC = """
buf_format rule formats Protobuf files.
"""

_TEMPLATE = """
buf=$(readlink "{}")
if ! cd "$BUILD_WORKSPACE_DIRECTORY"; then
echo "Unable to change to workspace (BUILD_WORKSPACE_DIRECTORY: $BUILD_WORKSPACE_DIRECTORY)"
exit 1
fi

$buf format {} .
"""

_TOOLCHAIN = str(Label("//tools/buf:toolchain_type"))

def _buf_format_impl(ctx):
ctx.actions.write(
output = ctx.outputs.executable,
is_executable = True,
content = _TEMPLATE.format(ctx.toolchains[_TOOLCHAIN].cli.short_path, "-w"),
)

return [
DefaultInfo(
runfiles = ctx.runfiles(
files = [ctx.toolchains[_TOOLCHAIN].cli],
),
),
]

buf_format = rule(
implementation = _buf_format_impl,
doc = _DOC,
attrs = {},
toolchains = [_TOOLCHAIN],
executable = True,
)
6 changes: 5 additions & 1 deletion examples/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_buf//buf:defs.bzl", "buf_lint_test")
load("@rules_buf//buf:defs.bzl", "buf_format", "buf_lint_test")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@gazelle//:def.bzl", "gazelle", "gazelle_binary")

Expand Down Expand Up @@ -51,3 +51,7 @@ buf_lint_test(
config = "//:buf.yaml",
targets = [":root_proto"],
)

buf_format(
name = "buf_format",
)
2 changes: 1 addition & 1 deletion examples/bzlmod/unused.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";
syntax = "proto3";