Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 4

The 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.

Fix
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.proto
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to protoc 36.2, buf 1.75, grpc-gateway v2.34
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
find . -name annotations.proto -path '*google/api/*'

Then find the line you see in this list and open its fix:

  1. 1The googleapis files were never added to a protoc include pathfind returns nothing, and the protoc command has only -I . or -I proto
  2. 2The include path points inside google/api instead of above googlefind shows the file, but the -I value ends in google/api or google, for example -I third_party/google/api
  3. 3Only annotations.proto was copied, so its own import failsthird_party/google/api holds annotations.proto and nothing else, and the protoc output also mentions google/api/http.proto
  4. 4With buf, the googleapis module is missing from deps or was never lockedYou 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.

Check
ls third_party/google/api 2>/dev/null || echo missing

Fix

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.

Fix
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.proto

Verify

protoc exits with status 0 and a .pb.gw.go file appears next to the other generated code.

Verify
ls gen/go/your/service/v1/

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Check
ls third_party/google/api/annotations.proto

Fix

Pass the directory that contains the google folder, not the folder that contains the .proto file.

Fix
# 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.

Verify
protoc -I . -I third_party --grpc-gateway_out ./gen/go --grpc-gateway_opt paths=source_relative your/service/v1/your_service.proto && echo ok

Confirmed by protobuf.dev

Did this fix it?

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.

Check
grep -n '^import' third_party/google/api/annotations.proto

Fix

Add http.proto next to annotations.proto, plus field_behavior.proto and httpbody.proto if your files use them, as the gateway README lists.

Fix
curl -sSfo third_party/google/api/http.proto \
  https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto

Verify

Run the same protoc command; no line mentions google/api any more.

Verify
protoc -I . -I third_party --grpc-gateway_out ./gen/go --grpc-gateway_opt paths=source_relative your/service/v1/your_service.proto && echo ok

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Check
grep -n -A3 '^deps' buf.yaml; ls buf.lock

Fix

Declare the module in buf.yaml, then update the lock and generate again.

Fix
# buf.yaml
version: v2
deps:
  - buf.build/googleapis/googleapis

buf dep update
buf generate

Verify

buf.lock now lists buf.build/googleapis/googleapis and buf generate finishes without import errors.

Verify
grep googleapis buf.lock

Confirmed by buf.build

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.