14

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
cbll
  • 6,499
  • 26
  • 74
  • 117

5 Answers5

13

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

Max R.
  • 811
  • 1
  • 13
  • 31
cbll
  • 6,499
  • 26
  • 74
  • 117
  • 4
    https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/adding_annotations/#using-protoc – kangkyu Mar 17 '21 at 23:35
6

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
Slimane amiar
  • 934
  • 12
  • 27
2

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

AJ AJ
  • 187
  • 2
  • 12
1

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

Igor
  • 1,589
  • 15
  • 15
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
subhajit das
  • 383
  • 3
  • 8