1616 "@bazel_skylib//lib:paths.bzl" ,
1717 "paths" ,
1818)
19+ load ("@com_google_protobuf//bazel/common:proto_common.bzl" , "proto_common" )
1920load (
20- "@rules_proto//proto:proto_common .bzl" ,
21- proto_toolchains = "toolchains " ,
21+ "@com_google_protobuf//bazel/common:proto_lang_toolchain_info .bzl" ,
22+ "ProtoLangToolchainInfo " ,
2223)
2324load (
2425 "//go:def.bzl" ,
4849# default.
4950_PROTO_TOOLCHAIN_TYPE = "@rules_proto//proto:toolchain_type"
5051
52+ def _incompatible_toolchains_enabled ():
53+ return getattr (proto_common , "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION" , False )
54+
55+ def _find_toolchain (ctx , legacy_attr , toolchain_type ):
56+ if _incompatible_toolchains_enabled ():
57+ toolchain = ctx .toolchains [toolchain_type ]
58+ if not toolchain :
59+ fail ("No toolchains registered for '%s'." % toolchain_type )
60+ return toolchain .proto
61+ else :
62+ return getattr (ctx .attr , legacy_attr )[ProtoLangToolchainInfo ]
63+
64+ def _use_toolchain (toolchain_type ):
65+ if _incompatible_toolchains_enabled ():
66+ return [config_common .toolchain_type (toolchain_type , mandatory = False )]
67+ else :
68+ return []
69+
70+ def _if_legacy_toolchain (legacy_attr_dict ):
71+ if _incompatible_toolchains_enabled ():
72+ return {}
73+ else :
74+ return legacy_attr_dict
75+
5176GoProtoCompiler = provider (
5277 doc = "Information and dependencies needed to generate Go code from protos" ,
5378 fields = {
@@ -188,7 +213,7 @@ def proto_path(src, proto):
188213def _go_proto_compiler_impl (ctx ):
189214 go = go_context (ctx , include_deprecated_properties = False )
190215 go_info = new_go_info (go , ctx .attr )
191- proto_toolchain = proto_toolchains . find_toolchain (
216+ proto_toolchain = _find_toolchain (
192217 ctx ,
193218 legacy_attr = "_legacy_proto_toolchain" ,
194219 toolchain_type = _PROTO_TOOLCHAIN_TYPE ,
@@ -233,7 +258,7 @@ _go_proto_compiler = rule(
233258 "_go_context_data" : attr .label (
234259 default = "//:go_context_data" ,
235260 ),
236- }, ** proto_toolchains . if_legacy_toolchain ({
261+ }, ** _if_legacy_toolchain ({
237262 "_legacy_proto_toolchain" : attr .label (
238263 # Setting cfg = "exec" here as the legacy_proto_toolchain target
239264 # already needs to apply the non_go_tool_transition. Flipping the
@@ -243,7 +268,7 @@ _go_proto_compiler = rule(
243268 default = "//proto/private:legacy_proto_toolchain" ,
244269 ),
245270 })),
246- toolchains = [GO_TOOLCHAIN ] + proto_toolchains . use_toolchain (_PROTO_TOOLCHAIN_TYPE ),
271+ toolchains = [GO_TOOLCHAIN ] + _use_toolchain (_PROTO_TOOLCHAIN_TYPE ),
247272)
248273
249274def go_proto_compiler (name , ** kwargs ):
0 commit comments