Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct format of protoc go_package?

I have an existing project in Go where I'm using Protocol buffers / gRPC. Until recent the go_package option was optional and the resulting Go package name would be the same as the proto package name.

This file lives in the project root. The generated code file (authenticator.pb.go) is living in the same location. Proto file:

syntax = "proto3";  package authenticator;  service Authenticator {...} 

Generation command specifies I want to output in the same directory:

protoc --go_out=plugins=grpc:. authenticator.proto 

Today I've pulled new version of the protocol buffers compiler and github.com/golang/protobuf/protoc-gen-go. Upon the first run a got a warning:

WARNING: Missing 'go_package' option in "authenticator.proto", please specify it with the full Go package path as a future release of protoc-gen-go will require this be specified. See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information. 

The suggested link is more or less useless. But the tutorial is a bit more explicit:

The go_package option defines the import path of the package which will contain all the generated code for this file. The Go package name will be the last path component of the import path. For example, our example will use a package name of "tutorialpb".

option go_package = "github.com/protocolbuffers/protobuf/examples/go/tutorialpb"; 

After adding this option to the proto file and rerunning the command, the output ends up in this path, relative to the project root. Something like:

$GOPATH/src/github.com/<org>/authenticator/github.com/<org>/authenticator/authenticator.pb.go 

I've tried the following alternatives as go_package names:

  • .
  • authenticator

Generation happened in the correct location, but I got the warning back:

WARNING: Deprecated use of 'go_package' option without a full import path... 

So what is the correct way without breaking the project layout?

like image 868
Tim Avatar asked May 07 '20 20:05

Tim


People also ask

What is option go_package in .proto file?

The go_package option defines the import path of the package which will contain all the generated code for this file. The Go package name will be the last path component of the import path — Google doc.

What is the protobuf file format?

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.

What is protoc command?

protoc is a compiler for protocol buffers definitions files. It can can generate C++, Java and Python source code for the classes defined in PROTO_FILE.

How do I run protoc Gen?

Using protocol buffers with Go To compile the protocol buffer definition, run protoc with the --go_out parameter set to the directory you want to output the Go code to. The generated files will be suffixed . pb.go. See the Test code below for an example using such a file.


1 Answers

For pure proto generations you can do the following

protoc --go_out=paths=source_relative:./gen -I. authenticator.proto 

or to generate for grpc the following can be used

protoc --go_out=plugins=grpc:./gen --go_opt=paths=source_relative authenticator.proto 

If that fails you likely have a newer version of protoc and need to use the following command instead.

protoc --go-grpc_out=./gen --go-grpc_opt=paths=source_relative authenticator.proto 

This is just Google making isolated decisions once again about how you should write and manage your code. However if you dig through enough issues of people having similar problems I found the above command can now be used. In addition to adding the full import path as shown in the other answer

option go_package = "github.com/example/path/gen;gen"; 

Following the above package and protoc command you would have your proto file in the project root with a module name of github.com/example/path. Your code would then be placed inside the gen folder which you will be responsible for creating.

You may need to adjust the output location of source_relative for your use but at least you don't end up with the duplication of paths or having to rely on placing your code in GOPATH again.

This may break going forward because of the additional changes found in this ticket. Unfortunately the binary name is exactly the same so if you run into issues read that ticket and try switching the version that you have installed until the new protoc-gen-go is ready for prod.

like image 158
eatingthenight Avatar answered Oct 20 '22 19:10

eatingthenight