rules-proto-grpc / rules_proto_grpc

Bazel rules for building Protobuf and gRPC code and libraries from proto_library targets
https://rules-proto-grpc.com
Apache License 2.0
256 stars 159 forks source link

How to properly setup pyi_out dir in `extra_protoc_args`? #295

Closed pcpLiu closed 11 months ago

pcpLiu commented 11 months ago

Description

Hi,

I wanna generate pyi so i need to setup --pyi_out dir in extra_protoc_args. I want it to be generated the same dir with generated pythons files.

However, I'm not sure how to properly specify this dir in the context of bazel such that I don't need to hardcode it.

Any suggestions? Thanks

pcpLiu commented 11 months ago

looks like we can work around with custom plugin

pavannaganna commented 10 months ago

How do we generate pyi files using custom plugin?

RmStorm commented 7 months ago

I'm also interested in this! @pcpLiu Can you describe how you solved it?

chrisirhc commented 5 months ago

I'd urge reopening this, since this comes in handy for type-checking and autocomplete dev experience. The examples in grpc feature the pyi files as if they're the default: https://github.com/grpc/grpc/blob/master/examples/python/async_streaming/phone_pb2.pyi

Update: Alternatively, I create a new issue as a feature request to support pyi output, since this was posed as a question.

pcpLiu commented 4 months ago

Hey guys, so in the core we created a plugin like

load("@rules_proto_grpc//:defs.bzl", "proto_plugin")

proto_plugin(
    name = "pyi_plugin",
    exclusions = [
        "google/protobuf",
    ],
    outputs = ["{protopath|python}_pb2.pyi"],
    protoc_plugin_name = "pyi",
    visibility = ["//visibility:public"],
)

This plugin just simply includes pyi files generated from grpc compiling step into the output dir. At least for our version, grpc compiling by default generates pyi files.

Then, depending on your needs, recreate the rule by copying this and injecting the new plugin defined above, you gonna get pyi files in your bazel output

Our impl is pretty hacky and embedded into our internal repo, so we don't have a neat code snippets for sharing right now. You should be able to make it work follow the similar thoughts.

chrisirhc commented 4 months ago

Thanks for sharing!

Hm after reading the source code at https://github.com/protocolbuffers/protobuf/blob/a4f9ddd8fc2ff1afa6697059d494a9bf0c09c680/src/google/protobuf/compiler/python/generator.cc#L197-L200 , I realized you can pass options = pyi_out to the python plugin. So the current rules already support pyi output. There's just no example of this type of usage, perhaps I can submit a PR on to add an example.

ryan-clancy commented 4 months ago

@chrisirhc - an example would be helpful, I am looking for a way to add pyi_out as well.