'Import "google/api/annotations.proto" was not found or had errors. How do I add it as a dependency?
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?
Solution 1:[1]
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
Solution 2:[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";
Solution 3:[3]
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
Solution 4:[4]
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
Solution 5:[5]
protoc releases include these well-known types in a directory (either libor include) alongside bin (on Linux).
You should not need to clone|copy them anywhere (else) and IIRC protoc doesn't even need a separate proto_path flag to the directory in order to generate code for protos that import them
Google also provides packages for the well-known types as part of the Golang Protobuf SDK:
https://pkg.go.dev/google.golang.org/protobuf/types/known
Suffice to say, on Linux, using protoc releases as-is, you should be able to add imports for these types without changing your protoc command and go mod tidy && go build ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Max R. |
| Solution 2 | AJ AJ |
| Solution 3 | Slimane amiar |
| Solution 4 | |
| Solution 5 |

