Skip to content

Commit

Permalink
Reset all Go settings for go_proto_* non-Go attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Apr 8, 2022
1 parent 9fc6d45 commit dbf2f89
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
10 changes: 10 additions & 0 deletions proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ load(
"//proto:compiler.bzl",
"go_proto_compiler",
)
load(
"//go/private/rules:transition.bzl",
"go_non_go_exec_reset_target",
)
load(
"//proto/wkt:well_known_types.bzl",
"GOGO_WELL_KNOWN_TYPE_REMAPS",
Expand Down Expand Up @@ -112,6 +116,12 @@ go_proto_compiler(
] + WELL_KNOWN_TYPE_RULES.values(),
)

go_non_go_exec_reset_target(
name = "protoc_reset_target_",
dep = "@com_google_protobuf//:protoc",
visibility = ["//visibility:public"],
)

filegroup(
name = "all_rules",
testonly = True,
Expand Down
6 changes: 3 additions & 3 deletions proto/compiler.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ load(
)
load(
"//go/private/rules:transition.bzl",
"go_reset_target",
"go_exec_reset_target",
)

GoProtoCompiler = provider(
Expand Down Expand Up @@ -200,7 +200,7 @@ _go_proto_compiler = rule(
"_protoc": attr.label(
executable = True,
cfg = "exec",
default = "@com_google_protobuf//:protoc",
default = "//proto:protoc_reset_target_",
),
"_go_context_data": attr.label(
default = "//:go_context_data",
Expand All @@ -212,7 +212,7 @@ _go_proto_compiler = rule(
def go_proto_compiler(name, **kwargs):
plugin = kwargs.pop("plugin", "@com_github_golang_protobuf//protoc-gen-go")
reset_plugin_name = name + "_reset_plugin_"
go_reset_target(
go_exec_reset_target(
name = reset_plugin_name,
dep = plugin,
visibility = ["//visibility:private"],
Expand Down
26 changes: 22 additions & 4 deletions proto/def.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ load(
"GoSource",
"go_context",
)
load(
"@bazel_skylib//lib:types.bzl",
"types",
)
load(
"//proto:compiler.bzl",
"GoProtoCompiler",
Expand All @@ -27,6 +31,10 @@ load(
"//go/private:providers.bzl",
"INFERRED_PATH",
)
load(
"//go/private/rules:transition.bzl",
"go_non_go_exec_reset_tool_transition",
)
load(
"@rules_proto//proto:defs.bzl",
"ProtoInfo",
Expand All @@ -37,8 +45,9 @@ GoProtoImports = provider()
def get_imports(attr):
proto_deps = []

if hasattr(attr, "proto") and attr.proto and ProtoInfo in attr.proto:
proto_deps = [attr.proto]
# ctx.attr.proto is a one-element array since there is a Starlark transition attached to it.
if hasattr(attr, "proto") and attr.proto and types.is_list(attr.proto) and ProtoInfo in attr.proto[0]:
proto_deps = [attr.proto[0]]
elif hasattr(attr, "protos"):
proto_deps = [d for d in attr.protos if ProtoInfo in d]
else:
Expand Down Expand Up @@ -92,7 +101,9 @@ def _go_proto_library_impl(ctx):
#TODO: print("DEPRECATED: proto attribute on {}, use protos instead".format(ctx.label))
if ctx.attr.protos:
fail("Either proto or protos (non-empty) argument must be specified, but not both")
proto_deps = [ctx.attr.proto]

# ctx.attr.proto is a one-element array since there is a Starlark transition attached to it.
proto_deps = [ctx.attr.proto[0]]
else:
if not ctx.attr.protos:
fail("Either proto or protos (non-empty) argument must be specified")
Expand Down Expand Up @@ -137,8 +148,12 @@ def _go_proto_library_impl(ctx):
go_proto_library = rule(
implementation = _go_proto_library_impl,
attrs = {
"proto": attr.label(providers = [ProtoInfo]),
"proto": attr.label(
cfg = go_non_go_exec_reset_tool_transition,
providers = [ProtoInfo],
),
"protos": attr.label_list(
cfg = go_non_go_exec_reset_tool_transition,
providers = [ProtoInfo],
default = [],
),
Expand All @@ -159,6 +174,9 @@ go_proto_library = rule(
"_go_context_data": attr.label(
default = "//:go_context_data",
),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
)
Expand Down

0 comments on commit dbf2f89

Please sign in to comment.