Protobuf: Need steps to download and install an older version of protoc-gen-go

Created on 17 Apr 2020  路  9Comments  路  Source: golang/protobuf

Need steps to download and install an older version of protoc-gen-go.

The current versions on my system for protoc-gen-go and protoc are as follows:
+// protoc-gen-go v1.21.0-devel
+// protoc v3.11.2

With the latest version of protoc-gen-go after I convert the *.proto file to a *.pb.go file (which has ProtoPackageIsVersion4 set) and then compile the code using go build, I have started seeing an error regarding ProtoPackageIsVersion4 (in all our other files we have it set to ProtoPackageIsVersion3). Detailed error is shown at the end . On my colleague's system, with same protoc version (v3.11.2) and an older version of protoc-gen-go (probably v1.20.1 as there is no way to check the version for old protoc-gen-go), after the conversion from, *.proto file to a *.pb.go file (which has ProtoPackageIsVersion3 set) and then doing go build works fine.

Detailed Error:
SBARAI-M-J2V1:sgw-service sbarai 18:52:04$ go build
.pb.go:26:11: undefined: "/vendor/github.com/golang/protobuf/proto".ProtoPackageIsVersion4

Hence I believe an older version of protoc-gen-go may help here. Please let me know how to get it. I tried downloading from the releases->v1.20.1->assets->linux386.gz for my MAC system. After unzipping the same, it generates an executable, but that executable gives and error that:

SBARAI-M-J2V1:sgw-common sbarai$ protoc --go_out=. proto/subscriberDB/pdnInfoDb.proto
/Users/sbarai/go/bin//protoc-gen-go: /Users/sbarai/go/bin//protoc-gen-go: cannot execute binary file
--go_out: protoc-gen-go: Plugin failed with status code 126.

Most helpful comment

For example, to build an exact version of protoc-gen-go which match protoc 3.6.1

go get -d -u github.com/golang/protobuf/protoc-gen-go
cd ~/go/src/github.com/golang/protobuf/protoc-gen-go
git checkout 7e65e51
go build
mv protoc-gen-go ~/go/bin/

Then can do the protoc work successfully:

protoc --go_out=./go/ -I proto ./proto/helloworld.proto

All 9 comments

If you are using Go modules, running either of the following commands should fix the issues:

go get -u github.com/golang/protobuf/[email protected]
go get -u google.golang.org/protobuf/[email protected]

Essentially, you need to use the same version of the protobuf runtime as you are for the version of protoc-gen-go.

@dsnet Even after running the above command(s), issue persists. After converting the .proto file to .pb.go file i see the following kind of diff.

-// 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
+// This is a compile-time assertion that a sufficiently up-to-date version
+// of the legacy proto package is being used.
+const _ = proto.ProtoPackageIsVersion4. <<<<<<<<< This seems to be causing the issue during go build

Any idea what more could be missing

Looking at your original failure earlier:

.pb.go:26:11: undefined: "/vendor/github.com/golang/protobuf/proto".ProtoPackageIsVersion4

It seems you're not relying on modules, but are using the older vendor system. You'll need to update your the vendor directory.

@dsnet We are using this as part of a very big project with multiple teams involved, so updating the vendor directory may not be straightforward.

Hence if you can, please share the steps to download an older version of protoc-gen-go. I feel that may solve the issue temporarily for me. As the same is working on my other colleagues' system.

We are using this as part of a very big project with multiple teams involved, so updating the vendor directory may not be straightforward.

It's clear that you've got a deployment that is far from standard. I suggest you talk to whoever is in charge of managing the vendor directory. There's clearly a mismatch where you're trying to use a new version of protoc-gen-go with an old version of the runtime. You can obtain an old version of the binary in the same way as you would any other Go program, build it from an older version of the module.

You can obtain an old version of the binary in the same way as you would any other Go program, build it from an older version of the module.

@dsnet This brings me to my original question as I am new to this. "Need steps to download and install an older version of protoc-gen-go". It would be really helpful if you could please point me to the detailed steps for the same.

please point me to the detailed steps for the same.

It's the same way you would build any other Go program:

git clone https://github.com/golang/protobuf
cd protobuf
git checkout $VERSION
cd protoc-gen-go && go build

@dsnet Thanks a lot for sharing the detailed steps. It was really helpful.

By the way while reading through the readme file of protobuf, I found they had mentioned another way of doing it. Please let me know if any difference is there between the 2 approaches:

GIT_TAG='v1.3.5'
go get -d -u github.com/golang/protobuf/protoc-gen-go
git -C "$(go env GOPATH)"/src/github.com/golang/protobuf checkout $GIT_TAG
go install github.com/golang/protobuf/protoc-gen-go

For example, to build an exact version of protoc-gen-go which match protoc 3.6.1

go get -d -u github.com/golang/protobuf/protoc-gen-go
cd ~/go/src/github.com/golang/protobuf/protoc-gen-go
git checkout 7e65e51
go build
mv protoc-gen-go ~/go/bin/

Then can do the protoc work successfully:

protoc --go_out=./go/ -I proto ./proto/helloworld.proto
Was this page helpful?
0 / 5 - 0 ratings