tonic icon indicating copy to clipboard operation
tonic copied to clipboard

The trait `Message` is not implemented

Open kurudj opened this issue 1 year ago • 5 comments

Bug Report

Version

│ │ └── tonic v0.12.1 │ │ └── tonic-build v0.12.1 │ ├── tonic v0.12.1 (*)

Platform

6.1.82-99.168.amzn2023.x86_64

Description

Upon generating Rust files from proto files as well as a service, the service build yields an issue with Message trait not being implemented for a struct generated from a proto message, despite the Rust file explicitly having a ::prost::Message derive.

I tried this code:

tonic_build::configure()
        .build_server(true)
        .out_dir("src")
        .type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
        .compile(&["MyStruct.proto"], &["src/raw/structs"])?;*/

tonic_build::configure().build_server(true).out_dir("src").compile(&["MyStructService.proto"], &["src/raw/structs"])?;

Where the Cargo.toml is:

[dependencies]
tonic = "0.12"
prost = "0.13"
serde = { version = "1.0", features = ["derive"] }

[build-dependencies]
tonic-build = "0.12"

This yields a MyStruct.rs with my main message as a struct:

#[derive(serde::Serialize, serde::Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct MyStruct {

Within the service the following code throws the following errors:

error[E0277]: the trait bound `MyStruct: Message` is not satisfied
    |
242 |                         let mut grpc = tonic::server::Grpc::new(codec)
    |                                        ------------------------ ^^^^^ the trait `Message` is not implemented for `MyStruct`, which is required by `ProstCodec<_, _>: Codec`
    |                                        |
    |                                        required by a bound introduced by this call

kurudj avatar Aug 06 '24 16:08 kurudj

I had a similar issue, with 170 or so build errors related to prost::Message and other traits. What solved it for me was adding prost to the project dependencies. Looks like a similar thing to me, so maybe that'll help.

Frank-Krick avatar Aug 10 '24 10:08 Frank-Krick

Have the same problem in my project. Maybe somebody can share an any workaround to fix this problem?

dmitrygrigoryev avatar Oct 03 '24 11:10 dmitrygrigoryev

Does adding prost to your dependencies help? cargo add prost

Frank-Krick avatar Oct 03 '24 11:10 Frank-Krick

Combination of tonic 0.9.2 and prost/prost-types 0.11.9 works! I think there were a mess between dependencies, cause one of my dependencies locks the tonic version to 0.9.2. It is just proposal.

My Cargo.toml with newest versions of dependencies was:

[dependencies]
prost = "0.13.3"
prost-types = "0.13.3"
dependency_with_tonic0.9.2 = { path = "../../libraries/dependency_with_tonic0.9.2", default-features = false}
tonic = "0.12.3"

So, yes I've tried to add prost to dependencies, in my case it did not help(.

dmitrygrigoryev avatar Oct 03 '24 11:10 dmitrygrigoryev

Just adding prost then everything works in 0.12.3

H-0-O avatar Dec 27 '24 20:12 H-0-O

I know this is well past due time: I was hit by same problem "the trait Message is not implemented for...".

In my case it was tonic and tonic-build in v0.13.1, but prost in v0.14.1. The cause was version mismatch between prost and tonic*. When I forced prost to v.0.13, the project built flawlessly.

p-kraszewski avatar Jul 18 '25 11:07 p-kraszewski

This is an expected error message when prost and tonic do not match. Not much we can do to improve this message as it comes from the compiler.

LucioFranco avatar Jul 21 '25 12:07 LucioFranco

I wish https://docs.rs/tonic-build/latest/tonic_build/ was explicit about that versions must match.

My understanding of the

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

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

was that they don't have to - that's why they used separate <tonic-version> and <prost-version>. There is not a single word about version matching throughout the docs.

p-kraszewski avatar Jul 21 '25 13:07 p-kraszewski