protocol-buffersprotocgrpc-go

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:

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?


Solution

  • 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.