3

I am using protobuf java, with following .proto

// service1.proto
option java_package = "package";
option java_outer_classname = "Proto1";
message M {
   ... // some definition
}

and

// service2.proto
option java_package = "package";
option java_outer_classname = "Proto2";
message M {
   ... // some different definition
}

while compile, error is thrown in service2.proto saying that "M" is already defined in service1.proto

But from package and generated code they should be package.Proto1.M and package.Proto2.M, is this conflict?

Litchy
  • 623
  • 7
  • 23
  • You need to give them different `package` names. These are essentially proto namespaces. You have two `M`s in the same namespace, in this case the default. That's an error. – Gene Dec 17 '21 at 02:32

1 Answers1

3

The "package" is also a .proto concept (not just a language/framework concept); if you need to have both schemas involved in anything, it may be useful to add

package Proto1;

to service1.proto and

package Proto2;

to service2.proto


Alternatively, if the M is actually the same in both places: move M to a different single file, and use import from both service1.proto and service2.proto

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • This works, and I found the detailed doc: https://developers.google.com/protocol-buffers/docs/proto3#packages – Litchy Dec 17 '21 at 06:44