proto: 文件已经在不同的包中注册

huangapple go评论102阅读模式
英文:

proto: file is already registered with different packages

问题

我有两个在不同的Go包下编译的proto文件,但当我在服务器上注册它们并运行时,出现以下错误:

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"

这是test-platform代码库中的common.proto文件(位于/api文件夹中):

syntax = "proto3";
package soft.testplatform.common; // 这里我定义了一个唯一的包名!

option java_multiple_files = true;
option go_package = "github.com/soft/test-platform.go/common"; // 这里我定义了一个唯一的Go包名!

message MyMessage{
    string commandId = 1;
}

如您所见,go_packagepackage的包定义与github.com/soft/proto-asterix/asterix的包不冲突。只有.proto文件名相似(common.proto)。

我使用以下命令使用protoc和protoc-gen-go插件生成Go文件:

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

根据此文档https://developers.google.com/protocol-buffers/docs/reference/go/faq#fix-namespace-conflict,应该将包和文件名附加到检查注册冲突,但在这里似乎不是这样。

是否有人遇到过这种情况?我是否遗漏了解决此包名冲突的明显方法?


以下是我尝试过的方法:

  • 在common.proto文件中添加/删除package指令
  • 更改protoc命令以使用绝对(而不是相对)的proto_path

Protoc版本:libprotoc 3.15.7
Protoc go插件版本:protoc-gen-go v1.26.0

英文:

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

答案1

得分: 4

接受的答案不再正确。这个提交撤销了这个“错误修复”,因为它与其他gRPC实现不同。

我唯一的解决办法是重命名文件/文件夹。

英文:

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.

答案2

得分: 3

感谢@blackgreen的建议,确实,这是一个由https://go-review.googlesource.com/c/protobuf/+/301953/修复的错误。

虽然protoc-gen-go的下一个版本已经发布,但是这里有一个快速修复方法适用于你的项目:

使用修复后的protoc-gen-go:

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

修改你的go.mod文件中的导入以匹配

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

你应该可以继续进行了!

英文:

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 !

huangapple
  • 本文由 发表于 2021年5月26日 01:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/67693170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定