hyperium / tonic

A native gRPC client & server implementation with async/await support.
https://docs.rs/tonic
MIT License
10.02k stars 1.02k forks source link

Helloworld tutorial is outdated? #765

Closed zhiburt closed 3 months ago

zhiburt commented 3 years ago

https://github.com/hyperium/tonic/blob/master/examples/helloworld-tutorial.md

Following the reference I was getting

error: environment variable `OUT_DIR` not defined
 --> src/server.rs:9:5
  |
9 |     tonic::include_proto!("helloworld");
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Then I took a look at build.rs used in examples dir and in a bit different.

And to fix it it was necessary to change a build.rs and Cargo.toml.

    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
    tonic_build::configure()
        .file_descriptor_set_path(out_dir.join("helloworld_descriptor.bin"))
        .compile(&["proto/helloworld/helloworld.proto"], &["proto"])
        .unwrap();
# include `prost`
[dependencies]
prost = "*"

# include `prost`
[build-dependencies]
tonic-build = { path = "../tonic-build", features = ["prost"] }
CommoDor64 commented 3 years ago

Rust build scripts are being expected to be in the root folder, i.e a sibling folder to src. Cargo then will set OUT_DIR for the compiler to use. So if you insist to move the build script somewhere else, you'll have to set it manually. But if you are willing to put it there, it still works, so the tutorial is correct.

zhiburt commented 3 years ago

Rust build scripts are being expected to be in the root folder, i.e a sibling folder to src. Cargo then will set OUT_DIR for the compiler to use.

That's right but as I remember there will be an erorr because of prost dependency.

I mean that In the tutorial there's snippets like these.

    // don't remember func name
    tonic_build::.compile_prots("proto/helloworld.proto").unwrap();
# include `prost`
[dependencies]

# include `prost`
[build-dependencies]
tonic-build = { path = "../tonic-build", features = [] }

And I was getting errors unless I changed them to the following (Which I found in your examples folder)

    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
    tonic_build::configure()
        .file_descriptor_set_path(out_dir.join("helloworld_descriptor.bin"))
        .compile(&["proto/helloworld.proto"], &["proto"])
        .unwrap();
# include `prost`
[dependencies]
prost = "*"

# include `prost`
[build-dependencies]
tonic-build = { path = "../tonic-build", features = ["prost"] }
zhiburt commented 3 years ago

To be precise here it is

When you keep things as described in tutorial

https://github.com/hyperium/tonic/blame/master/examples/helloworld-tutorial.md#L101-L122

https://github.com/hyperium/tonic/blame/master/examples/helloworld-tutorial.md#L130-L134

error[E0433]: failed to resolve: could not find `prost` in the list of imported crates
 --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:8:30
  |
8 | #[derive(Clone, PartialEq, ::prost::Message)]
  |                              ^^^^^ could not find `prost` in the list of imported crates

error[E0433]: failed to resolve: could not find `prost` in the list of imported crates
 --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:2:30
  |
2 | #[derive(Clone, PartialEq, ::prost::Message)]
  |                              ^^^^^ could not find `prost` in the list of imported crates

error: cannot find attribute `prost` in this scope
 --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:4:7
  |
4 |     #[prost(string, tag = "1")]
  |       ^^^^^

error: cannot find attribute `prost` in this scope
  --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:10:7
   |
10 |     #[prost(string, tag = "1")]
   |       ^^^^^

error[E0433]: failed to resolve: could not find `prost` in the list of imported crates
 --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:5:17
  |
5 |     pub name: ::prost::alloc::string::String,
  |                 ^^^^^ could not find `prost` in the list of imported crates

error[E0433]: failed to resolve: could not find `prost` in the list of imported crates
  --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:11:20
   |
11 |     pub message: ::prost::alloc::string::String,
   |                    ^^^^^ could not find `prost` in the list of imported crates

error[E0277]: the trait bound `HelloRequest: prost::message::Message` is not satisfied
  --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:87:24
   |
87 |             self.inner.unary(request.into_request(), path, codec).await
   |                        ^^^^^ the trait `prost::message::Message` is not implemented for `HelloRequest`
   |
   = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloRequest, _>`

error[E0277]: the trait bound `HelloReply: prost::message::Message` is not satisfied
  --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:87:24
   |
87 |             self.inner.unary(request.into_request(), path, codec).await
   |                        ^^^^^ the trait `prost::message::Message` is not implemented for `HelloReply`
   |
   = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloRequest, HelloReply>`

error[E0277]: the trait bound `HelloReply: Default` is not satisfied
  --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:87:24
   |
87 |             self.inner.unary(request.into_request(), path, codec).await
   |                        ^^^^^ the trait `Default` is not implemented for `HelloReply`
   |
   = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloRequest, HelloReply>`

error[E0277]: the trait bound `HelloReply: prost::message::Message` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:170:40
    |
170 |                         let res = grpc.unary(method, req).await;
    |                                        ^^^^^ the trait `prost::message::Message` is not implemented for `HelloReply`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloRequest: prost::message::Message` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:170:40
    |
170 |                         let res = grpc.unary(method, req).await;
    |                                        ^^^^^ the trait `prost::message::Message` is not implemented for `HelloRequest`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloRequest: Default` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:170:40
    |
170 |                         let res = grpc.unary(method, req).await;
    |                                        ^^^^^ the trait `Default` is not implemented for `HelloRequest`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloReply: prost::message::Message` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:166:72
    |
166 |                         let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
    |                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `prost::message::Message` is not implemented for `HelloReply`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloRequest: prost::message::Message` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:166:72
    |
166 |                         let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
    |                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `prost::message::Message` is not implemented for `HelloRequest`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloRequest: Default` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:166:72
    |
166 |                         let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
    |                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `HelloRequest`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloReply: prost::message::Message` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:166:40
    |
166 |                         let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `prost::message::Message` is not implemented for `HelloReply`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloRequest: prost::message::Message` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:166:40
    |
166 |                         let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `prost::message::Message` is not implemented for `HelloRequest`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloRequest: Default` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:166:40
    |
166 |                         let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `HelloRequest`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error: aborting due to 18 previous errors

Some errors have detailed explanations: E0277, E0433.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `examples`

To learn more, run the command again with --verbose.
LucioFranco commented 3 years ago

I don't really see what is missing in the tutorial, but I would accept a PR with suggestions you might have.

small-lei commented 3 years ago

To be precise here it is

When you keep things as described in tutorial

https://github.com/hyperium/tonic/blame/master/examples/helloworld-tutorial.md#L101-L122

https://github.com/hyperium/tonic/blame/master/examples/helloworld-tutorial.md#L130-L134

error[E0433]: failed to resolve: could not find `prost` in the list of imported crates
 --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:8:30
  |
8 | #[derive(Clone, PartialEq, ::prost::Message)]
  |                              ^^^^^ could not find `prost` in the list of imported crates

error[E0433]: failed to resolve: could not find `prost` in the list of imported crates
 --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:2:30
  |
2 | #[derive(Clone, PartialEq, ::prost::Message)]
  |                              ^^^^^ could not find `prost` in the list of imported crates

error: cannot find attribute `prost` in this scope
 --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:4:7
  |
4 |     #[prost(string, tag = "1")]
  |       ^^^^^

error: cannot find attribute `prost` in this scope
  --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:10:7
   |
10 |     #[prost(string, tag = "1")]
   |       ^^^^^

error[E0433]: failed to resolve: could not find `prost` in the list of imported crates
 --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:5:17
  |
5 |     pub name: ::prost::alloc::string::String,
  |                 ^^^^^ could not find `prost` in the list of imported crates

error[E0433]: failed to resolve: could not find `prost` in the list of imported crates
  --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:11:20
   |
11 |     pub message: ::prost::alloc::string::String,
   |                    ^^^^^ could not find `prost` in the list of imported crates

error[E0277]: the trait bound `HelloRequest: prost::message::Message` is not satisfied
  --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:87:24
   |
87 |             self.inner.unary(request.into_request(), path, codec).await
   |                        ^^^^^ the trait `prost::message::Message` is not implemented for `HelloRequest`
   |
   = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloRequest, _>`

error[E0277]: the trait bound `HelloReply: prost::message::Message` is not satisfied
  --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:87:24
   |
87 |             self.inner.unary(request.into_request(), path, codec).await
   |                        ^^^^^ the trait `prost::message::Message` is not implemented for `HelloReply`
   |
   = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloRequest, HelloReply>`

error[E0277]: the trait bound `HelloReply: Default` is not satisfied
  --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:87:24
   |
87 |             self.inner.unary(request.into_request(), path, codec).await
   |                        ^^^^^ the trait `Default` is not implemented for `HelloReply`
   |
   = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloRequest, HelloReply>`

error[E0277]: the trait bound `HelloReply: prost::message::Message` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:170:40
    |
170 |                         let res = grpc.unary(method, req).await;
    |                                        ^^^^^ the trait `prost::message::Message` is not implemented for `HelloReply`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloRequest: prost::message::Message` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:170:40
    |
170 |                         let res = grpc.unary(method, req).await;
    |                                        ^^^^^ the trait `prost::message::Message` is not implemented for `HelloRequest`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloRequest: Default` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:170:40
    |
170 |                         let res = grpc.unary(method, req).await;
    |                                        ^^^^^ the trait `Default` is not implemented for `HelloRequest`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloReply: prost::message::Message` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:166:72
    |
166 |                         let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
    |                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `prost::message::Message` is not implemented for `HelloReply`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloRequest: prost::message::Message` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:166:72
    |
166 |                         let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
    |                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `prost::message::Message` is not implemented for `HelloRequest`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloRequest: Default` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:166:72
    |
166 |                         let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
    |                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `HelloRequest`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloReply: prost::message::Message` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:166:40
    |
166 |                         let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `prost::message::Message` is not implemented for `HelloReply`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloRequest: prost::message::Message` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:166:40
    |
166 |                         let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `prost::message::Message` is not implemented for `HelloRequest`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error[E0277]: the trait bound `HelloRequest: Default` is not satisfied
   --> /home/mzhiburt/projects/tonic/target/debug/build/examples-020cf5705affc3b4/out/helloworld.rs:166:40
    |
166 |                         let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `HelloRequest`
    |
    = note: required because of the requirements on the impl of `Codec` for `ProstCodec<HelloReply, HelloRequest>`

error: aborting due to 18 previous errors

Some errors have detailed explanations: E0277, E0433.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `examples`

To learn more, run the command again with --verbose.

I have the same problem, I solve it , add prost = "0.8" to cargo.toml

zhiburt commented 3 years ago

I don't really see what is missing in the tutorial,

That's what I meant (I guess), that probably it makes sense to note or make it more clear that as @small-lei mentioned

I have the same problem, I solve it , add prost = "0.8" to cargo.toml

prost dependency is required.

davidpdrsn commented 3 years ago

The prost thing is known https://github.com/hyperium/tonic/pull/791 👀

LucioFranco commented 3 years ago

Yeah this should be fixed on master right now and in the next release.

chokosabe commented 2 years ago

Getting the same issue; following the helloworld tutorial example to the letter.

LucioFranco commented 2 years ago

@chokosabe can you post your cargo.toml file? The versions in the tutorial look fine to me.

frederikhors commented 1 year ago

Same issue here after the routeguide tutorial. And I added the prost dep but the error is still there.

Narayanbhat166 commented 1 year ago

Faced the same problem in helloworld turorial, Changed the prost and tonic_build versions and the project compiles

[package]
name = "helloworld-tonic"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[bin]] # Bin to run the HelloWorld gRPC server
name = "helloworld-server"
path = "src/server.rs"

[[bin]] # Bin to run the HelloWorld gRPC client
name = "helloworld-client"
path = "src/client.rs"

[dependencies]
tonic = "0.7"
prost = "0.10"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }

[build-dependencies]
tonic-build = "0.7"
vishnu-kumar commented 1 year ago

I solved it as follows: Looked at Cargo.lock and it was having multiple version of prost dependency like [[package]] name = "prost" version = "0.11.9"

[[package]] name = "prost" version = "0.8.0"

So seems like they are conflicting and right version is not getting picked. So I changed the dependencies of prost version to prost = "0.11.9" and it build successfully.

HMasataka commented 4 months ago

I did confirm the tutorial works with the latest version by to configure Cargo.toml as follows:

[package]
name = "helloworld-tonic"
version = "0.1.0"
edition = "2021"

[[bin]] # Bin to run the HelloWorld gRPC server
name = "helloworld-server"
path = "src/server.rs"

[[bin]] # Bin to run the HelloWorld gRPC client
name = "helloworld-client"
path = "src/client.rs"

[dependencies]
tonic = "0.12"
prost = "0.13"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }

[build-dependencies]
tonic-build = "0.12"
HMasataka commented 3 months ago

@LucioFranco @zhiburt Looks like lukem570 fixed it. Can you close this issue?