Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I am getting `Proto class is is already defined in file`?

The first proto file(main.proto) in offline directory

option java_package = "com.xxx.proto";
option java_outer_classname = "Service1";
option java_multiple_files = true;

message Response {
    repeated Entity entity = 1;
}

message Entity {}

and here I have the second proto file(recent.proto) which is in recent

option java_package = "com.xxx.proto";
option java_outer_classname = "Service2";
option java_multiple_files = true;

message Response {
    repeated Entity entity = 1;
    repeated Entity.Type type =2;
}
message Entity {}

And I receive protoc: stdout: . stderr: offline/main.proto: "offline.Response.entitiy" is already defined in file "recent/recent.proto". Want to mention Entity are different for both cases, the class name is the same

like image 500
I.S Avatar asked Sep 06 '25 03:09

I.S


1 Answers

Assuming the protobuf package is the same(which in this current case is), then the messages have the same protobuf name. So it is definitely a problem.

The maven plugin may have built each file as a separate invocation to protoc, which wouldn't have noticed. The gradle plugin uses a single invocation of protoc, which has advantages and isn't something I expect to change (but others may feel differently).

If you generated these are two separate projects, then it would probably be fine. Otherwise, protobuf needs to be able to refer to the messages. That is done by name. The name is proto package + message name, potentially with nesting.

The appropriate way to do what you want is to use nested messages (or simply use different names):

message Request {
  message Foo {}
}
like image 196
Stf Kolev Avatar answered Sep 08 '25 00:09

Stf Kolev