mornindew
mornindew

Reputation: 2221

Protobuf timestamp not found

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

Upvotes: 25

Views: 49507

Answers (11)

Neeraj Sharma
Neeraj Sharma

Reputation: 656

For linux user

sudo apt-get update && apt-get upgrade  
sudo apt-get install protobuf-compiler libprotobuf-dev

protobuf-compiler includes protoc compiler and libprotobuf-dev includes the well-known types.

Upvotes: 0

Clare Chu
Clare Chu

Reputation: 778

Because you installed protoc incorrectly, you need to download protoc on github.

$ wget https://github.com/protocolbuffers/protobuf/releases/download/v25.3/protoc-25.3-linux-x86_64.zip

$ unzip -r protoc-25.3-linux-x86_64.zip

1.  Switch to the protoc directory
$ cd protoc-25.3-linux-x86_64

2. copy bin to PATH

$ sudo mv bin/protoc /usr/local/bin

3.  copy include files to /usr/local
$ sudo mv include /usr/local

Just see that the /usr/local/include directory contains google directories

$ ls /usr/local/include
google

Upvotes: 1

Mahdi zarepoor
Mahdi zarepoor

Reputation: 410

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 !

Upvotes: 0

6jodeci
6jodeci

Reputation: 1

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

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

Upvotes: 0

timgo
timgo

Reputation: 455

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.

Upvotes: 16

SwiftD
SwiftD

Reputation: 6069

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"

Upvotes: 17

yiksanchan
yiksanchan

Reputation: 1940

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.

Upvotes: 2

Zbyněk Winkler
Zbyněk Winkler

Reputation: 1465

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.

Upvotes: 0

Trung Nguyen
Trung Nguyen

Reputation: 11

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

Upvotes: 1

Testobile Testossimo
Testobile Testossimo

Reputation: 197

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

Upvotes: 2

mornindew
mornindew

Reputation: 2221

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.

Upvotes: 3

Related Questions