Cannot find package "google/protobuf" - go

I am compiling an open source project written in go (openblockchain). I get the following error when I do go build. Can anyone help me with this issue
Compilation Error
> go build
../go/src/github.com/openblockchain/obc-peer/openchain/util/utils.go:28:2: cannot find package "google/protobuf" in any of:
/usr/src/pkg/google/protobuf (from $GOROOT)
/home/vichu/go/src/google/protobuf (from $GOPATH)
Additional Information
I referred the question here in Stack Overflow but still no luck in solving the issue. Here is some more information about what all I have:
Protoc version is up to date.
> protoc --version
libprotoc 3.0.0
My environment variables
> echo $GOPATH
/home/vichu/go
> echo $GOBIN
/home/vichu/go/bin
Protobuf has been built using the README.
~/go/src/github.com/golang/protobuf$ ls
AUTHORS CONTRIBUTORS jsonpb LICENSE Makefile Make.protobuf proto protoc-gen-go proto.pb.go ptypes README.md
Update
I did the following Util.go in source code as mentioned in answer.
The source code is open source and here is the link
- gp "google/protobuf"
+ gp "github.com/google/protobuf"
When I do go get, the below is the error
> go get github.com/google/protobuf
can't load package: package github.com/google/protobuf: no buildable Go source files in /home/vichu/go/src/github.com/google/protobuf

Firstly, your import is wrong, you are trying to import a C++ package, not a golang package. It needs to be:
import ("github.com/golang/protobuf/proto")
If you don't have this package installed already, you need to run from command line:
go get github.com/golang/protobuf/proto

I think, at the end of your *.pb.go file you also do not have some thing like "gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00 ...."
The issue is we use a wrong compiler for generating.
So:
1) I reinstall protobuf from:
https://github.com/google/protobuf/releases
2) Then (I'm using ubuntu and it has a proto compiler also):
apt remove protobuf-compiler
Rebuild the *.proto file. It fixed the bug.

Related

i am unable to install subzy tool in my linux it shows error

I tried to install this subzy tool from GitHub but it shows this kind of error and I can't install it I have faced the same problem with other programs written in (GO language).
I can clone the programs written in python but I am unable to clone the programs written in go language once I clone I don't know how to install them
****➜ ~ go install -v github.com/lukasikic/subzy#latest
go: finding a module f**or package github.com/mitchellh/go-homedir
go: finding module for package github.com/logrusorgru/aurora
go: found github.com/logrusorgru/aurora in github.com/logrusorgru/aurora v2.0.3+incompatible
go: found github.com/mitchellh/go-homedir in github.com/mitchellh/go-homedir v1.1.0
➜ ~ go get -u -v github.com/lukasikic/subzy
go get: installing executables with 'go get' in module mode is deprecated.
Use 'go install pkg#version' instead.
For more information, see https://golang.org/doc/go-get-install-deprecation
or run 'go help get' or 'go help install'.
➜ ~**
Welcome to the community,
Check the closed repo, whether it has go.mod and go.sum files
Check the version of the go (go mod is enforced in recent version meaning)
Check and learn about go mod and how to use (https://go.dev/blog/using-go-modules)

What does it mean the code in my directory "expects" a particular import?

A Go language tutorial uses the "quote" package to get familiar quotes. When I give the command "go get -v rsc.io/quote", I get the message
code in directory.../github/hello/src/rsc.io/quote/v3 expects import "rsc.io/quote"
So I went there and looked at the code. It has an import statement:
import "rsc.io/quote/v3"
So I thought maybe I had the wrong version of quote. Accordingly, I tried "go get -v rsc.io/quite/v3" THis produces the message:
code in directory .../rsc.io/quote/v3 expects import "rsc.io/quote."
This seems circular: asking without a version number produces a requirement for a versioned package, but trying to get a versioned package produces a requirement without a version number.
If you continue the tutorial to the end you will see that the addition of a go module, and the subsequent initialisation, will resolve your issue. The .mod file will pull in the required dependencies.
Can you try
go get -v rsc.io/quote
get "rsc.io/quote": found meta tag get.metaImport{Prefix:"rsc.io/quote", VCS:"git", RepoRoot:"https://github.com/rsc/quote"} at //rsc.io/quote?go-get=1
rsc.io/quote (download)
get "rsc.io/sampler": found meta tag get.metaImport{Prefix:"rsc.io/sampler", VCS:"git", RepoRoot:"https://github.com/rsc/sampler"} at //rsc.io/sampler?go-get=1
rsc.io/sampler (download)
github/gospace/src/rsc.io/quote/quote.go:8:8: code in directory /Users/james/github/gospace/src/rsc.io/quote/v3 expects import "rsc.io/quote"
cat $GOPATH/src/rsc.io/quote/v3/go.mod
module rsc.io/quote/v3
require rsc.io/sampler v1.3.0
~> cat $GOPATH/src/rsc.io/quote/v3/go.sum
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
rsc.io/sampler v1.99.99 h1:7i08f/p5TBU5joCPW3GjWG1ZFCmr28ybGqlXtelhEK8=
rsc.io/sampler v1.99.99/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
More about go get vs go install. What is the difference between go get and go install?
Can you please check your GO env path settings:
export GOPATH=$HOME/github/<your_workspace_name>
export GOBIN=$GOPATH/bin
export GOROOT=/usr/local/go
export PATH=$PATH:/usr/local/bin:/usr/local/go/bin:$GOPATH/bin
Here is a good source for setting up workspace:
https://golang.org/doc/gopath_code.html
As #MartinBennett mentioned, it's already mentioned in the tutorial but may be not so clear.
Once you add import "rsc.io/quote" in your hello.go file, you will need to run these commands in your terminal:
1-
go mod init hello
2-
go run hello.go

Build dependencis for aws-sdk-go fails on Go version 1.13

I have been trying to build a module on Go v1.13 with dependencies on github.com/aws/aws-lambda-go and github.com/aws/aws-sdk-go which fail on the two imports:
"github.com/aws/aws-sdk-go/aws/service/s3"
"github.com/aws/aws-sdk-go/aws/service/s3/s3manager"
The stderr is as follows for GOOS=linux GOARCH=amd64 go build -o dist/api ./api where the api directory contains my module definition:
api/main.go:11:2: cannot find package "github.com/aws/aws-sdk-go/aws/service/s3" in any of:
/usr/local/go/src/github.com/aws/aws-sdk-go/aws/service/s3 (from $GOROOT)
/u/go/src/github.com/aws/aws-sdk-go/aws/service/s3 (from $GOPATH)
api/main.go:12:2: cannot find package "github.com/aws/aws-sdk-go/aws/service/s3/s3manager" in any of:
/usr/local/go/src/github.com/aws/aws-sdk-go/aws/service/s3/s3manager (from $GOROOT)
/u/go/src/github.com/aws/aws-sdk-go/aws/service/s3/s3manager (from $GOPATH)
Honestly, I have no clue why this is happening and any inputs would be appreciated.
Already tried using go get to ensure that the dependencies have been pulled:
$ go get github.com/aws/aws-sdk-go
and the requested import paths are present under $GOPATH/src/github.com/aws/aws-sdk/go/aws/service/s3 and $GOPATH/src/github.com/aws/aws-sdk/go/aws/service/s3/s3manager
Also, tried clearing the cache using go clean --cache --modcache whilst removing previously pulled modules.
On closer inspection, something that I completely overlooked, the import path is /u/go/src/github.com/aws/aws-sdk-go/aws/service/s3 instead of /u/go/src/github.com/aws/aws-sdk-go/service/s3 with the former having an additional aws subpath inside aws-sdk-go.
Just realized the copy/paste error I had made in the code.
import (
"github.com/aws/aws-sdk-go/aws/service/s3"
"github.com/aws/aws-sdk-go/aws/service/s3/s3manager"
)
instead of
import (
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)

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
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
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
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
Update your environment’s path variable to include the path to the protoc executable. For example:
export PATH="$PATH:$HOME/.local/bin"
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.
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.
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
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
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.
Go to the VSCode proto 3 extension settings and paste "protocol" at the end of the file:
"protoc": {
"options": [
"--proto_path=proto",
]
}
}
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) :
find the appropriate version of compiler herer https://github.com/protocolbuffers/protobuf/releases
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 !
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.

protoc with grpc_python_plugin: google/protobuf/any.proto: File not found

I'm running this command:
protoc protobuf/file.proto --proto_path=protobuf --proto_path=protobuf --python_out=out/builtin_pb --grpc_out=out/builtin_pb --plugin=protoc-gen-grpc=/usr/local/bin/grpc_python_plugin
where file.proto has:
syntax = "proto3";
import "google/protobuf/any.proto";
And stuff like:
google.protobuf.Any arg = 3;
However, it has the error:
google/protobuf/any.proto: File not found.
bess_msg.proto: Import "google/protobuf/any.proto" was not found or had errors.
bess_msg.proto:251:3: "google.protobuf.Any" is not defined
...
Are there any steps I can follow to make sure any.proto is properly installed?
Thanks
I had this error when trying to compile a protobuf to python on Ubuntu. My problem was that I had installed the compiler using sudo apt install protobuf-compiler, but I had not installed the resources using sudo apt install libprotobuf-dev.
Thanks to this answer for showing me the error of my ways.
I recently encountered this issue and realized that I was using protobuf 3.0. Simply upgrade the protobuf compiler to the latest version (3.6 in my case) for definitions on those types.

Resources