Search code examples
protocol-buffers

How does protoc find definitions not on IMPORT_PATH?


In one of my .proto definitions, I import Google's timestamp definition:
import "google/protobuf/timestamp.proto";

From the documentation,

IMPORT_PATH specifies a directory in which to look for .proto files when resolving import directives.

I am pretty sure timestamp.proto is not on my IMPORT_PATH, which happens to be the current working directory.

Are google protobuf definitions special somehow? Are these google definitions installed locally, when protoc was installed?


Solution

  • They are indeed special - they are referred to as well-known types.

    The compiler looks to resolve these imports relative to where the protoc executable is installed; various locations are checked. If a location is found that contains proto definitions for the well-known types, this is location is looked at as a fall-back - in other words, a path relative to where the protoc executable is run is checked first, before checking the location with the well-known type definitions.

    You can see this when executing the compiler with strace or dtruss, e.g. on OSX:

    stat64("google/protobuf/timestamp.proto\0", 0x7FFEE4219B98, 0x0)                 = -1 2
    open("google/protobuf/timestamp.proto\0", 0x0, 0xFFFFFFFFFFFFFFFF)               = -1 2
    stat64("/usr/local/Cellar/protobuf/21.8/include/google/protobuf/timestamp.proto\0", 0x7FFEE4219B98, 0x0)                 = 0 0
    open("/usr/local/Cellar/protobuf/21.8/include/google/protobuf/timestamp.proto\0", 0x0, 0x0)              = 3 0