Skip to content
Closed
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
2 changes: 2 additions & 0 deletions bazel/common/proto_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ def _compile(

if plugin_output:
args.add(plugin_output, format = proto_lang_toolchain_info.out_replacement_format_flag)
if proto_lang_toolchain_info.out_replacement_format_flag.startswith("--python"):
args.add(plugin_output, format = "--pyi_out=%s")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure the best way to do this. I would prefer to just add the --pyi_out to python's proto_lang_toolchain instantiation but the command_line = "--python_out=%s", can only contain one %s string.

This is my first time in this code base so I would appreciate any suggestions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have you define another toolchain here for pyi:

proto_lang_toolchain(
name = "python_toolchain",
command_line = "--python_out=%s",
progress_message = "Generating Python proto_library %{label}",
runtime = ":protobuf_python",
# NOTE: This isn't *actually* public. It's an implicit dependency of py_proto_library,
# so must be public so user usages of the rule can reference it.
visibility = ["//visibility:public"],
)

Then it can be another dependency to the rule, and we can invoke proto_common.compile() twice, once for each toolchain.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR should not require changing proto_common.bzl. That is common code used by all proto rules, and shouldn't know anything about .pyi.

if proto_lang_toolchain_info.plugin:
tools.append(proto_lang_toolchain_info.plugin)
args.add(proto_lang_toolchain_info.plugin.executable, format = proto_lang_toolchain_info.plugin_format_flag)
Expand Down
16 changes: 10 additions & 6 deletions bazel/py_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ def _py_proto_aspect_impl(target, ctx):
proto_root = proto_info.proto_source_root
if proto_info.direct_sources:
# Generate py files
generated_sources = proto_common.declare_generated_files(
actions = ctx.actions,
proto_info = proto_info,
extension = "_pb2.py",
name_mapper = lambda name: name.replace("-", "_").replace(".", "/"),
)
generated_sources = []
for extension in ["_pb2.py", "_pb2.pyi"]:
generated_sources.extend(
proto_common.declare_generated_files(
actions = ctx.actions,
proto_info = proto_info,
extension = extension,
name_mapper = lambda name: name.replace("-", "_").replace(".", "/"),
),
)

# Handles multiple repository and virtual import cases
if proto_root.startswith(ctx.bin_dir.path):
Expand Down
Loading