导入“google/api/annotations.proto”未找到或有错误。如何将其添加为依赖项?

问题描述 投票:0回答:7

按照有关如何设置 gRPC 网关的文档,我发现自己陷入了生成 grpc 网关的第四步。

也就是说,当添加以下行时,事情就会崩溃:

import "google/api/annotations.proto";

文档说

You will need to provide the required third party protobuf files to the protoc compiler
- 但实际上并没有说明如何做到这一点。

如何添加

google/api/annotations.proto
作为依赖项?

go protocol-buffers grpc
7个回答
15
投票

我通过将 第三方 google apis 及其内容添加到我的项目的根目录中解决了这个问题。

感觉不对,但显然这受到鼓励


9
投票

我有同样的问题,我按照这个结构解决了它:

proto
├── google
│   └── api
│       ├── annotations.proto
│       └── http.proto
└── helloworld
    └── hello_world.proto

并运行命令:

protoc -I ./proto \
   --go_out ./proto --go_opt paths=source_relative \
   --go-grpc_out ./proto --go-grpc_opt paths=source_relative \
   --grpc-gateway_out ./proto --grpc-gateway_opt paths=source_relative \
   ./proto/helloworld/hello_world.proto

3
投票

我只通过复制annotations.proto和http.proto就解决了这个问题 在主要原型中:

import "Proto/google/api/annotations.proto";

和里面的annotations.proto

import "Proto/google/api/http.proto";

我的文件夹如下所示:


1
投票

如果您使用protoc生成存根,您需要确保 所需的依赖项在编译时可供编译器使用。 这些可以通过手动克隆和复制相关文件来找到 从 googleapis 存储库,并将它们提供给 protoc 跑步。您需要的文件是:

google/api/annotations.proto
google/api/field_behaviour.proto
google/api/http.proto
google/api/httpbody.proto

来自 grpc-gateway

例如在项目根目录中运行

git submodule add https://github.com/googleapis/googleapis
获取实际版本


0
投票

有时我们运行 protoc 时会出现此错误。 尝试在运行 protoc 命令时导入依赖项。

对于这种类型的结构使用

>protoc -I . -I pb/google/api --go_out . --go_opt paths=source_relative --go-grpc_out . --go-grpc_opt paths=source_relative --grpc-gateway_out . --grpc-gateway_opt paths=source_relative ./pb/*.proto

0
投票

我正在使用 Visual Studio。我的原始文件层次结构如下所示:

我必须调整 csproj 文件中项目的

ProtoRoot
属性,如下所示:

<ItemGroup>
    <Protobuf Include="Protos\authzed\api\v1\experimental_service.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\google\api\annotations.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\google\api\http.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\google\rpc\status.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\protoc-gen-openapiv2\options\annotations.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\protoc-gen-openapiv2\options\openapiv2.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\validate\validate.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\authzed\api\v1\core.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\authzed\api\v1\debug.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\authzed\api\v1\error_reason.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\authzed\api\v1\openapi.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\authzed\api\v1\permission_service.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\authzed\api\v1\schema_service.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
    <Protobuf Include="Protos\authzed\api\v1\watch_service.proto" GrpcServices="Client" ProtoRoot="Protos\" Access="Internal" />
</ItemGroup>

0
投票

对我有用的是:

import "google/api/annotations.proto";
import "google/api/http.proto";

但是我的用例是 java 和 IntelliJ。

© www.soinside.com 2019 - 2024. All rights reserved.