Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing proto with dotted name #1643

Open
Schottkyc137 opened this issue Feb 27, 2024 · 1 comment
Open

Importing proto with dotted name #1643

Schottkyc137 opened this issue Feb 27, 2024 · 1 comment

Comments

@Schottkyc137
Copy link

Schottkyc137 commented Feb 27, 2024

Bug Report

Version

tonic v0.11.0
tonic-build v0.11.0

Platform

Darwin 23.2.0 Darwin Kernel Version 23.2.0: Wed Nov 15 21:55:06 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6020 arm64

Description

I have the following two proto files:

foobar.proto

syntax = "proto3";

package foo.bar;

message Foo {}

message Bar {}

example.proto

syntax = "proto3";

package example;

import "foobar.proto";

service Example {
  rpc example(foo.bar.Foo) returns (foo.bar.Bar);
}

and a main.rs file:

use tonic::{Request, Response, Status};

use pb::*;

pub mod pb {
    tonic::include_proto!("example");
}

#[derive(Debug, Default)]
struct Service;

#[tonic::async_trait]
impl example_service_server::ExampleService for Service {
    async fn void(&self, _request: Request<Empty>) -> Result<Response<Empty>, Status> {
        todo!()
    }
}

fn main() {}

When running cargo clippy, the following output prints:

error[E0433]: failed to resolve: could not find `foo` in the crate root
  --> [...]/target/debug/build/tonic_bug_mre-913b1807619270b3/out/example.rs:88:60
   |
88 |             request: impl tonic::IntoRequest<super::super::foo::bar::Foo>,
   |                                                            ^^^ could not find `foo` in the crate root

... many more similar error messages

It seems like tonic is not set up to handle packages with dotted names (i.e. foo.bar) in imports.
If the dotted package is 'standalone', that works. Is there anything I overlooked to make this work?

@Schottkyc137
Copy link
Author

Unsure whether this is the intended solution, but one way to circumvent this is the following instead of the raw import of example:

pub mod foo {
    pub mod bar {
        tonic::include_proto!("foo.bar");
    }
}

use crate::foo::bar;

pub mod pb {
    tonic::include_proto!("foo.example");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant