部署Cloud Endpoints遇Unresolved type '.google.protobuf.Timestamp'错误求助
Let's break down why you're hitting this error and walk through how to fix it:
The Root Cause
When generating api_descriptor.pb, Cloud Endpoints needs all proto definitions your service depends on—including the standard google.protobuf.Timestamp type. Your current protoc command only packages your own service.proto and doesn't include the Timestamp definition. Plus, your proto file's vendor-specific import path is causing protoc to struggle resolving the type correctly.
Step 1: Correct the Proto File Import
First, update your proto file to use the standard, protoc-friendly import path for Timestamp:
syntax = "proto3"; package proto; // Replace the vendor-specific import with this standard one import "google/protobuf/timestamp.proto"; message CandleStick { string ID = 1; double Open = 2; double Close = 3; double High = 4; double Low = 5; google.protobuf.Timestamp TimeStamp = 6; }
Step 2: Update the Protoc Command to Include Dependencies
You need two key additions to your protoc command:
- A valid include path so protoc can locate
google/protobuf/timestamp.proto - The
--include_importsflag to bundle all dependent proto definitions intoapi_descriptor.pb
Option A: System-wide Protobuf Installation
If you have protobuf installed on your system (e.g., via apt install protobuf-compiler on Ubuntu), use this command:
protoc \ --plugin=protoc-gen-go=${GOBIN}/protoc-gen-go \ -I . \ -I /usr/include \ # Path to standard protobuf system includes proto/service.proto \ --descriptor_set_out=api_descriptor.pb \ --include_imports \ # Bundles all imported proto definitions into the descriptor set --go_out=plugins=grpc:. \
Option B: Go Module-managed Protobuf
If you're using Go modules and don't have a system-wide protobuf install, use this command to dynamically resolve the correct include path:
protoc \ --plugin=protoc-gen-go=${GOBIN}/protoc-gen-go \ -I . \ -I $(go list -f '{{.Dir}}' github.com/golang/protobuf/ptypes/timestamp)/.. \ # Points to the parent directory of the timestamp proto proto/service.proto \ --descriptor_set_out=api_descriptor.pb \ --include_imports \ --go_out=plugins=grpc:. \
Step 3: Validate the Fix
Regenerate api_descriptor.pb with the updated command, then re-run your Cloud Endpoints deploy validation:
gcloud endpoints services deploy api_descriptor.pb api_config.yaml --validate-only
This should resolve the "Unresolved type" error, as Cloud Endpoints will now have access to the full google.protobuf.Timestamp definition in the bundled descriptor set.
内容的提问来源于stack exchange,提问作者user5353256

