Skip to content

Commit 9adc381

Browse files
authored
Add support for protoc toolchains (#108)
Adds support for protoc toolchains. This is currently behind a flag `--incompatible_enable_proto_toolchain_resolution` Closes #74
1 parent 466139c commit 9adc381

File tree

22 files changed

+3763
-388
lines changed

22 files changed

+3763
-388
lines changed
File renamed without changes.

MODULE.bazel.lock

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

buf/internal/breaking.bzl

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
1818
load(":plugin.bzl", "protoc_plugin_test")
19+
load("@rules_proto//proto:proto_common.bzl", proto_toolchains = "toolchains")
20+
21+
_PROTO_TOOLCHAIN_TYPE = "@rules_proto//proto:toolchain_type"
1922

2023
_DOC = """
2124
`buf_breaking_test` is a test rule that checks one or more `proto_library` targets for breaking changes.
@@ -40,10 +43,11 @@ def _buf_breaking_test_impl(ctx):
4043
files_to_include = [ctx.file.against]
4144
if ctx.file.config != None:
4245
files_to_include.append(ctx.file.config)
46+
proto_toolchain_enabled = len(proto_toolchains.use_toolchain(_PROTO_TOOLCHAIN_TYPE)) > 0
4347
return protoc_plugin_test(
4448
ctx,
4549
proto_infos,
46-
ctx.executable._protoc,
50+
ctx.toolchains[_PROTO_TOOLCHAIN_TYPE].proto.proto_compiler.executable if proto_toolchain_enabled else ctx.executable._protoc,
4751
ctx.toolchains[_TOOLCHAIN].cli,
4852
config,
4953
files_to_include,
@@ -53,49 +57,49 @@ def _buf_breaking_test_impl(ctx):
5357
buf_breaking_test = rule(
5458
implementation = _buf_breaking_test_impl,
5559
doc = _DOC,
56-
attrs = {
57-
"_protoc": attr.label(
58-
default = "@com_google_protobuf//:protoc",
59-
executable = True,
60-
cfg = "exec",
61-
),
62-
"_windows_constraint": attr.label(
63-
default = "@platforms//os:windows",
64-
),
65-
"targets": attr.label_list(
66-
providers = [ProtoInfo],
67-
doc = """`proto_library` targets to check for breaking changes""",
68-
),
69-
"against": attr.label(
70-
mandatory = True,
71-
allow_single_file = True,
72-
doc = """The image file against which breaking changes are checked.""",
73-
),
74-
"config": attr.label(
75-
allow_single_file = True,
76-
doc = """The `buf.yaml` file""",
77-
),
78-
"module": attr.string(
79-
default = "",
80-
doc = "The module to use in v2 config",
81-
),
82-
"limit_to_input_files": attr.bool(
83-
default = False,
84-
doc = """Checks are limited to input files. If a file gets deleted that will not be caught. Please refer to https://docs.buf.build/breaking/protoc-plugin for more details""",
85-
),
86-
"exclude_imports": attr.bool(
87-
default = True,
88-
doc = """Checks are limited to the source files excluding imports from breaking change detection. Please refer to https://docs.buf.build/breaking/protoc-plugin for more details""",
89-
),
90-
"error_format": attr.string(
91-
default = "",
92-
doc = "error-format flag for buf breaking: https://buf.build/docs/reference/cli/buf/breaking#error-format",
93-
),
94-
"protoc_args": attr.string_list(
95-
default = [],
96-
doc = "Additional arguments to pass to protoc",
97-
),
98-
},
99-
toolchains = [_TOOLCHAIN],
60+
attrs = dict(
61+
{
62+
"_windows_constraint": attr.label(
63+
default = "@platforms//os:windows",
64+
),
65+
"targets": attr.label_list(
66+
providers = [ProtoInfo],
67+
doc = """`proto_library` targets to check for breaking changes""",
68+
),
69+
"against": attr.label(
70+
mandatory = True,
71+
allow_single_file = True,
72+
doc = """The image file against which breaking changes are checked.""",
73+
),
74+
"config": attr.label(
75+
allow_single_file = True,
76+
doc = """The `buf.yaml` file""",
77+
),
78+
"module": attr.string(
79+
default = "",
80+
doc = "The module to use in v2 config",
81+
),
82+
"limit_to_input_files": attr.bool(
83+
default = False,
84+
doc = """Checks are limited to input files. If a file gets deleted that will not be caught. Please refer to https://docs.buf.build/breaking/protoc-plugin for more details""",
85+
),
86+
"exclude_imports": attr.bool(
87+
default = True,
88+
doc = """Checks are limited to the source files excluding imports from breaking change detection. Please refer to https://docs.buf.build/breaking/protoc-plugin for more details""",
89+
),
90+
"error_format": attr.string(
91+
default = "",
92+
doc = "error-format flag for buf breaking: https://buf.build/docs/reference/cli/buf/breaking#error-format",
93+
),
94+
"protoc_args": attr.string_list(
95+
default = [],
96+
doc = "Additional arguments to pass to protoc",
97+
),
98+
},
99+
**proto_toolchains.if_legacy_toolchain({
100+
"_protoc": attr.label(default = "@com_google_protobuf//:protoc", executable = True, cfg = "exec"),
101+
})
102+
),
103+
toolchains = [_TOOLCHAIN] + proto_toolchains.use_toolchain(_PROTO_TOOLCHAIN_TYPE),
100104
test = True,
101105
)

buf/internal/dependencies.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ bazel_dependencies = {
2626
],
2727
},
2828
"rules_proto": {
29-
"sha256": "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd",
30-
"strip_prefix": "rules_proto-5.3.0-21.7",
29+
"sha256": "0e5c64a2599a6e26c6a03d6162242d231ecc0de219534c38cb4402171def21e8",
30+
"strip_prefix": "rules_proto-7.0.2",
3131
"urls": [
32-
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz",
32+
"https://github.com/bazelbuild/rules_proto/releases/download/7.0.2/rules_proto-7.0.2.tar.gz",
3333
],
3434
},
3535
}

buf/internal/lint.bzl

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
1818
load(":plugin.bzl", "protoc_plugin_test")
19+
load("@rules_proto//proto:proto_common.bzl", proto_toolchains = "toolchains")
20+
21+
_PROTO_TOOLCHAIN_TYPE = "@rules_proto//proto:toolchain_type"
1922

2023
_DOC = """
2124
`buf_lint_test` is a test rule that lints one or more `proto_library` targets.
@@ -37,10 +40,11 @@ def _buf_lint_test_impl(ctx):
3740
files_to_include = []
3841
if ctx.file.config != None:
3942
files_to_include.append(ctx.file.config)
43+
proto_toolchain_enabled = len(proto_toolchains.use_toolchain(_PROTO_TOOLCHAIN_TYPE)) > 0
4044
return protoc_plugin_test(
4145
ctx,
4246
proto_infos,
43-
ctx.executable._protoc,
47+
ctx.toolchains[_PROTO_TOOLCHAIN_TYPE].proto.proto_compiler.executable if proto_toolchain_enabled else ctx.executable._protoc,
4448
ctx.toolchains[_TOOLCHAIN].cli,
4549
config,
4650
files_to_include,
@@ -50,37 +54,37 @@ def _buf_lint_test_impl(ctx):
5054
buf_lint_test = rule(
5155
implementation = _buf_lint_test_impl,
5256
doc = _DOC,
53-
attrs = {
54-
"_protoc": attr.label(
55-
default = "@com_google_protobuf//:protoc",
56-
executable = True,
57-
cfg = "exec",
58-
),
59-
"_windows_constraint": attr.label(
60-
default = "@platforms//os:windows",
61-
),
62-
"targets": attr.label_list(
63-
providers = [ProtoInfo],
64-
mandatory = True,
65-
doc = "`proto_library` targets that should be linted",
66-
),
67-
"config": attr.label(
68-
allow_single_file = True,
69-
doc = "The `buf.yaml` file",
70-
),
71-
"module": attr.string(
72-
default = "",
73-
doc = "The module to use in v2 config",
74-
),
75-
"error_format": attr.string(
76-
default = "",
77-
doc = "error-format flag for buf lint: https://buf.build/docs/reference/cli/buf/lint#error-format",
78-
),
79-
"protoc_args": attr.string_list(
80-
default = [],
81-
doc = "Additional arguments to pass to protoc",
82-
),
83-
},
84-
toolchains = [_TOOLCHAIN],
57+
attrs = dict(
58+
{
59+
"_windows_constraint": attr.label(
60+
default = "@platforms//os:windows",
61+
),
62+
"targets": attr.label_list(
63+
providers = [ProtoInfo],
64+
mandatory = True,
65+
doc = "`proto_library` targets that should be linted",
66+
),
67+
"config": attr.label(
68+
allow_single_file = True,
69+
doc = "The `buf.yaml` file",
70+
),
71+
"module": attr.string(
72+
default = "",
73+
doc = "The module to use in v2 config",
74+
),
75+
"error_format": attr.string(
76+
default = "",
77+
doc = "error-format flag for buf lint: https://buf.build/docs/reference/cli/buf/lint#error-format",
78+
),
79+
"protoc_args": attr.string_list(
80+
default = [],
81+
doc = "Additional arguments to pass to protoc",
82+
),
83+
},
84+
**proto_toolchains.if_legacy_toolchain({
85+
"_protoc": attr.label(default = "@com_google_protobuf//:protoc", executable = True, cfg = "exec"),
86+
})
87+
),
88+
toolchains = [_TOOLCHAIN] + proto_toolchains.use_toolchain(_PROTO_TOOLCHAIN_TYPE),
8589
test = True,
8690
)

examples/bzlmod/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
bazel_dep(name = "rules_buf", version = "0.0.0", dev_dependency = True)
1818
bazel_dep(name = "gazelle", version = "0.34.0", dev_dependency = True)
19+
1920
bazel_dep(name = "protobuf", version = "29.0", repo_name = "com_google_protobuf")
2021
bazel_dep(name = "rules_proto", version = "7.0.2")
2122

0 commit comments

Comments
 (0)