I currently have a Go project (gRPC microservice) which has a nested module that exports the generated gRPC code. The following is the structure of this project:
/cmd
- main.go
/internal
- pkg1/
-- pkg1.go
go.mod
go.sum
/api
- /proto
- /v1
- hello.proto
- /gen
- go.mod
- go.sum
- /v1
- hello.pb.go
- hello_grpc.pb.go
Reason being I am looking to publish the generated proto & gRPC code as a Go module so that other microservices can use them. For example, this repo is hosted at github.com/myOrg/service
api/gen/go.mod
:
module github.com/myOrg/service/grpc
go 1.18
I have pushed a release successfully with the tag grpc/v0.0.1
. However, when I do go list -m -versions github.com/myOrg/service/grpc
, I get the following error:
go: loading module retractions for github.com/myOrg/service/grpc@v0.0.1: version "v0.0.1" invalid: missing github.com/myOrg/service/grpc/go.mod at revision grpc/v0.0.1
Is this because go list is expecting the nested module to be at github.com/myOrg/service/grpc
when instead it is at github.com/myOrg/service/api/gen
? If so, is the only workaround to put the generated code at github.com/myOrg/service/grpc
or is there a way to make the module github.com/myOrg/service/grpc
resolve to github.com/myOrg/service/api/gen
?
According to the Go Modules Reference, yes, the module github.com/myOrg/service/grpc
should be located in the subdirectory grpc
and there is not a workaround to make it resolve to the subdirectory api/gen
. Here is the doc (with the parts related to this question highlighted):
Module directories within a repository
Once a module’s repository has been checked out at a specific revision, the go command must locate the directory that contains the module’s go.mod file (the module’s root directory).
Recall that a module path consists of three parts: a repository root path (corresponding to the repository root directory), a module subdirectory, and a major version suffix (only for modules released at v2 or higher).
For most modules, the module path is equal to the repository root path, so the module’s root directory is the repository’s root directory.
Modules are sometimes defined in repository subdirectories. This is typically done for large repositories with multiple components that need to be released and versioned independently. Such a module is expected to be found in a subdirectory that matches the part of the module’s path after the repository root path. For example, suppose the module example.com/monorepo/foo/bar is in the repository with root path example.com/monorepo. Its go.mod file must be in the foo/bar subdirectory.