protoc cannot find google/api/annotations.proto for gRPC-Gateway
Your error
Import "google/api/annotations.proto" was not found or had errors.Fix it fast
Most common cause, 1 of 4The googleapis files were never added to a protoc include path
Download the four files into a third_party folder that mirrors the google/api path, then add that folder as an extra include directory.
mkdir -p third_party/google/api
for f in annotations http field_behavior httpbody; do
curl -sSfo third_party/google/api/$f.proto \
https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/$f.proto
done
protoc -I . -I third_party --grpc-gateway_out ./gen/go \
--grpc-gateway_opt paths=source_relative \
your/service/v1/your_service.protoShort answer
Import "google/api/annotations.proto" was not found or had errors means the compiler was never given Google's googleapis files, which you must supply yourself. Download annotations.proto and http.proto into third_party/google/api and pass -I third_party, or with buf add buf.build/googleapis/googleapis to deps and run buf dep update. Applies to protoc 36, buf 1.75, grpc-gateway v2.34.
Which cause do you have?
Check whether any directory you pass with -I holds the file at the exact relative path the import names. No output at all means the files were never added; a hit at the wrong depth means the include path is off by a level.
find . -name annotations.proto -path '*google/api/*'Then find the line you see in this list and open its fix:
- 1The googleapis files were never added to a protoc include path
find returns nothing, and the protoc command has only -I . or -I proto - 2The include path points inside google/api instead of above google
find shows the file, but the -I value ends in google/api or google, for example -I third_party/google/api - 3Only annotations.proto was copied, so its own import fails
third_party/google/api holds annotations.proto and nothing else, and the protoc output also mentions google/api/http.proto - 4With buf, the googleapis module is missing from deps or was never locked
You generate with buf generate, and buf.yaml has no deps entry for googleapis, or buf.lock is absent
What does the error look like?
Import "google/api/annotations.proto" was not found or had errors.
Cause 1 of 4
The googleapis files were never added to a protoc include path
You seefind returns nothing, and the protoc command has only -I . or -I proto
Check
protoc reports Import google/api/annotations.proto was not found because that file belongs to Google's googleapis repository, not to the protobuf compiler. The gRPC-Gateway README says protoc users must fetch the files themselves and lists four: annotations.proto, field_behavior.proto, http.proto and httpbody.proto.
ls third_party/google/api 2>/dev/null || echo missingFix
Download the four files into a third_party folder that mirrors the google/api path, then add that folder as an extra include directory. Commit them, or fetch them in the build script.
mkdir -p third_party/google/api
for f in annotations http field_behavior httpbody; do
curl -sSfo third_party/google/api/$f.proto \
https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/$f.proto
done
protoc -I . -I third_party --grpc-gateway_out ./gen/go \
--grpc-gateway_opt paths=source_relative \
your/service/v1/your_service.protoVerify
protoc exits with status 0 and a .pb.gw.go file appears next to the other generated code.
ls gen/go/your/service/v1/Confirmed by raw.githubusercontent.com
Cause 2 of 4
The include path points inside google/api instead of above google
You seefind shows the file, but the -I value ends in google/api or google, for example -I third_party/google/api
Check
protoc resolves an import string relative to every -I directory. The string google/api/annotations.proto therefore has to exist as <include dir>/google/api/annotations.proto. Pointing -I at the folder that directly holds the file makes protoc look for third_party/google/api/google/api/annotations.proto, which does not exist.
ls third_party/google/api/annotations.protoFix
Pass the directory that contains the google folder, not the folder that contains the .proto file.
# wrong
protoc -I . -I third_party/google/api ...
# right
protoc -I . -I third_party ...Verify
The import error is gone; any remaining message refers to your own files.
protoc -I . -I third_party --grpc-gateway_out ./gen/go --grpc-gateway_opt paths=source_relative your/service/v1/your_service.proto && echo okConfirmed by protobuf.dev
Cause 3 of 4
Only annotations.proto was copied, so its own import fails
You seethird_party/google/api holds annotations.proto and nothing else, and the protoc output also mentions google/api/http.proto
Check
annotations.proto is not self-contained. Its source imports google/api/http.proto, and also google/protobuf/descriptor.proto. Copy annotations.proto alone and the compiler cannot load http.proto, so the annotations import counts as one that had errors.
grep -n '^import' third_party/google/api/annotations.protoFix
Add http.proto next to annotations.proto, plus field_behavior.proto and httpbody.proto if your files use them, as the gateway README lists.
curl -sSfo third_party/google/api/http.proto \
https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.protoVerify
Run the same protoc command; no line mentions google/api any more.
protoc -I . -I third_party --grpc-gateway_out ./gen/go --grpc-gateway_opt paths=source_relative your/service/v1/your_service.proto && echo okConfirmed by raw.githubusercontent.com
Cause 4 of 4
With buf, the googleapis module is missing from deps or was never locked
You seeYou generate with buf generate, and buf.yaml has no deps entry for googleapis, or buf.lock is absent
Check
buf finds imports from outside your module through the Buf Schema Registry modules that buf.yaml declares under deps, and it records the resolved versions in buf.lock. The gateway README adds that buf dep update has to run after every new dependency, so editing buf.yaml alone is not enough.
grep -n -A3 '^deps' buf.yaml; ls buf.lockFix
Declare the module in buf.yaml, then update the lock and generate again.
# buf.yaml
version: v2
deps:
- buf.build/googleapis/googleapis
buf dep update
buf generateVerify
buf.lock now lists buf.build/googleapis/googleapis and buf generate finishes without import errors.
grep googleapis buf.lockConfirmed by buf.build
How do I stop it coming back?
- Keep third-party protos in one folder such as third_party, passed with one -I flag in a single Makefile or script that everyone runs.
- Commit buf.lock, and run buf dep update in a scheduled job rather than on every build.
- Add a CI job that regenerates the stubs from a clean checkout and fails when protoc or buf exits non-zero.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.