How to deal with Bazel grpc transitive dependencies?

232 views Asked by At

I tried to setup a grpc Python server using Bazel. On my Mac machine, if I load the grpc rules first before the proto rules, I can build and run the binary just fine. However, if I load the proto rules before grpc rules, I'm unable to build the binary.

I looked at the grpc_extra_deps and it loads the protobuf deps also. So I'm guessing the issue has something to do with loading the proto deps twice. (Also because I do not understand the error message)

However, I find it quite difficult to figure out. Does this mean if I add another rule in the future and that rule requires loading some other rule, I have to make sure I'm not already loading these rules? What happens if I load two rules that both depends on the protobuf rule? Moving the load order seems like a workaround.

Is there a better approach than to reorder the load ordering when the build fails? Thanks!

My BUILD file:

load("@com_github_grpc_grpc//bazel:python_rules.bzl", "py_grpc_library", "py_proto_library")

proto_library(
    name = "dummy_pb2",
    srcs = ["dummy.proto"],
)

py_proto_library(
    name = "dummy_py_pb2",
    deps = [":dummy_pb2"],
)

py_grpc_library(
    name = "dummy_py_pb2_grpc",
    srcs = [":dummy_pb2"],
    deps = [":dummy_py_pb2"],
)

py_binary(
    name = "main",
    srcs = ["main.py"],
    deps = [
        ":dummy_py_pb2",
        ":dummy_py_pb2_grpc",
    ],
)

My WORKSPACE file(load grpc rules first, works fine), had to remove a bunch of http_archives to save space for this post.

...


load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")

py_repositories()

python_register_toolchains(
    name = "python3_10",
    python_version = "3.10",
)

load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")

grpc_deps()

load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")

grpc_extra_deps()

# Load proto rules after grpc rules: works fine

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

rules_proto_dependencies()

rules_proto_toolchains()

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

Load proto rules before grpc, fails

...

# Load proto rules before grpc rules: fails

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

rules_proto_dependencies()

rules_proto_toolchains()

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

# groc rules

load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")

grpc_deps()

load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")

grpc_extra_deps()

Error message:

...
ERROR: /private/var/tmp/_bazel_chuanye/9a81a05a7dab3dd78d1a247bcd97d00b/external/upb/BUILD:517:28: Executing genrule @upb//:gen_descriptor_upb_proto_stage1 [for tool] failed: (Exit 1): bash failed: error executing command (from target @upb//:gen_descriptor_upb_proto_stage1) /bin/bash -c ... (remaining 1 argument skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
src: warning: directory does not exist.
Could not make proto path relative: google/protobuf/descriptor.proto: No such file or directory
ERROR: /private/var/tmp/_bazel_chuanye/9a81a05a7dab3dd78d1a247bcd97d00b/external/upb/upbc/BUILD:64:28: Executing genrule @upb//upbc:gen_plugin_upb_proto_stage1 [for tool] failed: (Exit 1): bash failed: error executing command (from target @upb//upbc:gen_plugin_upb_proto_stage1) /bin/bash -c ... (remaining 1 argument skipped)

...

I tried moving the load order of Bazel rules.

0

There are 0 answers