Reputation: 2221
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
Reputation: 656
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
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
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) :
$ unzip protoc-3.15.8-linux-x86_64.zip -d $HOME/.local
DONE !
Upvotes: 0
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
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
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/*
Downloading a pre-compiled binary as indicated below will have the needed include
directory.
Instructions from grpc.io/docs/protoc-installation
PB_REL="https://github.com/protocolbuffers/protobuf/releases"
curl -LO $PB_REL/download/v3.15.8/protoc-3.15.8-linux-x86_64.zip
$HOME/.local
or a directory of your choice. For example:unzip protoc-3.15.8-linux-x86_64.zip -d $HOME/.local
export PATH="$PATH:$HOME/.local/bin"
Upvotes: 17
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
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
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
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
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