Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defining a Skylark py_proto_library via proto_library #3935

Closed
htuch opened this issue Oct 19, 2017 · 18 comments
Closed

Defining a Skylark py_proto_library via proto_library #3935

htuch opened this issue Oct 19, 2017 · 18 comments
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Documentation Documentation improvements that cannot be directly linked to other team labels team-Rules-Python Native rules for Python type: documentation (cleanup) type: feature request

Comments

@htuch
Copy link

htuch commented Oct 19, 2017

Given #2626, I was wondering whether it would be possible to add a pure Skylark py_proto_library built on proto_library.

I've played around with rules and had a look at how rules_go does this with toolchains (https://github.com/bazelbuild/rules_go/pull/803/files). These basically involve you maintaining an additional dependency tree (which proto_library is supposed to solve) and having your custom py_proto_library applied across this tree. The most promising approach to avoiding this seems to be to use aspects.

It's pretty clear how aspects could be used to invoke protoc to generate the _pb2.py files. What's less clear is how to use these outputs, which are unknown ahead of time and come from the proto_library transitive dep provider, in conjunction with the native py_library rule (I think this is the sandwich problem?).

Any guidance here from the Bazel proto or Python teams?

@dslomov
Copy link
Contributor

dslomov commented Oct 24, 2017

The idea here is that:

  • aspects will collect all the transitive _pb2.py files and pass it to py_proto_library rule
  • py_proto_library rule will make those available to py_library rule in the py provider.
    This is not super-wel documented.

@htuch
Copy link
Author

htuch commented Oct 24, 2017

@dslomov can Bazel team do a worked example of using aspects + providers to implement a py_proto_library as a blog post? When I think back to the most useful docs I've seen on Bazel, I think these extended examples are great, e.g. I learned a lot about repository_rule from following the C++ autoconfigured toolchain example linked to from https://docs.bazel.build/versions/master/skylark/repository_rules.html.

@ali5h
Copy link

ali5h commented Jan 20, 2018

any update on this would be really appreciated

@junghoahnsc
Copy link

Which rule should I use for now?
Should I use https://github.com/google/protobuf/blob/master/protobuf.bzl#L316?

@brandjon brandjon added type: feature request P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-Python Native rules for Python and removed P2 We'll consider working on this in future. (Assignee optional) category: misc > misc more data needed labels Jan 8, 2019
ironstein0 added a commit to ironstein0/rules_proto that referenced this issue Feb 10, 2019
A rule can not generate a file that is outside the target package
that invoked the rule. Will probably have to use aspects that propogate
to all the proto_library dependencies of py_proto_library/py_grpc_library
rules, and then combine the outputs and make them available to the
py_library rule (bazelbuild/bazel#3935)
@gnossen
Copy link

gnossen commented May 31, 2019

@dslomov @brandjon
Ping. It seems this feature is in limbo between the gRPC, Protobuf, and Bazel teams. Any updates on the Bazel end? A conversation revolving around it is unfolding at grpc/grpc#19183.

@gnossen
Copy link

gnossen commented Jun 4, 2019

@dslomov @brandjon Friendly ping.

@brandjon
Copy link
Member

brandjon commented Jun 5, 2019

+@hlopko since I think there's plans about prioritizing proto support in general.

@hlopko
Copy link
Member

hlopko commented Jun 5, 2019

Yes, I'm trying to get us a person to work on proto rules. No concrete results yet, only active effort :)

@sseveran
Copy link

Any updates on this?

@brandjon brandjon assigned hlopko and unassigned dslomov Oct 23, 2019
@hlopko
Copy link
Member

hlopko commented Oct 28, 2019

Yes, we didn't get the person :/ I'm actively trying to prioritize this, but more important stuff keeps coming up.

@hlopko hlopko removed their assignment Dec 6, 2019
@nicknezis
Copy link

Why not leverage the work being done here? It seems they are handling Python and a few more languages. https://github.com/stackb/rules_proto

@pcj
Copy link
Member

pcj commented Feb 6, 2020

Also worth noting that grpc folks did implement py_proto_library and py_grpc_library since earlier comments. I've used them and they work on a routeguide example but have not used them extensively.

https://github.com/grpc/grpc/blob/master/bazel/python_rules.bzl#L88

Here's link to the functioning example: https://github.com/stackb/rules_proto/tree/5050a8a13d6d13d4284c0cc9f3711181f532f4bb/example/py_grpc_library

@awbraunstein
Copy link

What's the Bazel team's philosophy on rules like the py_proto_library rule being requested here. Currently there are implementations from the protobuf team, grpc team, as well as other users such as http:https://github.com/stackb/rules_proto mentioned above. Is the end goal for the bazel team to own these rules? Or should they be owned by some other entity?

As a user of these rules it becomes difficult to understand which one is the best/right one to use since they all tend to function in the same way (except some use the proto_libaray and some don't.)

@dayfine
Copy link

dayfine commented May 26, 2020

As far as I've tried, none of these py_proto_library works to my satisfaction (so are some other rules that I've tried, like go_proto_library). My expectation for these rules come from google3, and it's frustrating the bazel versions don't quite work the same way.

What I would expect with a lang_proto_library and which is not currently how these rules work:

proto_library(
    name = "foo_proto",
    srcs = ["foo.proto"],
    deps = [
        "bar_proto",
        "@com_google_protobuf//:timestamp_proto",
    ],
)

py_proto_library(
    name = "foo_py_pb2",
    deps = [":foo_proto"],
)

proto_library(
    name = "bar_proto",
    srcs = ["bar.proto"],
)

py_proto_library(
    name = "bar_py_pb2",
    deps = [":bar_proto"],
)

# Using foo_py_pb2 without fuss
py_library(
    deps = [":foo_py_pb2"],
)

Basically:

  1. lang_proto_library only needs foo_proto in dependencies, not other bar_py_pb2. The Go rule and some flavors of the python rule do not work like this.
  2. When using the proto foo in python, I only need foo_py_pb2 as the dependency of the users, i.e. the target would include all its transitive dependencies. The grpc version of py_proto_library fails with this.

@scal444
Copy link

scal444 commented May 18, 2021

Has there been any action here? Agree with the last 2 comments, each version has its own quirks and does not align with internal Google use, which is quite frustrating when trying to maintain open-source libraries that Google also uses.

@sgowroji sgowroji added the team-Documentation Documentation improvements that cannot be directly linked to other team labels label Jan 11, 2023
@weichunstevelee
Copy link

weichunstevelee commented Feb 14, 2023

Hi I'm curious about the current state. Reading the current implementation of py_proto_library in protocobuffer repo, it says

"""Deprecated alias for use before Bazel 5.3.
    Args:
      *args: the name of the py_proto_library.
      **kwargs: other keyword arguments that are passed to py_library.
    Deprecated:
      This is provided for backwards compatibility only.  Bazel 5.3 will
      introduce support for py_proto_library, which should be used instead.

https://github.com/protocolbuffers/protobuf/blob/main/protobuf.bzl#L636

Did it happen?

@cnsgsz
Copy link

cnsgsz commented Mar 6, 2023

I found this doc which has a nice summary of all the options.
The multi-language rule sets seem close to @dayfine said.

Between rules_proto and rules_proto_grpc:

Choosing between the two rulesets will effectively come down to your preference for using Gazelle within your project and the languages supported by each.

Does anyone know if using gazelle is a plus or minus? I assume rules_proto uses gazelle but not rules_proto_grpc. rules_proto_grpc seems to support strictly more languages.

@comius
Copy link
Contributor

comius commented Aug 16, 2023

Starlark py_proto_library is available in https://github.com/bazelbuild/rules_python/blob/main/python/proto.bzl#L21

@comius comius closed this as completed Aug 16, 2023
htuch pushed a commit to cncf/xds that referenced this issue Nov 16, 2023
py_proto_library provided by @com_google_protobuf//:protobuf.bzl has been deprecated for a while now:

This is provided for backwards compatibility only. Bazel 5.3 will
introduce support for py_proto_library, which should be used instead.
https://github.com/protocolbuffers/protobuf/blob/32af7d211b85f71920acdfa9b796db008f8c2a45/protobuf.bzl#L642-L644

However, native py_proto_library has never been provided, see bazelbuild/bazel#3935. Instead @rules_python//python:proto.bzl is recommended. I attempted switching to this library, but it's not compatible with @com_google_googleapis's py_proto_library targets, see #69. I found a hacky workaround by using cc_proto_library to generate python targets, but downstream integration into Envoy failed (envoyproxy/envoy#30159).

This PR migrates py_proto_library implementation to to @com_github_grpc_grpc. This implementation is used by @com_google_googleapis's, and, more importantly, uses bazel aspects. Which decouples cncf/xds and Envoy's dependencies from concrete upstream py_proto_library implementations.

This resulted in a significant code improvement of bazel/api_build_system.bzl:

No more custom @com_google_googleapis dependency mapping needed via py_proto_library rules override.
No more hardcoded dependencies _xds_py_proto_library - proto dependency tree is determined from proto_library definitions via Basel aspects.
No more EXTERNAL_PROTO_PY_BAZEL_DEP_MAP dependency map needed - for the same reason.
Similar work in Envoy: envoyproxy/envoy#30834.

Signed-off-by: Sergii Tkachenko <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Documentation Documentation improvements that cannot be directly linked to other team labels team-Rules-Python Native rules for Python type: documentation (cleanup) type: feature request
Projects
None yet
Development

No branches or pull requests