Rust Protobuf Rules

Overview

These build rules are used for building protobufs/gRPC in Rust with Bazel.

See the protobuf example for a more complete example of use.

Setup

To use the Rust proto rules, add the following to your WORKSPACE file to add the external repositories for the Rust proto toolchain (in addition to the rust rules setup):

load("@io_bazel_rules_rust//proto:repositories.bzl", "rust_proto_repositories")

rust_proto_repositories()

This will load crate dependencies of protobuf that are generated using cargo raze inside the rules_rust repository. However, using those dependencies might conflict with other uses of cargo raze. If you need to change those dependencies, please see the dedicated section below.

For additional information about Bazel toolchains, see here.

Customizing dependencies

These rules depends on the protobuf and the grpc crates in addition to the protobuf compiler. To obtain these crates, rust_proto_repositories imports the given crates using BUILD files generated with cargo raze.

If you want to either change the protobuf and gRPC rust compilers, or to simply use cargo raze in a more complex scenario (with more dependencies), you must redefine those dependencies.

To do this, once you've imported the needed dependencies (see our Cargo.toml file to see the default dependencies), you need to create your own toolchain. To do so you can create a BUILD file with your toolchain definition, for example:

load("@io_bazel_rules_rust//proto:toolchain.bzl", "rust_proto_toolchain")

rust_proto_toolchain(
    name = "proto-toolchain-impl",
    # Path to the protobuf compiler.
    protoc = "@com_google_protobuf//:protoc",
    # Protobuf compiler plugin to generate rust gRPC stubs.
    grpc_plugin = "//cargo_raze/remote:cargo_bin_protoc_gen_rust_grpc",
    # Protobuf compiler plugin to generate rust protobuf stubs.
    proto_plugin = "//cargo_raze/remote:cargo_bin_protoc_gen_rust",
)

toolchain(
    name = "proto-toolchain",
    toolchain = ":proto-toolchain-impl",
    toolchain_type = "@io_bazel_rules_rust//proto:toolchain",
)

Now that you have your own toolchain, you need to register it by inserting the following statement in your WORKSPACE file:

register_toolchains("//my/toolchains:proto-toolchain")

Finally, you might want to set the rust_deps attribute in rust_proto_library and rust_grpc_library to change the compile-time dependencies:

rust_proto_library(
    ...
    rust_deps = ["//cargo_raze/remote:protobuf"],
    ...
)

rust_grpc_library(
    ...
    rust_deps = [
        "//cargo_raze/remote:protobuf",
        "//cargo_raze/remote:grpc",
        "//cargo_raze/remote:tls_api",
        "//cargo_raze/remote:tls_api_stub",
    ],
    ...
)

Note: Ideally, we would inject those dependencies from the toolchain, but due to bazelbuild/bazel#6889 all dependencies added via the toolchain ends-up being in the wrong configuration.