I'm trying to build a simple gRPC client in rust using crates prost and tonic. My proto definitions are quite simple, but I suddenly stuck with using messages imported from other proto.
// file src/protos/types.proto
syntax = "proto3";
package Types;
message Message1 {
uint32 value1 = 1;
bytes value2 = 2;
}
message Message2 {
uint32 value1 = 1;
uint32 value2 = 2;
uint32 value3 = 3;
uint32 value4 = 4;
}
// file src/protos/service.proto
syntax = "proto3";
import "types.proto";
package Service;
service Worker {
rpc Do (Request) returns (Reply);
}
message Request {
Types.Message1 message1 = 1;
Types.Message2 message2 = 2;
}
message Reply {
bool success = 1;
}
My build.rs
is very straightforward:
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::compile_protos("src/protos/types.proto")?;
tonic_build::compile_protos("src/protos/service.proto")?;
Ok(())
}
The problem begins when I including protos in main.rs
:
pub mod service {
tonic::include_proto!("types");
tonic::include_proto!("service");
}
The compilation fails with the following error:
--> D:\temp\rust-proto\target\debug\build\rust-proto-11c38604fbc7ce30\out/service.rs:4:48
|
4 | pub message1: ::std::option::Option<super::types::Message1>,
| ^^^^^ maybe a missing crate `types`?
What might be wrong here?! I uploaded my playground project to github in case it might be useful.
I don't know much about tonic, but you must put the includes in modules corresponding to their proto packages, e.g.:
pub mod types {
tonic::include_proto!("types");
}
pub mod service {
tonic::include_proto!("service");
}
fn main() {
let msg = types::Message1::default();
println!("Hello, world! {:?}", msg);
}
This compiles correctly.
BTW, you can check the generated rust code at the following location in your setup here:
D:\temp\rust-proto\target\debug\build\rust-proto-11c38604fbc7ce30\out