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
CodePudding user response:
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.
CodePudding user response:
You appear to be doing everything correctly.
I found this transition by the Google team insufficiently documented and problematic too.
I was unaware that it's possible to compile protos using Go's code generation (//go:generate protoc).
Have you tried running the (latest version of) protoc on the command-line instead? If nothing else, perhaps this yield additional debugging.
On the machine I'm using, go install places the binaries in ${HOME}/go/bin but GOBIN is not set and the directory is not in my ${PATH}. I suspect (!?) if I tried protoc, I'd encounter problems too.
Do you have GOBIN or underlying directory in your PATH?
If all else fails, have you deleted occurrences of protoc-gen-go* on your machine and re-go install'd?
