go.work.sum文件在Go 1.18中跟踪了什么?

huangapple go评论82阅读模式
英文:

What does the go.work.sum file track in Go 1.18?

问题

我刚刚在一个现有项目上尝试了Go 1.18工作区。考虑以下项目目录结构:

project-root/
|-- app/
|  |-- go.mod
|  |-- go.sum

根据文档,我在项目的根目录中运行了命令go work init ./app。这个命令创建了一个预期的go.work文件,但它还创建了一个意外的go.work.sum文件。

令人困惑的是,go.work.sum引用了两个可以在go.sum中找到的模块,但是当比较go.sumgo.work.sum时,每个模块的版本并不相同。然后还有一个问题,为什么go.work.sum只引用了这两个模块,而没有引用其他模块?请注意,工作区中只有一个模块。

go.work.sum文件跟踪的是什么?有没有相关的文档说明?

英文:

I just tried out the Go 1.18 workspace on an existing project. Consider the following project directory structure:

project-root/
|-- app/
|  |-- go.mod
|  |-- go.sum

Per documentation, I ran the command go work init ./app within the root directory of the project. This command created a go.work file as expected, but it also created a go.work.sum file that was not expected.

What's confusing is that go.work.sum references two modules that can be found in go.sum, but the version of each module is not the same when compared between go.sum and go.work.sum. Then there is also the question why are only these two modules referenced in go.work.sum but none of the other ones? Note that there is only the one module within the workspace.

What does the go.work.sum file track? Is it documented anywhere?

答案1

得分: 9

go.work.sum文件在相关的功能提案中提到(似乎只在这里提到了?):

https://go.googlesource.com/proposal/+/master/design/45713-workspace.md#files

go命令将使用工作区模块中存在的所有go.sum文件的集合来验证依赖模块,但存在一种情况,即工作区模块的go.mod文件本身不完整,go命令将会将缺失的校验和添加到工作区的go.work.sum文件中,而不是模块的go.sum文件中。

该提案(与上述链接相同)还描述了另一种情况,即两个项目都不直接导入某个模块的特定版本的包,但作为间接依赖项需要该模块,因此缺少了该模块代码的校验和。

因此,你的子模块可能出现了这些情况之一。如果是前一种情况,我预计在子模块上运行go mod tidy可以使一切同步,并且不再需要go.work.sum。根据你的描述,听起来更像是后一种情况,那么go.work.sum是必要的,用于跟踪缺失的校验和。

英文:

The go.work.sum file is mentioned in the related feature proposal (and seemingly nowhere else?):

https://go.googlesource.com/proposal/+/master/design/45713-workspace.md#files

> The go command will use the collective set of go.sum files that exist across the workspace modules to verify dependency modules, but there are cases where the go.sum files in the workspace modules collectively do not contain all sums needed to verify the build: The simpler case is if the workspace go.mod files themselves are incomplete, the go command will add missing sums to the workspace‘s go.work.sum file rather than to the module’s go.sum.

The proposal (same link as above) also describes one more use case where neither of the individual projects import packages from some particular version of a module, but require it as indirect dependency, thus missing the checksum for the module's code.

So your sub-modules may show one of these situations. If it's the former, I'd expect running go mod tidy on the sub-modules puts everything in sync and removes the need for go.work.sum. Based on your description, it sounds like it's the latter instead, then go.work.sum is necessary to track the missing checksums.

huangapple
  • 本文由 发表于 2022年3月25日 09:26:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/71611196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定