Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

proto: file is already registered with different packages

Tags:

go

proto

protoc

I have 2 proto compiled under different go packages but when I register them in a a server and run it, I get :

panic: proto: file "common.proto" is already registered
    previously from: "github.com/soft/test-platform.go/common"
    currently from:  "github.com/soft/proto-asterix/asterix"

Here is common.proto in test-platform repository (in /api folder) :

syntax = "proto3";
package soft.testplatform.common; // here I do defint a UNIQUE package name !

option java_multiple_files = true;
option go_package = "github.com/soft/test-platform.go/common"; // Here I do define a unique go package name !

message MyMessage{
    string commandId = 1;
}

As you can see, the package definition for go_package and package do not collide with package from github.com/soft/proto-asterix/asterix. Only the .proto filenames are similar (common.proto).

I generate go files with protoc an protoc-gen-go plugin using the following command :

protoc \
--proto_path=../test-platform/api/   \
--go_out=./common --go_opt=paths=source_relative \
../test-platform/api/common.proto

As per documentation here https://developers.google.com/protocol-buffers/docs/reference/go/faq#fix-namespace-conflict the package and filename should be appended to check for a registration conflict but it does not seem to be the case here.

Has anyone encountered such behavior ? Do I miss something obvious to resolve this package name collision ?


Here is what I tried :

  • Adding/removing package instruction to common.proto file
  • Change protoc command to use an absolute (and not relative) proto_path

Protoc version : libprotoc 3.15.7 Protoc go plugin version : protoc-gen-go v1.26.0

like image 441
Antonin Avatar asked May 25 '21 17:05

Antonin


People also ask

Can a proto file have multiple services?

Current implementation doesn't support a proto file that has multiple services in it(Basically this should enable running multiple services on same port).

How do I import Proto from another project?

In the service project, you need to add a proto file, then add the Protobuf element to the ItemGroup. Then you need to build the project. After that, in the client's project, you need to add the Protobuf element to the ItemGroup and build the project.

What is file format proto?

A . proto file is similar to a JSON file in that it represents structured data, but you can run a compiler on the . proto file to generate code that can read and write the data in the programming language of your choice. For more information about protocol buffers, see Protocol Buffer Developer Guide on Google's site.


2 Answers

Thanks to @blackgreen suggestion, indeed, this was a bug fixed by https://go-review.googlesource.com/c/protobuf/+/301953/

While the next release of protoc-gen-go is out, here is a quick fix for your projects :

Use the fixed protoc-gen-go :

go install google.golang.org/protobuf/cmd/protoc-gen-go@febffdd

Change your imports in your go.mod to match

google.golang.org/protobuf v1.26.1-0.20210525005349-febffdd88e85

You should be good to go !

like image 128
Antonin Avatar answered Oct 23 '22 22:10

Antonin


The accepted answer is not correct anymore. This commit reverted the "bugfix" as it was different from other gRPC implementations.

My only way to fix this was to rename the file/folder.

like image 28
Martin Rauscher Avatar answered Oct 23 '22 23:10

Martin Rauscher