Import "google/api/annotations.proto" was not found or had errors. How do I add it as a dependency?
Asked Answered
P

9

20

Following the docs on how to set up a gRPC gateway, I find myself stuck at step four of generating the grpc gateway.

Namely, things fall apart when the following line is added:

import "google/api/annotations.proto";

The documentation says You will need to provide the required third party protobuf files to the protoc compiler - but not actually how do do so.

How do I add google/api/annotations.proto as a dependency?

Permanganate answered 12/2, 2021 at 8:14 Comment(0)
P
17

I solved it one way by adding third party google apis and its content to the root of my project.

Feels wrong, but apparently this is encouraged

Permanganate answered 12/2, 2021 at 9:42 Comment(1)
D
11

I had the same issue and i resolved it following this structure :

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

and run the command :

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
Dg answered 31/8, 2021 at 17:53 Comment(2)
Did you download those google's proto files from grpc repository manually ?Pulsimeter
Important if you don't have the "google/api" dir at the root of your project and you are managing it another way.Intrigant
B
3

I solved it with only copying annotations.proto and http.proto in the main proto:

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

and inside annotations.proto

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

and my folders look like this: enter image description here

Baltic answered 28/7, 2021 at 13:25 Comment(1)
doesn't work for meBacksight
U
2

If you are using protoc to generate stubs, you need to ensure the required dependencies are available to the compiler at compile time. These can be found by manually cloning and copying the relevant files from the googleapis repository, and providing them to protoc when running. The files you will need are:

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

from grpc-gateway

for example run in project root git submodule add https://github.com/googleapis/googleapis to get actual version

Unsnap answered 3/10, 2021 at 19:19 Comment(0)
F
1

I am using Visual Studio. My proto file hierarchy looks like this:

proto file hierarchy

I had to adjust the ProtoRoot property of the items in the csproj file like this:

<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>
Fitting answered 18/9, 2023 at 16:11 Comment(1)
"ProtoRoot" was the key part for me. Thanks for that!Imbecilic
P
1

For C# and Visual Studio this is a working method:

  1. Add nuget package "Microsoft.AspNetCore.Grpc.Swagger"
  2. Add this line to your .csproj file
    <PropertyGroup>
        ...
        <IncludeHttpRuleProtos>true</IncludeHttpRuleProtos>
    </PropertyGroup>

More information here

Pierrette answered 17/6 at 20:49 Comment(1)
that is the only working solution for me...Sanderling
P
0

Sometimes this error occurs when we run protoc . try to import the dependency when running the protoc command.

proto folder structure

for this type of structure use

>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
Paleolith answered 22/12, 2022 at 20:0 Comment(0)
C
0

What worked for me was:

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

with the same structure as explained here: https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/adding_annotations/#using-protoc

Cioffred answered 1/12, 2023 at 7:5 Comment(0)
T
0

You can manage your dependencies using buf. See the following snippet of their documentation:

You will need to provide the required third party protobuf files to the protobuf compiler. If you are using buf, this dependency can be added to the deps array in your buf.yaml under the name buf.build/googleapis/googleapis:

version: v1
name: buf.build/yourorg/myprotos
deps:
  - buf.build/googleapis/googleapis

Always run buf mod update after adding a dependency to your buf.yaml.

Trimming answered 12/12, 2023 at 18:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.