如何解决Go模块在两个版本之间声明其路径为”x”,但要求为”y”的问题。

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

How to resolve Go module declares its path as "x" but was required as "y" between 2 versions

问题

最近我不得不将私有仓库从SaaS Gitlab迁移到本地版本。一切都进行得很顺利,我做了以下操作:

  1. 将仓库a中旧的go模块路径从old.com/workspace/a更新为new.com/workspace/a
  2. 在最新的提交上添加了一个新的标签v1.2.3-new
  3. 将仓库b更新为引用最新的标签v1.2.3-new,路径为new.com/workspace/a
  4. 在仓库b中运行go mod tidy并验证其是否正常工作。

现在我有一个要求,需要引用new.com/workspace/a的旧版本标签(最初是old.com/workspace/a)。因此,在仓库a中,我切换到了旧的标签,并将模块路径从old.com/workspace/a修复为new.com/workspace/a,并将其标记为v1.1.1-new

然后在仓库b中,我使用v1.1.1-new引用了new.com/workspace/a。然而,这导致了以下错误:

go: new.com/workspace/a@v1.1.1-new: parsing go.mod:
	module declares its path as: old.com/workspace/b
	        but was required as: new.com/workspace/b

如果我在仓库a中检查v1.1.1-new标签,可以看到go.mod文件中的模块路径已正确设置为:

module new.com/workspace/a

我不清楚为什么在最新的提交上使用v1.2.3-new标签时可以正常工作,但在引用旧提交时却失败了。

英文:

I recently had to move private repositories from SaaS Gitlab to an on-premise version. Everything was going well where I did:

  1. Update old go module paths in repo a from old.com/workspace/a to new.com/workspace/a
  2. Add a new tag v1.2.3-new to the latest commit
  3. Update repo b to reference latest tag v1.2.3-new from new.com/workspace/a
  4. Run go mod tidy in repo b and verify it works

Now I have a requirement to reference an older version tag of new.com/workspace/a (originally old.com/workspace/a). So in repo a, I checked out the older tag, fixed up the module path to new.com/workspace/a from old.com/workspace/a and tagged it as v1.1.1-new.

In repo b then I referenced new.com/workspace/a with v1.1.1-new. However, this results in:

go: new.com/workspace/a@v1.1.1-new: parsing go.mod:
	module declares its path as: old.com/workspace/b
	        but was required as: new.com/workspace/b

If I check the v1.1.1-new tag in repo a, the module path is set correctly in the go.modfile:

module new.com/workspace/a

It is unclear to me why it works with the tag v1.2.3-new on the latest commit but fails when I reference an older commit.

答案1

得分: 1

所以我不能说我完全理解为什么这样做可以成功,但以下是使其成功的步骤(包括未成功的部分)。

  1. 我尝试使用 go clean -modcache 清除缓存。

  2. 使用以下命令进行测试,但仍然失败。

    go get new.com/workspace/a@v1.1.1-new
    
  3. 根据我在原始问题中的评论,这通过 v1.1.1-new 提交哈希值成功。所以我再次尝试了这个方法。

    go get new.com/workspace/a@27ca81f7
    

    现在它选择了该提交的版本,并且成功了。go.mod 文件也正确地更新了标签/版本,尽管在 go get 命令中使用了提交哈希值。

    new.com/workspace/a v1.1.1-new
    
英文:

So I can't say I fully understood why this worked but here are the steps that made it work (including what didn't).

  1. I resorted to clearing the cache with go clean -modcache

  2. Tested with the following but it still failed.

    go get new.com/workspace/a@v1.1.1-new
    
  3. As per my comment in the original question, this worked via the v1.1.1-new commit hash So I resorted to that again.

    go get  new.com/workspace/a@27ca81f7
    

    Now it picked up the version for that commit and was successful. The
    go.mod file was also correctly updated with the tag/version despite
    using the commit hash in the go get command.

    new.com/workspace/a v1.1.1-new
    

huangapple
  • 本文由 发表于 2022年9月19日 17:25:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/73771268.html
匿名

发表评论

匿名网友

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

确定