#protobuf #async #codegen #rpc

build tonic-build

Codegen module of tonic gRPC implementation

47 releases

Uses new Rust 2024

0.14.6 May 7, 2026
0.14.5 Feb 19, 2026
0.14.3 Jan 28, 2026
0.14.2 Sep 2, 2025
0.1.0-alpha.6 Nov 10, 2019

#139 in #rpc

Download history 443639/week @ 2026-03-03 2956573/week @ 2026-03-10 2793462/week @ 2026-03-17 2478500/week @ 2026-03-24 2416479/week @ 2026-03-31 2746782/week @ 2026-04-07 2920126/week @ 2026-04-14 2914153/week @ 2026-04-21 3129430/week @ 2026-04-28 3416472/week @ 2026-05-05 3943163/week @ 2026-05-12 3694177/week @ 2026-05-19 3698541/week @ 2026-05-26 3015050/week @ 2026-06-02

14,930,166 downloads per month
Used in 2,541 crates (917 directly)

MIT license

72KB
1.5K SLoC

tonic-build

Provides code generation for service stubs to use with tonic. For protobuf compilation via prost, use the tonic-prost-build crate instead.

Feature flags

  • transport: Enables generation of connect method using tonic::transport::Channel (enabled by default).

Features

Required dependencies

[dependencies]
tonic = "<tonic-version>"
prost = "<prost-version>"

[build-dependencies]
tonic-prost-build = "<tonic-version>"

Getting Started

For protobuf compilation, use tonic-prost-build in your build.rs file at the root of the binary/library.

You can rely on the defaults via

fn main() -> Result<(), Box<dyn std::error::Error>> {
    tonic_prost_build::compile_protos("proto/service.proto")?;
    Ok(())
}

Or configure the generated code deeper via

fn main() -> Result<(), Box<dyn std::error::Error>> {
   tonic_prost_build::configure()
        .build_server(false)
        .compile_protos(
            &["proto/helloworld/helloworld.proto"],
            &["proto/helloworld"],
        )?;
   Ok(())
}

For further details how to use the generated client/server, see the examples here or the Google APIs example below.

On NixOS, it is better to specify the location of PROTOC and PROTOC_INCLUDE explicitly.

$ export PROTOBUF_LOCATION=$(nix-env -q protobuf --out-path --no-name)
$ export PROTOC=$PROTOBUF_LOCATION/bin/protoc
$ export PROTOC_INCLUDE=$PROTOBUF_LOCATION/include
$ cargo build

The reason being that if prost_build::compile_protos fails to generate the resultant package, the failure is not obvious until the include!(concat!(env!("OUT_DIR"), "/resultant.rs")); fails with No such file or directory error.

Google APIs example

A good way to use Google API is probably using git submodules.

So suppose in our proto folder we do:

git submodule add https://github.com/googleapis/googleapis

git submodule update --remote

And a bunch of Google proto files in structure will be like this:

├── googleapis
│   └── google
│       ├── api
│       │   ├── annotations.proto
│       │   ├── client.proto
│       │   ├── field_behavior.proto
│       │   ├── http.proto
│       │   └── resource.proto
│       └── pubsub
│           └── v1
│               ├── pubsub.proto
│               └── schema.proto

Then we can generate Rust code via this setup in our build.rs:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    tonic_prost_build::configure()
        .build_server(false)
        //.out_dir("src/google")  // you can change the generated code's location
        .compile_protos(
            &["proto/googleapis/google/pubsub/v1/pubsub.proto"],
            &["proto/googleapis"], // specify the root location to search proto dependencies
        )?;
    Ok(())
}

Then you can reference the generated Rust like this in your code:

pub mod api {
    tonic::include_proto!("google.pubsub.v1");
}
use api::{publisher_client::PublisherClient, ListTopicsRequest};

Or if you want to save the generated code in your own code base, you can uncomment the line .out_dir(...) above, and in your lib file config a mod like this:

pub mod google {
    #[path = ""]
    pub mod pubsub {
        #[path = "google.pubsub.v1.rs"]
        pub mod v1;
    }
}

See the example here

Dependencies

~270–660KB
~16K SLoC