The proto file is importing "github.com/golang/protobuf/proto" instead of "google.golang.org/protobuf/proto" when "protoc --proto_path=proto proto/*.proto --go_out=plugins=grpc:pb" command
The import file
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
...
> This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
My proto file
syntax="proto3";
message Processor{
string name=1;
uint32 cores=2;
uint32 min_ghz=3;
uint32 max_ghz=4;
}
~go/bin/protoc-gen-go-grpc has versions
go: downloading google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0
go: downloading google.golang.org/grpc v1.58.2
go: downloading google.golang.org/protobuf v1.28.1
What I did
Initially installed
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
typed go clean -modcache before installing a new package and reinstalled the latest version with annotation @latest
go version: go version go1.21.1 linux/amd64 on Ubuntu 20.4
protoc --version libprotoc 3.6.1
installed protobuf-compiler and golang-goprotobuf using apt
sudo apt install protobuf-compiler
sudo apt install golang-goprotobuf -dev
export PATH="$PATH:$(go env GOPATH)/bin"
I feel like here lies the problem but I'm not sure what to fix or how to read this
go mod graph | grep github.com/golang/protobuf
example-first github.com/golang/protobuf@v1.5.3
github.com/golang/protobuf@v1.5.3 github.com/google/go-cmp@v0.5.5
github.com/golang/protobuf@v1.5.3 google.golang.org/protobuf@v1.26.0
google.golang.org/protobuf@v1.31.0 github.com/golang/protobuf@v1.5.0
google.golang.org/protobuf@v1.26.0 github.com/golang/protobuf@v1.5.0
github.com/golang/protobuf@v1.5.0 github.com/google/go-cmp@v0.5.5
github.com/golang/protobuf@v1.5.0 google.golang.org/protobuf@v1.26.0-rc.1
go mod why github.com/golang/protobuf
go: downloading github.com/golang/protobuf v1.5.3
go: downloading github.com/google/go-cmp v0.5.5
go: downloading golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
# github.com/golang/protobuf
(main module does not need package github.com/golang/protobuf)
Edit: I think I initially installed it using go get -u github.com/golang/protobuf/proto but I deleted the binary using rm -rf $(go env GOPATH)/pkg/mod/github.com/golang/protobuf/proto and installed the new one using go install google.golang.org/protobuf/cmd/protoc-gen-go@latest and go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest. It still generates go file with old import
Edit2: protoc-gen-go --version cannot be found but protoc-gen-go-grpc --version is 1.2.0. protoc --version is libprotoc 3.6.1 whereis protoc-gen-go protoc-gen-go: /usr/bin/protoc-gen-go /home/hp/go/bin/protoc-gen-go /usr/share/man/man1/protoc-gen-go.1.gz
As mentioned in https://github.com/golang/protobuf/issues/1451 by @puellanivis
The $PATH
variable in your linux env should start with /home/{username}/go/bin
and then /usr/bin
afterwards in that order. This is because we need google.golang.org/gprc/cmd/protoc-gen-go-grpc@latest
to be found before /usr/bin/protoc-gen-go
.
Edit the ~/.bashrc
or ~/.bash_profile
files ($vim ~/.bashrc
) and simply export the whole path enviroment manually. In my case I had to add
export PATH=/home/hp/go/bin:/usr/local/go:/home/hp/go:usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin