Why is a publicly visible Bazel ProtoBuf target 'not declared'

北战南征 提交于 2019-12-06 13:55:38

The problem is that your target (//proto:py) depends on //:protobuf_python, not @com_gooogle_protobuf//:protobuf_python. You can confirm this with bazel query.

$ bazel query --output build //proto:py
# proto/BUILD:3:1
py_library(
  name = "py",
  visibility = ["//visibility:public"],
  generator_name = "py",
  generator_function = "py_proto_library",
  generator_location = "proto/BUILD:3",
  deps = ["//:protobuf_python", "@com_google_protobuf//:protobuf_python", "//proto:example_proto"],
  imports = [],
  srcs = [],
)

You can see it in the deps list. So now the question is, why does it depend on that? You certainly didn't set that anywhere. The answer is that, since py_proto_library is a macro, it can do whatever it wants.

In particular, these lines of the macro are causing you trouble:

https://github.com/google/protobuf/blob/6032746882ea48ff6d983df8cb77e2ebf399bf0c/protobuf.bzl#L320 https://github.com/google/protobuf/blob/6032746882ea48ff6d983df8cb77e2ebf399bf0c/protobuf.bzl#L373-L374

py_proto_library has an attribute called default_runtime that it appends to the deps list. The default value is ":protobuf_python". But that only works if you use the macro in the same repository that declares protobuf_python.

So, you can fix this by setting default_runtime = "@com_google_protobuf//:protobuf_python" in your py_proto_librarys attributes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!