I'm new to Rust and trying to create a simple gRPC application. Here is the directory structure:
grpc-protobuf
├── Cargo.toml
├── build.rs
├── proto
│ ├── hello
│ │ └── hello.proto
│ └── messages
│ └── hello.proto
└── src
└── lib.rs
I have the protobuf messages defined under proto/messages/hello.proto and below is the file content:
syntax = "proto3";
package proto.messages.v1;
message HelloRequest {
string name = 1;
}
message HelloResponse {
string message = 1;
}
The service that uses the messages is defined as proto/hello/hello.proto:
syntax = "proto3";
package hello;
import "messages/hello.proto";
service Greeter {
rpc SayHello (proto.messages.v1.HelloRequest) returns (proto.messages.v1.HelloResponse) {}
}
And here is how I'm creating a package:
pub mod hello {
tonic::include_proto!("hello");
}
pub mod messages {
tonic::include_proto!("proto.messages.v1");
}
As I try to build the package I get the following errors:
➜ cargo build -p grpc-protobuf
Compiling grpc-protobuf v0.1.0 (/Users/gaurav.gahlot/workspace/rusty/rust-grpc/grpc-protobuf)
error[E0433]: failed to resolve: could not find `proto` in the crate root
--> /Users/gaurav.gahlot/workspace/rusty/rust-grpc/target/debug/build/grpc-protobuf-6fc85551b5cb3a84/out/hello.rs:91:31
|
91 | super::super::proto::messages::v1::HelloRequest,
| ^^^^^ could not find `proto` in the crate root
error[E0433]: failed to resolve: could not find `proto` in the crate root
--> /Users/gaurav.gahlot/workspace/rusty/rust-grpc/target/debug/build/grpc-protobuf-6fc85551b5cb3a84/out/hello.rs:94:43
|
94 | tonic::Response<super::super::proto::messages::v1::HelloResponse>,
| ^^^^^ could not find `proto` in the crate root
error[E0433]: failed to resolve: could not find `proto` in the crate root
--> /Users/gaurav.gahlot/workspace/rusty/rust-grpc/target/debug/build/grpc-protobuf-6fc85551b5cb3a84/out/hello.rs:124:51
|
I've replicated your file structure (I'm using
bin
rather thanlib
):proto/hello/hello.proto
:proto/messages/messages.proto
:build.rs
:main.rs
:Coming from using gRPC mostly with Go and some Python, I like how
tonic
works but found theprotoc
generation initially confusing.tonic-build
usesOUT_DIR
as the location for theprotoc
generated sources. You can:Yielding:
Update
When you have a nested
package
hierarchy, e.g.package messages.v1
, there are several changes in particular to the rustmod
hierarchy to reflect thepackage
hierarchy:proto/hello/hello.proto
:proto/messages/v1/messages.proto
:build.rs
:main.rs
: