可以将Go模块发布在与之前的非模块模块相同的路径下吗?

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

Can a Go module be published in the same path as the previous non-module module?

问题

我有一个小型库,一直托管在非模块仓库中,但现在我想将其转换为Go模块。

我成功执行了以下操作:

go mod init
go mod tidy

go.modgo.sum文件已经创建,并且库仍然通过了测试。

之前的版本是v2.0.0,所以我将版本增加到了v3.0.0,并在go.mod中的模块名称末尾添加了/v3。根据我在网上看到的,这就是所需的全部步骤。
我的go.mod文件如下所示:

module tssgit.reyrey.com/teschste/go-utils/v3

go 1.19

require (
	github.com/lib/pq v1.10.9
	golang.org/x/text v0.9.0
)

然后,我将库发布到了我们的内部git服务器,并认为一切准备就绪。

当我尝试将该库添加到一个新的Go模块项目时,我收到了以下错误信息:

Installing: tssgit.reyrey.com/teschste/go-utils/v3
Installing dependency: tssgit.reyrey.com/teschste/go-utils/v3
go: module tssgit.reyrey.com/teschste/go-utils@upgrade found (v2.0.0+incompatible), but does not contain package tssgit.reyrey.com/teschste/go-utils/v3
Failed to add dependencies: exit status 1

我发现,如果我将v3.0.0发布到一个新的路径(tssgit.reyrey.com/teschste/go-utils2),我就能成功将其添加到我的新项目中,即使我再次增加了主版本号。

我是否遗漏了某些允许我继续在相同路径发布的步骤,还是说这是不可能做到的?

非常感谢您的帮助!

英文:

I have a small library that has always been hosted in a non-module repository but I now want to convert it to a Go module.

I successfully performed:

go mod init
go mod tidy

The go.mod and go.sum files were created with no errors and the library still passes testing.

The previous version was v2.0.0, so I incremented the version to v3.0.0 and added /v3 to the end of the module name in go.mod. From what I have seen online, that is all that was necessary.
My go.mod files looks like this:

module tssgit.reyrey.com/teschste/go-utils/v3

go 1.19

require (
	github.com/lib/pq v1.10.9
	golang.org/x/text v0.9.0
)

I then published the library to our internal git server and thought I was ready to go.

When I attempted to add the library to a new Go module project, I received the following:

Installing: tssgit.reyrey.com/teschste/go-utils/v3
Installing dependency: tssgit.reyrey.com/teschste/go-utils/v3
go: module tssgit.reyrey.com/teschste/go-utils@upgrade found (v2.0.0+incompatible), but does not contain package tssgit.reyrey.com/teschste/go-utils/v3
Failed to add dependencies: exit status 1

I found that if I published v3.0.0 to a new path (tssgit.reyrey.com/teschste/go-utils2), I was able to successfully add it to my new project, even if I again incremented the major version.

Am I missing something that would allow me to continue publishing in the same path, or is that just something you cannot do?

Any help would be appreciated!

答案1

得分: 1

是的,这是支持的。版本子目录是可选的。

我找到了一个会导致你看到的错误的情况。当主分支没有更新(仍然指向标签v2.0.0),并且新的标签v3.0.0没有推送到服务器时,就会发生这种情况。请仔细检查以确保远程仓库已经更新。

如果远程仓库已经更新但仍然无法工作,你可以按照以下步骤缩小问题范围:

  1. 使用-x选项运行go get命令:

    $ go get -x tssgit.reyrey.com/teschste/go-utils/v3
    
  2. 进入输出中显示的目录。目录路径类似于:

    /home/username/go/pkg/mod/cache/vcs/96f42aa32430149c99ad6625ceafc5b59e047b9e11d6a03f687d59845b53b2d5
    
  3. 在该目录中,运行git ls-remote -q origin命令并检查输出。下面的示例是在我的机器上无法工作时的输出。你会看到HEADv2.0.0都指向相同的提交ID,并且没有v3.0.0

    $ git ls-remote -q origin
    5ac4c172806e80461086ea9feb485cec0b6a27f0	HEAD
    5ac4c172806e80461086ea9feb485cec0b6a27f0	refs/heads/main
    bbdc583c0b83489db1a30fa6ca8559ffa295a74d	refs/heads/other-branch
    5ac4c172806e80461086ea9feb485cec0b6a27f0	refs/tags/v2.0.0
    
英文:

Yes, this is supported. And the version subdirectory is optional.

I found one case that will result in the error you saw. This happens when the main branch is not updated (still points to the tag v2.0.0) and the new tag v3.0.0 is not pushed to the server. Please check carefully to make sure the remote repository has been updated.

If the remote repository has been updated but it still does not work, you can narrow down the issue like this:

  1. run go get with the -x option:

    $ go get -x tssgit.reyrey.com/teschste/go-utils/v3
    
  2. cd into the directory shown in the output. The directory path is something like this:

    /home/username/go/pkg/mod/cache/vcs/96f42aa32430149c99ad6625ceafc5b59e047b9e11d6a03f687d59845b53b2d5
    
  3. in this directory, run git ls-remote -q origin and check the output. The example below is the one on my machine when it does not work. You see that both HEAD and v2.0.0 point to the same commit id, and there is not v3.0.0.

    $ git ls-remote -q origin
    5ac4c172806e80461086ea9feb485cec0b6a27f0	HEAD
    5ac4c172806e80461086ea9feb485cec0b6a27f0	refs/heads/main
    bbdc583c0b83489db1a30fa6ca8559ffa295a74d	refs/heads/other-branch
    5ac4c172806e80461086ea9feb485cec0b6a27f0	refs/tags/v2.0.0
    

huangapple
  • 本文由 发表于 2023年5月18日 03:49:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275723.html
匿名

发表评论

匿名网友

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

确定