Skip to content

Commit 8434c12

Browse files
alexeaglecopybara-github
authored andcommitted
fix(bazel/python): incompatible_enable_proto_toolchain_resolution (#17078)
This was originally landed in bazel-contrib/rules_python#1577 However the fork of py_proto_library brought to the protobuf repo was from an earlier commit: 8257c44 (cl/623401031 for googlers) Thus the fix was lost. This PR simply cherry-picks that change. Closes #17078 COPYBARA_INTEGRATE_REVIEW=#17078 from alexeagle:py_proto_toolchain 226540e PiperOrigin-RevId: 642123868
1 parent 4a5660c commit 8434c12

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

bazel/py_proto_library.bzl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ load("@rules_python//python:py_info.bzl", "PyInfo")
44
load("//bazel/common:proto_common.bzl", "proto_common")
55
load("//bazel/common:proto_info.bzl", "ProtoInfo")
66

7-
ProtoLangToolchainInfo = proto_common.ProtoLangToolchainInfo
7+
PY_PROTO_TOOLCHAIN = "@rules_python//python/proto:toolchain_type"
88

99
_PyProtoInfo = provider(
1010
doc = "Encapsulates information needed by the Python proto rules.",
@@ -22,6 +22,9 @@ _PyProtoInfo = provider(
2222
def _filter_provider(provider, *attrs):
2323
return [dep[provider] for attr in attrs for dep in attr if provider in dep]
2424

25+
def _incompatible_toolchains_enabled():
26+
return getattr(proto_common, "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION", False)
27+
2528
def _py_proto_aspect_impl(target, ctx):
2629
"""Generates and compiles Python code for a proto_library.
2730
@@ -48,7 +51,14 @@ def _py_proto_aspect_impl(target, ctx):
4851
proto.path,
4952
))
5053

51-
proto_lang_toolchain_info = ctx.attr._aspect_proto_toolchain[ProtoLangToolchainInfo]
54+
if _incompatible_toolchains_enabled():
55+
toolchain = ctx.toolchains[PY_PROTO_TOOLCHAIN]
56+
if not toolchain:
57+
fail("No toolchains registered for '%s'." % PY_PROTO_TOOLCHAIN)
58+
proto_lang_toolchain_info = toolchain.proto
59+
else:
60+
proto_lang_toolchain_info = getattr(ctx.attr, "_aspect_proto_toolchain")[proto_common.ProtoLangToolchainInfo]
61+
5262
api_deps = [proto_lang_toolchain_info.runtime]
5363

5464
generated_sources = []
@@ -110,14 +120,15 @@ def _py_proto_aspect_impl(target, ctx):
110120

111121
_py_proto_aspect = aspect(
112122
implementation = _py_proto_aspect_impl,
113-
attrs = {
123+
attrs = {} if _incompatible_toolchains_enabled() else {
114124
"_aspect_proto_toolchain": attr.label(
115125
default = "//python:python_toolchain",
116126
),
117127
},
118128
attr_aspects = ["deps"],
119129
required_providers = [ProtoInfo],
120130
provides = [_PyProtoInfo],
131+
toolchains = [PY_PROTO_TOOLCHAIN] if _incompatible_toolchains_enabled() else [],
121132
)
122133

123134
def _py_proto_library_rule(ctx):

0 commit comments

Comments
 (0)