Protocol Buffers 无效嵌入描述符问题

发布于 2024-09-06 11:04:53 字数 2169 浏览 2 评论 0原文

我在运行时遇到一些生成的协议缓冲区类的问题。

我的项目布局如下:

module/
  protobuf-api/
    proto/
      com/foo/api/Service.proto
      com/foo/shared/Shared.proto
      org/bar/api/Message1.proto
      org/bar/api/Message2.proto

Service.proto 文件依赖于 Shared.proto 和一些 Message*.proto 文件。从 protobuf-api 目录中,我运行以下命令进行编译:
<代码>查找 . -name *.proto -exec protoc --java_out=java -I=proto {} \;

当我尝试运行我的服务时,出现以下异常:

java.lang.ExceptionInInitializerError
    at com.linkedin.history.api.protobuf.HistoryServiceProtos$HistoryServiceQuery.(HistoryServiceProtos.java:544)
    at com.linkedin.history.api.serializer.HistoryServiceSerializer.serialize(HistoryServiceSerializer.java:47)
     at test.history.serializer.TestSerializer.testHistoryServiceQuery(TestSerializer.java:38)
    at test.fwk.util.core.BaseTestSuiteCore.run(BaseTestSuiteCore.java:304)
     at test.fwk.util.core.BaseTestSuiteConf.run(BaseTestSuiteConf.java:186)
    at test.fwk.lispring.BaseTestSuite.run(BaseTestSuite.java:232)
    at test.fwk.lispring.BaseTestSuite.callAppropriateRun(BaseTestSuite.java:265)
    at test.fwk.util.core.BaseTestSuiteCore.run(BaseTestSuiteCore.java:199)
Caused by: java.lang.IllegalArgumentException: Invalid embedded descriptor for "com/linkedin/history/api/protobuf/HistoryService.proto".
     at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:268)
     at com.linkedin.history.api.protobuf.HistoryServiceProtos.(HistoryServiceProtos.java:1794)
Caused by: com.google.protobuf.Descriptors$DescriptorValidationException: com/linkedin/history/api/protobuf/HistoryService.proto: Dependencies passed to FileDescriptor.buildFrom() don't match those listed in the FileDescriptorProto.
     at com.google.protobuf.Descriptors$FileDescriptor.buildFrom(Descriptors.java:221)
     at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:266)

我已阅读帖子 此处,但我认为我做的一切都是正确的。关于为什么我遇到初始化程序错误有什么建议吗?我正在使用相同的 -I 标志编译所有内容。

I'm having some problems at runtime with some of my generated protocol buffer classes.

My project layout is as follows:

module/
  protobuf-api/
    proto/
      com/foo/api/Service.proto
      com/foo/shared/Shared.proto
      org/bar/api/Message1.proto
      org/bar/api/Message2.proto

The Service.proto file depends on Shared.proto and some of the Message*.proto files. From the protobuf-api directory, I run the following command to compile:
find . -name *.proto -exec protoc --java_out=java -I=proto {} \;

When I attempt to run my Service, I get the following exception:

java.lang.ExceptionInInitializerError
    at com.linkedin.history.api.protobuf.HistoryServiceProtos$HistoryServiceQuery.(HistoryServiceProtos.java:544)
    at com.linkedin.history.api.serializer.HistoryServiceSerializer.serialize(HistoryServiceSerializer.java:47)
     at test.history.serializer.TestSerializer.testHistoryServiceQuery(TestSerializer.java:38)
    at test.fwk.util.core.BaseTestSuiteCore.run(BaseTestSuiteCore.java:304)
     at test.fwk.util.core.BaseTestSuiteConf.run(BaseTestSuiteConf.java:186)
    at test.fwk.lispring.BaseTestSuite.run(BaseTestSuite.java:232)
    at test.fwk.lispring.BaseTestSuite.callAppropriateRun(BaseTestSuite.java:265)
    at test.fwk.util.core.BaseTestSuiteCore.run(BaseTestSuiteCore.java:199)
Caused by: java.lang.IllegalArgumentException: Invalid embedded descriptor for "com/linkedin/history/api/protobuf/HistoryService.proto".
     at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:268)
     at com.linkedin.history.api.protobuf.HistoryServiceProtos.(HistoryServiceProtos.java:1794)
Caused by: com.google.protobuf.Descriptors$DescriptorValidationException: com/linkedin/history/api/protobuf/HistoryService.proto: Dependencies passed to FileDescriptor.buildFrom() don't match those listed in the FileDescriptorProto.
     at com.google.protobuf.Descriptors$FileDescriptor.buildFrom(Descriptors.java:221)
     at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:266)

I've read the post here but I think I'm doing everything correctly. Any suggestions on why I'm having the initializer errors? I'm compiling everything with the same -I flag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘴硬脾气大 2024-09-13 11:04:53

我怀疑问题在于,当您查找 proto 文件时,您已经给了它完整路径,例如 proto/com/foo/api/Service.proto 但当它引用它时通过包含目录,它使用 com/foo/api/Service.proto

简单的修复 - 从 proto 目录运行它:

find . -name *.proto -exec protoc --java_out=../java -I=. {} \;

我必须承认我不记得 的很多细节protoc (我真的应该这样做),但我怀疑这会起作用。

另一种可行的替代方案:

protoc --java_out=java `find . -name '*.proto'`

即将所有 proto 文件传递​​到对 protoc 的单个调用中。

I suspect that the problem is that when you're finding the proto file, you've given it the full path, e.g. proto/com/foo/api/Service.proto but when it refers to it via the include directory, it's using com/foo/api/Service.proto

Simple fix - run this from the proto directory:

find . -name *.proto -exec protoc --java_out=../java -I=. {} \;

I must admit I can't remember a lot of the details of protoc (which I really should) but I suspect that will work.

Another alternative which may work:

protoc --java_out=java `find . -name '*.proto'`

i.e. pass all the proto files into a single call to protoc.

离笑几人歌 2024-09-13 11:04:53

我在 C# 中遇到了相同的错误类型,这就是我的问题:我在项目的预构建步骤中调用了 protoc 。在那里,我使用了 Visual Studio 内置宏,例如 $(SolutionDir)$(ProjectDir) 来检索必要的路径。由于我引用了其他项目中的 *.proto 文件,因此我使用了两个 --proto_path 选项:一个用于根路径(用于解析 import 路径) ) 和文件本身。我的解决方案文件位于根目录的子目录中,因此我使用相对路径 .. 来访问根目录。 Proto 文件始终位于特定项目的子目录 gen 中。总而言之,该命令是这样的:

protoc.exe --proto_path=$(SolutionDir).. --proto_path=$(ProjectDir)gen  $(ProjectDir)gen\DemoFile.proto

它编译得很好,但我在运行时调用 CreateBuilder() 方法时遇到了 System.TypeInitializationException 。问题是,由于相对路径组件,两个路径 $(SolutionDir)..$(ProjectDir) (尽管实际上指向同一目录)具有不同的文本表示<代码>..。我通过始终使用相同的路径解决了这个问题,如下所示:

protoc.exe --proto_path=$(SolutionDir).. $(SolutionDir)..\My\Demo\Project\Directory\gen\DemoFile.proto

我花了将近 3 天的时间来缩小范围并识别问题,所以我在这里分享我的解决方案,希望它能为某人节省一些时间。

I had the same error type in C# and here was my problem: I called the protoc in a pre-build step in my project. There I used Visual Studio built-in macros like $(SolutionDir) and $(ProjectDir) to retrieve necessary paths. Since I referenced *.proto files from other projects, I used two --proto_path options: one for the root path (to resolve import paths) and one for the file itself. My solution file was inside a subdirectory of the root directory, so I used the relative path .. to get to the root. Proto files are always in subdirectory gen of the particular project. All in all, the command was like this:

protoc.exe --proto_path=$(SolutionDir).. --proto_path=$(ProjectDir)gen  $(ProjectDir)gen\DemoFile.proto

It compiled fine, but I got the System.TypeInitializationException at runtime on calling CreateBuilder() method. The problem was that both paths $(SolutionDir).. and $(ProjectDir) (though effectively pointing to the same directory) had different textual representation due to the relative path component ... I solved the problem by consistently using the same path like this:

protoc.exe --proto_path=$(SolutionDir).. $(SolutionDir)..\My\Demo\Project\Directory\gen\DemoFile.proto

It cost me almost 3 days to narrow down and recognize the problem, so I share my solution here in hope that it will save some time for someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文