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
7 changes: 7 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ jobs:
(cd "${dir}" && bazel build //...)
echo "::endgroup::"
done
- name: Build examples with proto toolchain resolution
run: |
for dir in examples/bzlmod/ examples/echo/; do
echo "::group::bazel build in ${dir}"
(cd "${dir}" && bazel build //... --incompatible_enable_proto_toolchain_resolution)
echo "::endgroup::"
done
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module(

bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "protobuf", version = "29.1", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_proto", version = "7.0.2")
bazel_dep(name = "rules_proto", version = "7.1.0")
bazel_dep(name = "rules_go", version = "0.59.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "bazel_skylib", version = "1.7.1")

Expand Down
3 changes: 2 additions & 1 deletion MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# rules_buf

Bazel rules for [Buf](https://buf.build/). The rules work alongside the `proto_library` rule of [rules_proto](https://github.com/bazelbuild/rules_proto).
Bazel rules for [Buf](https://buf.build/). The rules work alongside the `proto_library` rule of [protobuf](https://github.com/protocolbuffers/protobuf/tree/main/bazel).

## Status

Expand Down
12 changes: 10 additions & 2 deletions buf/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ bzl_library(
srcs = ["breaking.bzl"],
deps = [
":plugin",
"@rules_proto//proto:defs",
":proto_toolchains",
"@com_google_protobuf//bazel/common:proto_info_bzl",
],
)

Expand All @@ -35,7 +36,8 @@ bzl_library(
srcs = ["lint.bzl"],
deps = [
":plugin",
"@rules_proto//proto:defs",
":proto_toolchains",
"@com_google_protobuf//bazel/common:proto_info_bzl",
],
)

Expand All @@ -44,6 +46,12 @@ bzl_library(
srcs = ["plugin.bzl"],
)

bzl_library(
name = "proto_toolchains",
srcs = ["proto_toolchains.bzl"],
deps = ["@com_google_protobuf//bazel/common:proto_common_bzl"],
)

bzl_library(
name = "dependencies",
srcs = ["dependencies.bzl"],
Expand Down
12 changes: 5 additions & 7 deletions buf/internal/breaking.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@

"""Defines buf_breaking_test rule"""

load("@rules_proto//proto:defs.bzl", "ProtoInfo")
load("@rules_proto//proto:proto_common.bzl", proto_toolchains = "toolchains")
load("@com_google_protobuf//bazel/common:proto_info.bzl", "ProtoInfo")
load(":plugin.bzl", "protoc_plugin_test")

_PROTO_TOOLCHAIN_TYPE = "@rules_proto//proto:toolchain_type"
load(":proto_toolchains.bzl", "PROTO_TOOLCHAIN_TYPE", "proto_toolchains")

_DOC = """
`buf_breaking_test` is a test rule that checks one or more `proto_library` targets for breaking changes.
Expand All @@ -43,11 +41,11 @@ def _buf_breaking_test_impl(ctx):
files_to_include = [ctx.file.against]
if ctx.file.config != None:
files_to_include.append(ctx.file.config)
proto_toolchain_enabled = len(proto_toolchains.use_toolchain(_PROTO_TOOLCHAIN_TYPE)) > 0
proto_toolchain_enabled = len(proto_toolchains.use_toolchain(PROTO_TOOLCHAIN_TYPE)) > 0
return protoc_plugin_test(
ctx,
proto_infos,
ctx.toolchains[_PROTO_TOOLCHAIN_TYPE].proto.proto_compiler.executable if proto_toolchain_enabled else ctx.executable._protoc,
ctx.toolchains[PROTO_TOOLCHAIN_TYPE].proto.proto_compiler.executable if proto_toolchain_enabled else ctx.executable._protoc,
ctx.toolchains[_TOOLCHAIN].cli,
config,
files_to_include,
Expand Down Expand Up @@ -100,6 +98,6 @@ buf_breaking_test = rule(
"_protoc": attr.label(default = "@com_google_protobuf//:protoc", executable = True, cfg = "exec"),
})
),
toolchains = [_TOOLCHAIN] + proto_toolchains.use_toolchain(_PROTO_TOOLCHAIN_TYPE),
toolchains = [_TOOLCHAIN] + proto_toolchains.use_toolchain(PROTO_TOOLCHAIN_TYPE),
test = True,
)
12 changes: 5 additions & 7 deletions buf/internal/lint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@

"""Defines buf_lint_test rule"""

load("@rules_proto//proto:defs.bzl", "ProtoInfo")
load("@rules_proto//proto:proto_common.bzl", proto_toolchains = "toolchains")
load("@com_google_protobuf//bazel/common:proto_info.bzl", "ProtoInfo")
load(":plugin.bzl", "protoc_plugin_test")

_PROTO_TOOLCHAIN_TYPE = "@rules_proto//proto:toolchain_type"
load(":proto_toolchains.bzl", "PROTO_TOOLCHAIN_TYPE", "proto_toolchains")

_DOC = """
`buf_lint_test` is a test rule that lints one or more `proto_library` targets.
Expand All @@ -40,11 +38,11 @@ def _buf_lint_test_impl(ctx):
files_to_include = []
if ctx.file.config != None:
files_to_include.append(ctx.file.config)
proto_toolchain_enabled = len(proto_toolchains.use_toolchain(_PROTO_TOOLCHAIN_TYPE)) > 0
proto_toolchain_enabled = len(proto_toolchains.use_toolchain(PROTO_TOOLCHAIN_TYPE)) > 0
return protoc_plugin_test(
ctx,
proto_infos,
ctx.toolchains[_PROTO_TOOLCHAIN_TYPE].proto.proto_compiler.executable if proto_toolchain_enabled else ctx.executable._protoc,
ctx.toolchains[PROTO_TOOLCHAIN_TYPE].proto.proto_compiler.executable if proto_toolchain_enabled else ctx.executable._protoc,
ctx.toolchains[_TOOLCHAIN].cli,
config,
files_to_include,
Expand Down Expand Up @@ -85,6 +83,6 @@ buf_lint_test = rule(
"_protoc": attr.label(default = "@com_google_protobuf//:protoc", executable = True, cfg = "exec"),
})
),
toolchains = [_TOOLCHAIN] + proto_toolchains.use_toolchain(_PROTO_TOOLCHAIN_TYPE),
toolchains = [_TOOLCHAIN] + proto_toolchains.use_toolchain(PROTO_TOOLCHAIN_TYPE),
test = True,
)
39 changes: 39 additions & 0 deletions buf/internal/proto_toolchains.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2021-2025 Buf Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Proto toolchain resolution helpers"""

load("@com_google_protobuf//bazel/common:proto_common.bzl", "proto_common")

# Starting with rules_proto 7.1.0, @rules_proto//proto:toolchain_type is an
# alias for this type, so toolchains registered against it still resolve.
PROTO_TOOLCHAIN_TYPE = "@com_google_protobuf//bazel/private:proto_toolchain_type"

# Copied from protobuf's private toolchain_helpers.bzl.
_toolchain_resolution_enabled = getattr(proto_common, "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION", False)

def _use_toolchain(toolchain_type):
if _toolchain_resolution_enabled:
return [config_common.toolchain_type(toolchain_type, mandatory = False)]
return []

def _if_legacy_toolchain(legacy_attr_dict):
if _toolchain_resolution_enabled:
return {}
return legacy_attr_dict

proto_toolchains = struct(
use_toolchain = _use_toolchain,
if_legacy_toolchain = _if_legacy_toolchain,
)
3 changes: 2 additions & 1 deletion examples/bzlmod/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/echo/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/unused/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading