英文:
Go module path resolution
问题
我目前有一个Go项目(gRPC微服务),其中有一个嵌套模块导出了生成的gRPC代码。以下是该项目的结构:
/cmd
- main.go
/internal - pkg1/
-- pkg1.go
go.mod
go.sum
/api - /proto
- /v1
- hello.proto
- /v1
- /gen
- go.mod
- go.sum
- /v1
- hello.pb.go
- hello_grpc.pb.go
我希望将生成的proto和gRPC代码发布为Go模块,以便其他微服务可以使用它们。例如,此存储库托管在github.com/myOrg/service
。
api/gen/go.mod
文件内容如下:
module github.com/myOrg/service/grpc
go 1.18
我成功推送了一个标签为grpc/v0.0.1
的发布版本。然而,当我运行go list -m -versions github.com/myOrg/service/grpc
时,我遇到了以下错误:
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
这是因为go list期望嵌套模块位于github.com/myOrg/service/grpc
,而实际上它位于github.com/myOrg/service/api/gen
。如果是这样,唯一的解决方法是将生成的代码放在github.com/myOrg/service/grpc
,还是有办法使模块github.com/myOrg/service/grpc
解析为github.com/myOrg/service/api/gen
?
英文:
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
?
答案1
得分: 0
根据Go Modules参考文档,是的,模块github.com/myOrg/service/grpc
应该位于子目录grpc
中,没有办法使其解析到子目录api/gen
。以下是相关问题的文档部分:
仓库中的模块目录
一旦模块的仓库在特定的修订版本上被检出,go命令必须找到包含该模块的go.mod文件的目录(即模块的根目录)。
回想一下,模块路径由三个部分组成:仓库根路径(对应仓库的根目录),模块子目录和主要版本后缀(仅适用于发布为v2或更高版本的模块)。
对于大多数模块来说,模块路径等于仓库根路径,因此模块的根目录就是仓库的根目录。
有时模块会定义在仓库的子目录中。这通常用于具有多个需要独立发布和版本化的组件的大型仓库。这样的模块应该位于与仓库根路径之后的模块路径的子目录中。例如,假设模块example.com/monorepo/foo/bar位于具有根路径example.com/monorepo的仓库中。它的go.mod文件必须位于foo/bar子目录中。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论