rust-protobuf icon indicating copy to clipboard operation
rust-protobuf copied to clipboard

Reused names in different protobuf packages

Open pmorales-stat opened this issue 3 years ago • 2 comments

I cannot use rust-protobuf to compile a package of proto files, where many files are grouped into packages and packages contain overlapping message names.

Assume I have the following protobuf files in src/protos

  • Stath/Message.proto
  • Stath/A/Message.proto

The files contain a message called MyMessage:

Stath/message.proto

syntax = "proto3";

package Stath;

message MyMessage {

}

Stath/A/Message.proto

syntax = "proto3";

package Stath.A;

message MyMessage {

}

When compiled, this generate these two files:

out/protos/Message.rs out/protos/mod.rs

The first (Message.rs) is the generated one from stath.A (which means the original one from stath got overwritten). Then mod.rs contains two references, presumably from each file.

// @generated

pub mod Message;
pub mod Message;

In build.rs I have a very simple call to compile the two files:

fn main() {
    protobuf_codegen::Codegen::new()
        // Use `protoc` parser, optional.
        // .protoc()
        // All inputs and imports from the inputs must reside in `includes` directories.
        .includes(&["src/proto"])
        // Inputs must reside in some of include paths.
        .inputs(&[
            "src/proto/Stath/Message.proto",
            "src/proto/Stath/A/Message.proto",
        ])
        // Specify output directory relative to Cargo output directory.
        .cargo_out_dir("protos")
        .run()
        .expect("Code generation");
}

I tried to use the Customizer but I cannot appear to customize the output file.

I was expecting the files to be generated to specific packages and to be public, so I can use perhaps: stath::MyMessage and stath::A::MyMessage. But the flat generation does not let me use both types, since one of them is overwritten.

I think I was sort of expecting the output folder to have something like:

  • protos/Stath/message.rs
  • protos/Stath/mod.rs
  • protos/Stath/A/message.rs
  • protos/Stath/A/mod.rs

Thank you,

pmorales-stat avatar Jul 13 '22 18:07 pmorales-stat

I tried this same exercise with prost, and found an excellent feature that prost has that could be a low-cost fix for this. Prost's Config object has an "include_file" option to generate an includes file. The includes file correctly generates nested pub mod statements that include!/concat!/env!/OUT_DIR the correct files.

So decided to not use rust-protobuf, but wanted to leave this here as a possibility. I will as time permits, maybe try to implement this and send you a PR.

pmorales-stat avatar Jul 14 '22 15:07 pmorales-stat

https://docs.rs/prost-build/0.10.4/prost_build/struct.Config.html#method.include_file

pmorales-stat avatar Jul 14 '22 15:07 pmorales-stat