I find this one of the more confusing aspects of Protobufs (and gRPC).
I think the issue being solved is that Protobufs need to:
- Permit namespacing to scope services|messages to e.g. DNS domains
- Support a myriad programming languages (which implement namespacing differently).
protoc
has options that permit (re)mapping of e.g. a protobuf's package v1/api
to a language-specific namespace using protobuf options, e.g. go_package
). See Go Generated Code for Golang.
This is slightly more complex when using Go Modules but, in summary, what you're experiencing is probably some (unexpected) combination of the above where, the sample code assumes one module name and protoc
is building on the assumption of a different one.
TL;DR update the code's module reference to reflect the correctly generated pb
path. If the generate code is in the wrong place, you can simply move it to the correct subdirectory (path) but it's better to update your protoc
command to generate the files to the correct directory.
Example
something.proto
:
syntax = "proto3";
package v1alpha1;
option go_package = "github.com/me/my-protos;v1alpha1";
NOTE go_package
aliases the proto package v1alpha1
to what I want to reference as github.com/me/my-protos
in Golang.
Then I generate with:
MODULE="github.com/me/my-protos"
protoc \
--proto_path=. \
--go_out=./api/v1alpha1 \
--go_opt=module=${MODULE} \
--go-grpc_out=./api/v1alpha1 \
--go-grpc_opt=module=${MODULE} \
./something.proto
NOTE This example generate gRPC code too. It avoids (!) protoc
creating a path github.com/me/my-protos
for the generated code because I'm generating the source in that repo. I just want the relative path ./api/v1alpha
where the files will be created.
Which yields:
my-protos
├── something.proto
├── api
│ └── v1alpha1
│ ├── something_grpc.pb.go
│ └── something.pb.go
├── go.mod
├── go.sum
└── README.md
And I can import with:
import (
pb "github.com/me/my-protos/api/v1alpha1"
)
NOTE From a different repo, I can now access my protos repo github.com/me/my-project/api/v1alpha1
combining the repro and the generated location to give my desired path.