Deps attribute of py_library rule does not have mandatory providers: 'py'

1,743 views
Skip to first unread message

hai...@grakn.ai

unread,
Sep 14, 2018, 10:48:09 AM9/14/18
to Bazel/Python Special Interest Group
Hi everyone,

I've been introduced to this discussion group by Ofer from the Bazel team. Very excited to see a Bazel+Python specialist group!

'm trying to work on this issue (https://github.com/protocolbuffers/protobuf/issues/5147) and submit a PR to extend protobuf example documentation for Bazel by adding py_proto_library.

I'm starting by trying to integrate py_proto_library into our repository, aiming to have it look like how java_proto_library is shown in the examples/BUILD and examples/WORKSPACE files in protobuf's repository.

So I thought I'd share what I have here and where I'm stuck. Perhaps someone can help. Please do let me know if I should post this somewhere else, or in a new issue.

So far I've defined the following:

//dependencies/compilers:dependencies.bzl:
```
def protobuf_dependencies():

native.http_archive(
name = "bazel_skylib",
sha256 = "bbccf674aa441c266df9894182d80de104cabd19be98be002f6d478aaa31574d",
strip_prefix = "bazel-skylib-2169ae1c374aab4a09aa90e65efe1a3aad4e279b",
urls = ["https://github.com/bazelbuild/bazel-skylib/archive/2169ae1c374aab4a09aa90e65efe1a3aad4e279b.tar.gz"],
)

native.bind(name = "six", actual = "@six_archive//:six")
native.new_http_archive(
name = "six_archive",
build_file = "@com_google_protobuf//:six.BUILD",
sha256 = "105f8d68616f8248e24bf0e9372ef04d3cc10104f1980f54d57b2ce73a5ad56a",
urls = ["https://pypi.python.org/packages/source/s/six/six-1.10.0.tar.gz#md5=34eed507548117b2ab523ab14b2f8b55"],
)

# This com_google_protobuf repository is required for proto_library rule.
# It provides the protocol compiler binary (i.e., protoc).
native.http_archive(
name = "com_google_protobuf",
sha256 = "0a4c6d0678eb2f063df332cff1a41647ef692c067b5cfb19e51bca778e79d9e0",
strip_prefix = "protobuf-3.6.1",
urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protobuf-all-3.6.1.zip"],
)
```

WORKSPACE:
```
load("//dependencies/compilers:dependencies.bzl", "protobuf_dependencies")
protobuf_dependencies()

load("@bazel_skylib//:lib.bzl", "versions")
versions.check(minimum_bazel_version = "0.5.4")
//client-protocol/BUILD

proto_library(
name = "client-protocol",
srcs = glob(["proto/*.proto"]),
visibility = [
"//server:__subpackages__",
"//client-java:__subpackages__",
"//client-python:__subpackages__",
]
)
```

//client-python/BUILD
```
load("@com_google_protobuf//:protobuf.bzl", "py_proto_library")

py_proto_library(
name = "protocol",
deps = ["//client-protocol:client-protocol"],
protoc = "@com_google_protobuf//:protoc",
)
```

Everything works fine for //server:... and //client-java:.... But when I try to bazel build //client-python:protocol, I get the following error:
```
ERROR: /Users/haikalpribadi/Workspace/repos/graknlabs/grakn/client-python/BUILD:21:1: in deps attribute of py_library rule //client-python:protocol: '//client-protocol:client-protocol' does not have mandatory providers: 'py'. Since this rule was created by the macro 'py_proto_library', the error might have been caused by the macro implementation in /private/var/tmp/_bazel_haikalpribadi/3cdeaa1301f056ec7b4e7ee46bda5c79/external/com_google_protobuf/protobuf.bzl:382:12
```

I can't find any reference on the internet to the error message shown above, except for this bazelbuild/bazel#4237

Does anyone have any idea what is the cause of this problem and how to solve it?


Thank you so much!

Jon Brandvein

unread,
Sep 14, 2018, 12:17:26 PM9/14/18
to Bazel/Python Special Interest Group
Hello Haikal,

The error you're seeing means that the py_library rule's "deps" attribute requires all targets listed in there to return a Starlark (formerly called "Skylark") provider named "py".

Just some background on providers: See here for the general explanation. (Note that "py" is a "legacy provider" since it's identified using a string as opposed to a declared Starlark symbol.) Rules can declare that they require their dependencies to have certain mandatory providers; for a Starlark-defined rule this would look like this example, but in native code it's baked into the py_library rule definition here. Looking in PyCommon.java it appears this is used to track the transitive source files and whether any transitive dep has shared libraries.

All that's a roundabout way of saying that your use of the py_proto_library macro caused it to try to create a py_library target with a non-Python dep, which it didn't like. The doc for the py_proto_library macro says its own deps args must be py_proto_library targets, but you pass in a proto_library. I'd double check the recommended usage of this macro, in particular whether it can operate on proto_library targets or just .proto files (via its srcs param).

hai...@grakn.ai

unread,
Sep 14, 2018, 3:24:50 PM9/14/18
to Bazel/Python Special Interest Group
Thank you so much, Jon!

That was definitely the problem.

It turns out, in the `py_proto_library rule`, I just needed to provide the `.proto` files as `srcs`, instead of providing a `proto_library` rule as `deps`. `py_proto_library` runs its own `proto_gen` and then feeds that into a `py_library`.

i.e. I just needed to add the following to my BUILD file:
```
py_proto_library(
    name = "protocol",
    srcs = glob(["proto/*.proto"]),
    srcs_version = "PY2AND3",
    deps = [
        "@com_google_protobuf_py//:protobuf_python",
    ],
)
```

Thank you!
Reply all
Reply to author
Forward
0 new messages