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
250 stars 157 forks source link

Can not use both rules_proto_grpc_python and rules_proto_grpc_java in the same project #341

Open vudaoanhtuan opened 1 month ago

vudaoanhtuan commented 1 month ago

Issue Description

I'm migrating my project to from WORKSPACE to MODULE.bazel, my project includes some grpc services written by both java and python, and I am using rules_proto_grpc v4.1.1, every work well. After migrating to bzlmod, I use rules_proto_grpc v5.0.0, first I use rules_proto_grpc_python for python services and they work, then I use rules_proto_grpc_java for java services but I got error:

ERROR: Analysis of target '//:java_binary' failed; 
build aborted: in tag at https://bcr.bazel.build/modules/grpc-java/1.62.2/MODULE.bazel:89:15: 
no repository visible as '@com_google_protobuf_javalite' to the repository '@@grpc-java~', 
but referenced by label '@com_google_protobuf_javalite//:protobuf_javalite' in attribute 'target' of tag 'override'. 
Is the module 'grpc-java' missing a bazel_dep or use_repo(..., "com_google_protobuf_javalite")?

I notice that when I disable one of them, the other will work without any error, but when I enable both rules, I got above error message again

Here is my simple setup to reproduce the error: test_bazel.zip

bazel run //:python_binary works but bazel run //:java_binary throw error message

Log Output

ERROR: Analysis of target '//:java_binary' failed; build aborted: in tag at https://bcr.bazel.build/modules/grpc-java/1.62.2/MODULE.bazel:89:15: no repository visible as '@com_google_protobuf_javalite' to the repository '@@grpc-java~', but referenced by label '@com_google_protobuf_javalite//:protobuf_javalite' in attribute 'target' of tag 'override'. Is the module 'grpc-java' missing a bazel_dep or use_repo(..., "com_google_protobuf_javalite")?
INFO: Elapsed time: 2.157s, Critical Path: 0.02s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target
FAILED: 
    Fetching module extension maven in @@rules_jvm_external~//:extensions.bzl; starting

rules_proto_grpc Version

5.0.0

Bazel Version

7.2.1

OS

MacOS

Link to Demo Repo

No response

MODULE.bazel or WORKSPACE Content

bazel_dep(name = "rules_python", version = "0.34.0")
python = use_extension("@rules_python//python/extensions:python.bzl", "python")

python.toolchain(python_version = "3.11", is_default = True)

bazel_dep(name = "protobuf", version = "27.2")
bazel_dep(name = "rules_proto_grpc", version = "5.0.0")
bazel_dep(name = "rules_proto_grpc_python", version = "5.0.0")
bazel_dep(name = "rules_proto_grpc_java", version = "5.0.0")

BUILD Content

load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_proto_grpc_python//:defs.bzl", "python_grpc_library")
load("@rules_proto_grpc_java//:defs.bzl", "java_grpc_library")

load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_java//java:defs.bzl", "java_binary")

proto_library(
    name = "thing_proto",
    srcs = ["thing.proto"],
    deps = ["@protobuf//:any_proto"],
)

python_grpc_library(
    name = "thing_python_grpc",
    protos = [":thing_proto"],
)

py_binary(
    name = "python_binary",
    main = "main.py",
    srcs = ["main.py"],
    deps = [
        ":thing_python_grpc"
    ]
)

java_grpc_library(
    name = "thing_java_grpc",
    protos = [":thing_proto"],
)

java_binary(
    name = "java_binary",
    main_class = "Main",
    srcs = ["Main.java"],
    deps = [
        ":thing_java_grpc"
    ]
)

Proto Content

syntax = "proto3";

package examples.proto;

import "google/protobuf/any.proto";

message Thing {
  string name = 1;
  google.protobuf.Any payload = 2;
}

Any Other Content

No response

aaliddell commented 1 month ago

This is due to a bug in a released version of grpc-java: https://github.com/grpc/grpc-java/issues/11275

This project doesn't directly ever use the grpc-java repo, but it gets pulled transitively through googleapis -> grpc. Whilst there is a new version released that fixes this, you end up pulling the broken version due to googleapis not being updated to the fixed version.

A temporary fix is to use a single_version_override in your MODULE.bazel to force the fixed version, but a real fix needs to be done in one of those other repos sadly...

single_version_override(
    module_name = "grpc-java",
    version = "1.64.0",
)
vudaoanhtuan commented 1 month ago

I added single_version_override but I got another error:

ERROR: no such package '@@[unknown repo 'maven' requested from @@protobuf~]//': The repository '@@[unknown repo 'maven' requested from @@protobuf~]' could not be resolved: No repository visible as '@maven' from repository '@@protobuf~'
ERROR: /private/var/tmp/_bazel_user/691169fb99f8988cdeecf9ebf10cdc9f/external/protobuf~/java/util/BUILD.bazel:8:13: no such package '@@[unknown repo 'maven' requested from @@protobuf~]//': The repository '@@[unknown repo 'maven' requested from @@protobuf~]' could not be resolved: No repository visible as '@maven' from repository '@@protobuf~' and referenced by '@@protobuf~//java/util:util'
ERROR: Analysis of target '//:java_binary' failed; build aborted: Analysis failed
aaliddell commented 1 month ago

Gah, I see this too. This is a different bug in yet another upstream dependency: https://github.com/protocolbuffers/protobuf/issues/17176

This one has no simple workaround, as there is no fixed protobuf release available (let alone available in BCR)...

vudaoanhtuan commented 1 month ago

I see that https://github.com/protocolbuffers/protobuf/issues/17176 is resolved by PR https://github.com/protocolbuffers/protobuf/pull/17402, that PR is merged into main branch, so I tried to override protobuf by using git_override rule, but it produces another error, perhaps I should wait for them to release a new version