Protobuf timestamp not found
Asked Answered
H

10

22

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
Homer answered 7/5, 2019 at 22:12 Comment(0)
D
14

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"
Dickie answered 28/11, 2019 at 12:26 Comment(4)
What exactly did you do @Dickie after downloading itMontanez
@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 confirmDickie
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'.Dayak
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 pathDoorman
E
13

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.

Elevate answered 19/9, 2022 at 15:45 Comment(2)
That's exactly the reason in my case! I was trying to install protobuf-compiler instead. Thank youTabret
Thank you for specifying the dnf packageLepidopteran
H
3

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.

Homer answered 8/5, 2019 at 4:57 Comment(0)
P
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

Punishable answered 1/4, 2020 at 9:57 Comment(0)
M
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.

Maud answered 22/11, 2021 at 9:44 Comment(0)
S
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

Sharronsharyl answered 30/11, 2020 at 13:48 Comment(0)
S
1

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
Saintpierre answered 11/3 at 13:58 Comment(0)
J
0

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

"protoc": {
        
        "options": [
            "--proto_path=proto",
        ]
    }
}
Jordanson answered 29/9, 2022 at 16:27 Comment(0)
A
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 !

Aframe answered 10/1, 2023 at 9:27 Comment(0)
D
-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.

Dextrogyrate answered 18/3, 2021 at 10:59 Comment(3)
why is this voted down?Ignatius
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.Clubfoot
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.Ignatius

© 2022 - 2024 — McMap. All rights reserved.