How to use legacy version of protoc-gen-go with plugins - protocol-buffers

I'm not being able to generate the protobuffer files for grpc
protoc -I=./ --go_out=plugins=grpc:. code/proto/*
--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC
See https://grpc.io/docs/languages/go/quickstart/#regenerate-grpc-code for more information.
make: *** [protoc-gen] Error 1
I don't want to regenerate using the new version cause is not backwards compatible, I need to use the version (I don't find which one is it) that can run with plugins

I've been able to generate valid code (doesnt break anything) with this command. First using --go-grpc_out as a replacement of the grpc plugin used before and second using this flag require_unimplemented_servers=false to avoid adding an extra method that can break your code if you're using polimorphism
protoc -I=./ --go-grpc_out=require_unimplemented_servers=false:. --go_out=. code/proto/*

Related

Getting "protoc": executable file not found in $PATH when running "go generate" command

We have an application written in GoLang and we are using GRPC for defining service contracts.
When we try to run "go generate" command to generate stub from proto file it gives following error:
main.go:4: running "protoc": exec: "protoc": executable file not found in $PATH
Command format in code:
//go:generate protoc -I . --go-grpc_out=. --go_out=. ./proto/service.proto
Note:
I have already installed protoc-gen-go and protoc-gen-go-grpc
Tried to search for the above error but haven't been able to find a solution yet.
protoc-gen-go and protoc-gen-go-grpc are plugins for protoc. You need to install the protoc (the Protobuf compiler) itself, as explained in Go gRPC - Prerequisites.
See protobuf - Releases page for a download.
On a Mac can just use brew install protobuf.

How am I supposed to use protoc-gen-go-grpc?

I am trying to generate Go code for some Protocol Buffers as well as a gRPC service.
In the past I have used https://github.com/golang/protobuf with a generate command that looked something like this:
//go:generate protoc --proto_path=. --go-out=. --go_opt=paths=source_relative <file>.proto
//go:generate protoc --proto_path=. --go_grpc_out=. --go_grpc_opt=paths=source_relative <file>.proto
This method worked just fine, but the repo has since been superceded by google.golang.org/protobuf and google.golang.org/grpc.
From what I've read, this was an intentional split to separate the protobuf and gRPC project release cycles.
Using the new repositories, I installed the tools like this:
go install google.golang.org/protobuf/cmd/protoc-gen-go#latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc#latest
Both tools are installed, and are visible on the path. The problem that I'm encountering is that protoc or protoc-gen-go is looking for the older tool - protoc-gen-go_grpc. Note the underscore.
I have been reading all the documentation and Github issues that I can find, and haven't yet been able to figure out how I am meant to use the new protoc-gen-go-grpc. Note the dash.
$ echo $GOPATH
/Users/<username>/dev/go
$ echo $GOBIN
/Users/<username>/dev/go/bin
$ which protoc
/opt/homebrew/bin/protoc
$ which protoc-gen-go
/Users/<username>/dev/go/bin/protoc-gen-go
$ which protoc-gen-go_grpc
protoc-gen-go_grpc not found
$ which protoc-gen-go-grpc
/Users/<username>/dev/go/bin/protoc-gen-go-grpc
My issue was so simple to fix, but darn annoying to find.
When running protoc, using the --go_grpc_out flag looks for protoc-gen-go_grpc.
When running protoc, using --go-grpc_out flag looks for protoc-gen-go-grpc.
It seems that instead of protoc having well defined, documented flags, and erroring when an incorrect flag is provided they're using the flags to determine which plugin to use. If you misspell the flag, it looks for the wrong plugin.

protoc-gen-go specific version require

I need a pre build project and I need a version of protoc-gen-go v1.25.0-devel but I am unable to find command etc to install It , till now I tried to do something like this :
go get -u google.golang.org/protobuf/cmd/protoc-gen-go#v1.25.0-devel
and I am getting this ERROR invalid version: unknown revision cmd/protoc-gen-go/v1.25.0-devel
Is there any way to get this version ?
If you're following this post to create a sample service, you don't need the specific version 1.25.0-devel that appears in the generated files. You should use the latest stable version (currently v1.26.0). Since it is an minor version upgrade, there should be no breaking changes between versions. One thing that you may need to make the examples in the post to work is that you should set the package for the generated code. You can set it in the .proto file as an option, for example option go_package = grpc-example/generated/protos/calc or as a command line argument for the protoc command, for example protoc ...OTHER_OPTS --go_opt=Mprotos/calc.proto=grpc-example/generated/protos/calc.
If you are sure that you absolutely need the specific version v1.25.0-devel you can install the plugin pointing to a specific commit (as that version is not currently available as a tag/branch name). If you're using go 1.16.x you can use go install google.golang.org/grpc/cmd/protoc-gen-go-grpc#a9513eb pointing at this commit. For older go versions, use go get

protoc-gen-go-rpc: program not found or is not executable - Where do I get protoc-gen-go-rpc?

We are using protoc-gen-go v2 (v2 is google.golang.org/protobuf, v1 is github.com/golang/protobuf)
When we are trying to compile our gRPC services, it tells us that plugins is not supported anymore and we should instead use --go-grpc_out:
$ protoc --go_out=plugins=grpc:. *.proto
--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC
And when using --go-grpc_out, it is telling us we need the protoc-gen-go-rpc:
$ protoc --go-rpc_out=. *.proto
protoc-gen-go-rpc: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
Where do you find, or how do you install protoc-gen-go-rpc?
Update: We also just found this post on Reddit for additional information
https://www.reddit.com/r/golang/comments/fe3a4k/documentation_on_getting_grpc_working_with_the/
I'm not sure about protoc-gen-go v2, because the latest protobuf tag I can see is v1.4.
Here is the example how we call protoc to generate source code for sevice called crab that has no external dependencies (e.g. no external protofile imports). The only binary dependency is protoc-gen-go
protoc -I $(pwd) -I /home/isaev/go/src \
$(pwd)/crab/error_codes.proto \
$(pwd)/crab/goproxy.proto \
$(pwd)/crab/crab.proto \
--go_out=plugins=grpc:/tmp/crab

when compile protocol buffer, missing input or program is not executable error

I'm trying to make golang+grpc server on mac. I installed golang, grpc, protocol buffer and checked that grpc server following this example https://grpc.io/docs/quickstart/go/
The problem is when I try to compile .proto file using protocol buffer it said 'missing input' or 'program is not executable'.
install protocol buffer and grpc
brew install grpc protobuf
install golang based protoc plugin
go get github.com/golang/protobuf/protoc-gen-go
protoc path
which protoc
/usr/local/bin/protoc
example location
/Users/usrname/go/src/google.golang.org/grpc/examples/helloworld
run proto compiler
protoc -I=/Users/username/go/src/google.golang.org/grpc/examples/helloworld/helloworld/ --go_out=/Users/username/go/src/google.golang.org/grpc/examples/helloworld/helloworld/ /Users/username/go/src/google.golang.org/grpc/examples/helloworld/helloworld/helloworld.proto
error message
protoc-gen-go: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--go_out: protoc-gen-go: Plugin failed with status code 1.
This was mentioned in golang/protobuf issue 795
See the Installation section of the page README:
Grab the code from the repository and install the proto package.
The simplest way is to run go get -u github.com/golang/protobuf/protoc-gen-go.
The compiler plugin, protoc-gen-go, will be installed in $GOPATH/bin unless $GOBIN is set. It must be in your $PATH for the protocol compiler, protoc, to find it.
So double-check the value of your $GOPATH (~/go by default if not set)
If $GOPATH/bin (or ~/go/bin) in your $PATH?
The OP camilla confirms it is not:
$PATH also has :/Users/username/go/bin/protoc-gen-go
It should have :/Users/username/go/bin

Resources