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

Conflict proto files with diffrent package name #717

Open
YeahhhhzZ opened this issue Apr 15, 2024 · 3 comments
Open

Conflict proto files with diffrent package name #717

YeahhhhzZ opened this issue Apr 15, 2024 · 3 comments

Comments

@YeahhhhzZ
Copy link

project directories

tree .
.
├── build.rs
├── Cargo.toml
├── demo
│   └── main.rs
└── proto
    ├── common
    │   └── header.proto
    ├── mod.rs
    └── module
        ├── header.proto
        └── items.proto

common/header.proto:

syntax = "proto2";

package common.header;

message Header {
  optional uint64 id = 1;
  optional uint64 timestamp_ms = 2;
}

module/header.proto:

syntax = "proto2";

package module.header;

message Header {
  optional string source = 1;
}

module/items.proto:

syntax = "proto2";

import "proto/common/header.proto";

package module.items;

message Items {
  optional common.header.Header header = 1;
  optional string name = 2;
}

build.rs

use protobuf_codegen::Codegen;

fn main() {
    let mut codegen = Codegen::new();
    codegen
        .protoc()
        .cargo_out_dir("proto")
        .includes(&["./"])
        .inputs(&[
            "./proto/common/header.proto",
            "./proto/module/header.proto",
            "./proto/module/items.proto",
        ]);
    if let Ok(protoc_env) = std::env::var("PROTOC") {
        codegen.protoc_path(std::path::Path::new(&protoc_env));
    }
    codegen.run_from_script();
}

build output:
image

mod.rs:

// @generated

pub mod header;
pub mod header;
pub mod items;
@YeahhhhzZ
Copy link
Author

When compiled using prost(https://github.com/tokio-rs/prost), the output file is as follows:
image

build.rs:

use prost_build;

fn main() {
    let mut prost_build = prost_build::Config::new();
    prost_build.include_file("proto_mod.rs");
    prost_build
        .compile_protos(
            &[
                "proto/common/header.proto",
                "proto/module/header.proto",
                "proto/module/items.proto",
            ],
            &["."],
        )
        .unwrap();
}

proto_mod.rs:

// This file is @generated by prost-build.
pub mod common {
    pub mod header {
        include!(concat!(env!("OUT_DIR"), "/common.header.rs"));
    }
}
pub mod module {
    pub mod header {
        include!(concat!(env!("OUT_DIR"), "/module.header.rs"));
    }
    pub mod items {
        include!(concat!(env!("OUT_DIR"), "/module.items.rs"));
    }
}

@jokemanfire
Copy link

jokemanfire commented Jul 17, 2024

Hello , I also meet this problem . Is it resolved? Sorry but my problem is different protos , but generate mod have same name.

@jokemanfire
Copy link

#732

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

2 participants