tonic_build::prost::Builder proto_path function panics

Created on 20 May 2020  路  7Comments  路  Source: hyperium/tonic

Bug Report

Version

tonic-build = "0.2.0"
tonic = "0.2.1"
prost = "0.6"

Platform

Linux R1 5.3.0-51-generic #44~18.04.2-Ubuntu SMP Thu Apr 23 14:27:18 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

Crates

tonic-build

Description

I added the following code in build.rs:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    tonic_build::configure()
        .build_server(false)
        .out_dir("proto/generated/")
        .proto_path("../proto/generated")
        .compile(&["proto/service.proto"], &["proto/"])?;

    Ok(())
}

I expected that I will be able to use the package "service" in following way in main.rs:

mod service {
    tonic::include_proto!("service");
}

Instead, the build failed with the following message:

--- stderr
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("expected identifier")', src/libcore/result.rs:1165:5

Build, and everything else works if I'm not using proto_path() in Builder, but then I can't use include_proto! (I have to use include! instead).
Bug is probably somewhere in the Builder in compiling protos using proto_path(); or maybe I'm using it wrong.

All 7 comments

Would you be able to show me what your proto looks like? (sorry for the late reply been on vacation)

@petratbtl Using out_dir will generate the proto files outside of OUT_DIR, so you will need to follow the advice here and use include!.

include_proto! itself just does:

include!(concat!(env!("OUT_DIR"), concat!("/", $package, ".rs")));

@LucioFranco I was on the vacation, too so I haven't seen this, sorry. I'll find proto and paste it here. @ajguerrer tried that also, works but only if I comment out the line with proto_path.The documentation states the following for proto_path: "Set the path to where tonic will search for the Request/Response proto structs live relative to the module where you call include_proto!." So from this quote, include_proto! is supposed to be used with proto_path set relative to the module using include_proto? Or I'm getting it wrong.

Would you be able to show me what your proto looks like? (sorry for the late reply been on vacation)

syntax = "proto3";

package service.proto;


service Greeter {
    // Sends a greeting
    rpc SayHello (HelloRequest) returns (HelloReply) {}
  }

  // The request message containing the user's name.
  message HelloRequest {
    string name = 1;
  }

  // The response message containing the greetings
  message HelloReply {
    string message = 1;
  }

Here you go, it's proto from examples...

@petratbtl I think proto_path is for situations in which the messages are in a separate location from the service that makes use of them.

Suppose the generated messages are output to some other module (or crate) messages::hello. Then you might need to generate the service with .proto_path("messages::hello") so that the messages can be found within the generated service code.

For your purposes, I don't think proto_path is necessary. If you ever end up using it, then the path arguments should be module path syntax, not file path syntax.

I just started looking at tonic, so I'm a little unsure if that's how this feature was intended to be used but I'm sure @LucioFranco can clear up my mistakes if needed.

Yup, that is correct, it is to be used to point to a different _module_ path where structs with the correct names exist.

@LucioFranco & @ajguerrer thanks for explanation and help! I'll close the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dangerousplay picture dangerousplay  路  10Comments

Jasper-Bekkers picture Jasper-Bekkers  路  6Comments

blittable picture blittable  路  9Comments

LucioFranco picture LucioFranco  路  5Comments

pranjalssh picture pranjalssh  路  4Comments