I'm trying to create shared protobuf definitions in a shared go module, but I'm not having much luck. Specifically:
- I want a proto file in module protos that contains shared definitions.
- Other proto files in module protos would reference the shared definitions.
- Module protos-use would reference module protos (via go.mod)
- Module protos-use go code would import and use definitions from module protos
- Both modules are stored in github.com
I'm unable to get the various package names to align. No matter what I use, something fails. Currently, module protos doesn't have any errors, but go mod tidy on module protos-use fails.
Module protos is at https://github.com/rob-woerner/protos
Module protos-use is at https://github.com/rob-woerner/protos-use
I haven't found an example that addresses this scenario. If I simply duplicate the shared proto definitions everywhere, I can get it to work. The most recent error is:
$ go mod tidy
go: finding module for package github.com/rob-woerner/protos/messages
go: finding module for package github.com/rob-woerner/protos/common
github.com/rob-woerner/protos-use/code imports
github.com/rob-woerner/protos/common: module github.com/rob-woerner/protos@latest found (XXX), but does not contain package github.com/rob-woerner/protos/common
github.com/rob-woerner/protos-use/code imports
github.com/rob-woerner/protos/messages: module github.com/rob-woerner/protos@latest found (XXX), but does not contain package github.com/rob-woerner/protos/messages
But I emphasize that I get lots of different errors, depending on what package names I use in the dozen or so places that need package names.
If anyone knows of a working example, or if this is not supported, I'd appreciate it.
Code snippets are elided for compactness
Module protos:
go.mod
Makefile
protobuf:
common.proto
messages.proto
pb:
common.pb.go
messages.pb.go
Module protos-use:
go.mod
code:
project.go
Module protos:
go.mod:
module protos
go 1.16
require google.golang.org/protobuf v1.27.1
protobuf/common.proto:
package common;
option go_package = "github.com/rob-woerner/protos";
enum MyState { LOGGED_OUT = 0; }
protobuf/messages.proto:
import "common.proto";
package messages;
option go_package = "github.com/rob-woerner/protos";
message MyRequest { common.MyState state = 1; }
Makefile:
protos:
protoc --proto_path=protobuf --go_out=plugins=grpc:pb --go_opt=module=github.com/rob-woerner/protos protobuf/*.proto
Module protos-use:
go.mod:
module github.com/rob-woerner/protos-use
go 1.16
require (
github.com/rob-woerner/protos latest
)
code/project.go:
package code
import (
"github.com/rob-woerner/protos/common"
"github.com/rob-woerner/protos/messages"
)
func MyFunc() messages.MyMessage {
return messages.MyMessage { state: common.MyState_LOGGED_OUT}
}