18

Relatively new to GRPC and getting an error in my proto file that I cannot seem to make sense of. I would like to send a time in a message using the "google.protobuf.Timestamp". I cannot seem to import it. What am I doing wrong?

syntax = "proto3";
    
import "google/protobuf/timestamp.proto";
        
service ProfileService {
    rpc ConstructProfileStructFromUser (ConstructProfileStructFromUserRequest) returns (ConstructProfileStructFromUserResponse);
}
        
message ConstructProfileStructFromUserRequest {
    string transactionID = 1;
    string User = 2;
}
        
message ConstructProfileStructFromUserResponse {
    string UID = 1;
    string ContactEmail = 2;
    google.protobuf.Timestamp DateOfBirth = 3;
}

Both in my IDE and my compiler (using the below command) then I get the error

google/protobuf/timestamp.proto: File not found.
profile.proto: Import "google/protobuf/timestamp.proto" was not found or had errors.
profile.proto:21:5: "google.protobuf.Timestamp" is not defined.

Command to run:

protoc -I profile/ profile/profile.proto --go_out=plugins=grpc:profile

Protoc --version

libprotoc 3.0.0
Clément Jean
  • 1,735
  • 1
  • 14
  • 32
mornindew
  • 1,993
  • 6
  • 32
  • 54

9 Answers9

10

I had this issue after installing the protoc compiler using the apt package manager (Ubuntu) and it put the protoc compiler somewhere like /usr/local/bin.

It seems by default protoc expects imports to be present in an include path relative to the protoc installation directory. For example:

  • protoc location: /usr/local/bin/protoc
  • include location: /usr/local/bin/include/*

Install pre-compiled binaries (any OS)

Downloading a pre-compiled binary as indicated below will have the needed include directory.

Instructions from grpc.io/docs/protoc-installation

  1. Manually download from github.com/google/protobuf/releases the zip file corresponding to your operating system and computer architecture (protoc--.zip), or fetch the file using commands such as the following:
PB_REL="https://github.com/protocolbuffers/protobuf/releases"
curl -LO $PB_REL/download/v3.15.8/protoc-3.15.8-linux-x86_64.zip
  1. Unzip the file under $HOME/.local or a directory of your choice. For example:
unzip protoc-3.15.8-linux-x86_64.zip -d $HOME/.local
  1. Update your environment’s path variable to include the path to the protoc executable. For example:
export PATH="$PATH:$HOME/.local/bin"
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
SwiftD
  • 5,769
  • 6
  • 43
  • 67
  • 1
    What exactly did you do @SwiftD after downloading it – Dapo Michaels Feb 18 '20 at 11:25
  • 3
    @dapo first you need to uninstall any existing protoc then once downloaded you want the binary from the download to be somewhere in your path e.g `/usr/local/bin/protoc` (assuming `/usr/local/bin` is in your PATH var) then you just need to place the includes folder from the download in the same folder next to it e.g `/usr/local/bin/includes`. should work now from any location, call version flag to confirm – SwiftD Apr 24 '20 at 12:29
  • 2
    I installed the protoc compiler to /usr/local/bin, but didn't copy the required imports. This issue got solved after I copied the additional imports to "/usr/local/bin/include'. – LeoRado Aug 27 '20 at 14:10
  • For my own reference, steps are: 1) Run `protoc --version` to get version, 2) go to github and download the relevant compiled release for os (mac in my case) 3) extract to the `/usr/local/bin/include` folder 4) add it to path – Hansang Nov 21 '22 at 11:42
9

If you installed protoc with your package manager, you only have to install the libprotobuf-dev (Ubuntu) or protobuf-devel (Fedora) package.

In general, you can find the containing package of a file on Ubuntu with apt-file find google/protobuf/timestamp.proto or on Fedora dnf repoquery --file "**/google/protobuf/timestamp.proto" (this is how I found the package I needed). Other package managers probably have similar commands.

timgo
  • 345
  • 3
  • 6
2

I had same problem with protoc 3.0.0 installed from ubuntu repo. I have found another solution, without reinstalling protobuf as @SwiftD suggested, using --proto_path protoc option. In your .proto import should look like (i.e. without path):

syntax = "proto3";

import "timestamp.proto"

Then in the protoc invocation you pass absolute path to your package directory containing timestamp.proto (I use github.com/golang/protobuf/ptypes/timestamp) using --proto_path option.

protoc kcproto.proto --go_out=./  --proto_path=/home/my_home_dir_name/go/src/github.com/golang/protobuf/ptypes/timestamp --proto_path=./

replace /home/my_home_dir_name/ with your go package directory

2

If you download from https://github.com/protocolbuffers/protobuf/releases, in the download directory there is an include directory, where all the google defined types (such as google/protobuf/timestamp.proto) live.

You can pass in an extra parameter --proto_path=/path/to/include to your protoc command, and it should work.

yiksanchan
  • 1,890
  • 1
  • 13
  • 37
1

My problem was quite simple...

I didn't have the timestamp.proto downloaded locally and as a result it couldn't find it.

I cloned:

https://github.com/protocolbuffers/protobuf/tree/master/src/google/protobuf

And then when I run my compiler I have to give it the location to locate the timestamp.proto files.

For me it was...

protoc -I profile/ -I MY_CLONED_REPO_LOCATION/protobuf/src profile/profile.proto --go_out=plugins=grpc:profile

Once it knew where it had the path to the source then it could find it with no issues.

mornindew
  • 1,993
  • 6
  • 32
  • 54
1

For mac, I run this in the terminal

PROTOC_ZIP=protoc-3.14.0-osx-x86_64.zip
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.14.0/$PROTOC_ZIP
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
rm -f $PROTOC_ZIP

Note that you can change the version protoc-3.14.0 to whatever your need, for example protoc-3.x.x

Docs: http://google.github.io/proto-lens/installing-protoc.html

0

Go to the VSCode proto 3 extension settings and paste "protocol" at the end of the file:

"protoc": {
        
        "options": [
            "--proto_path=proto",
        ]
    }
}
0

I had the same problem with

import "google/protobuf/timestamp.proto";

I'm on a fedora and installed protocol buffer compiler using the package manager dnf . solution : delete your current protoc using then package manger . on fedora:

sudo dnf remove protobuf-compiler 

then follow the instruction to Install pre-compiled binaries (any OS) :

  1. find the appropriate version of compiler herer https://github.com/protocolbuffers/protobuf/releases
  2. unzip it in somewhere in your PATH . for example you can do something like this : $ unzip protoc-3.15.8-linux-x86_64.zip -d $HOME/.local

DONE !

Mahdi zarepoor
  • 310
  • 1
  • 6
-1

What I ended up doing in same situation was to include

message google {
    message protobuf {
        message Timestamp {
          int64 seconds = 1;
          int32 nanos = 2;
        }
    }
}

In my proto-file. That was enough for it to be recognized as well known type, so in python I get the addition API described at https://developers.google.com/protocol-buffers/docs/reference/python-generated#timestamp.

The main advantage is that we all can keep using the system install of protoc and don't have to install from source.

Zbyněk Winkler
  • 1,263
  • 1
  • 12
  • 12
  • why is this voted down? – heisenberg Jun 23 '23 at 08:45
  • I'd like to know as well. It definitely works and I appreciate that the solution is contained within the proto file - i.e. hand compiling "just works", no need to install anything. The code is literally what's inside the proto file distributed by the project. – Zbyněk Winkler Jun 29 '23 at 17:20
  • this works too for my case. Not in Kotlin though, I write relevant script in Javascript protobuf definition and defining timestamp like this works for me. Upvoting this. – heisenberg Jul 04 '23 at 06:33