Search code examples
c++bazelgrpc-c++bazel-cpp

Bazel failed to find toolchain package


I am writing grpc server using c++ and bazel.

Here is my project structure:
enter image description here

Here is my proto/BUILD:

load("@rules_proto_grpc_cpp//:defs.bzl", "cpp_grpc_library")

cpp_grpc_library(
    name = "main-grpc",
    protos = ["main.proto"],
)

src/BUILD:

cc_binary(
  name = "server",
  srcs = ["main.cpp"],
  deps = [
    "//proto:main-grpc", 
    "//src/libs:api", 
    "//src/libs:dbmanager",
    ]
)

MODULE.bazel:

bazel_dep(name = "rules_proto_grpc_cpp", version = "5.0.1")
bazel_dep(name = "toolchains_protoc", version = "0.3.6")
bazel_dep(name = "rules_proto", version = "6.0.2")
bazel_dep(name = "protobuf", version = "29.1")


protoc = use_extension("@toolchains_protoc//protoc:extensions.bzl", "protoc")
protoc.toolchain(
    google_protobuf = "com_google_protobuf",
    version = "v27.1",
)

I follow this example. My goal is to compile main.proto to libs using bazel. Unfortunately, my code fail with next error:

ERROR: .../src/backend/proto/BUILD:3:17: Target '//proto:main-grpc_pb' depends on toolchain '@@toolchains_protoc++protoc+toolchains_protoc_hub.linux_x86_64//:prebuilt_protoc_toolchain', which cannot be found: error loading package '@@toolchains_protoc++protoc+toolchains_protoc_hub.linux_x86_64//': cannot load '@@rules_proto+//proto/private/rules:proto_toolchain_rule.bzl': no such file'

I don't understand why i got this error 'cause I include this toolchain as dependency.


Solution

  • I'm not familiar with "rules_proto_grpc_cpp", but I think one of the bazel dependencies is bugging. And this rule is not needed for grpc builds since grpc repo covers the build rules. See examples of proto and cpp in https://github.com/grpc/grpc/tree/master/examples. Well the example doesn't use the bazel registry, but the idea is similar.

    In this case for bazel registry, let's first import modulebazel_dep(name = "grpc", version = "1.70.1") and in BUILD.bzl

    load("@com_google_protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
    load("@rules_proto//proto:defs.bzl", "proto_library")
    load("@grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library")
    
    proto_library(
        name = "main_proto",
        srcs = ["main.proto"],
    )
    
    
    cc_proto_library(
        name = "main_cc_proto",
        deps = [":main_proto"],
    )
    
    
    cc_grpc_library(
        name = "main_cc_grpc",
        srcs = [":main_proto"],
        grpc_only = True,
        deps = [":main_cc_proto"],
    )