1

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/[email protected]

$ go install google.golang.org/grpc/cmd/[email protected]

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/[email protected]
github.com/golang/[email protected] github.com/google/[email protected]
github.com/golang/[email protected] google.golang.org/[email protected]
google.golang.org/[email protected] github.com/golang/[email protected]
google.golang.org/[email protected] github.com/golang/[email protected]
github.com/golang/[email protected] github.com/google/[email protected]
github.com/golang/[email protected] google.golang.org/[email protected]


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

1 Answer 1

0

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 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.