Go模块与多路径

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

Go module with multiple path

问题

我有一个 Go 模块,它被镜像到几个位置。一个在 GitLab,另一个在 Bitbucket。当我尝试基于新的 GitLab 位置进行操作时,我遇到了以下错误:

go: gitlab.com/company/core@v1.0.21: 解析 go.mod 文件时出错:
模块声明的路径为:bitbucket.org/company/core
但要求的路径为:gitlab.com/company/core

我知道为什么会出现这个问题,但我该如何定义我的 go.mod 文件以支持两个路径呢?

英文:

I have a go module that is mirrored to several locations. One is in gitlab and the other bitbucket. When trying to base it off of the new gitlab location I get:

> go: gitlab.com/company/core@v1.0.21: parsing go.mod:
module declares its path as: bitbucket.org/company/core
but was required as: gitlab.com/company/core

I know why this is happening, but how can I define my go.mod to be either or?

答案1

得分: 3

简短回答:你不能这样做

但也许并非一无所获...

如果你的代码既存在于模块路径所引用的位置,又存在于其他某个你更喜欢从中下载的位置,你可以配置一个git别名,将引用从所述模块路径重定向到所需的下载源:

git config --global --add url."https://<下载路径>".insteadOf "https://<模块路径>"

这个别名不必是一个_完全_替换。你可以使用别名来替换任何模块路径上的_前缀_。所以,如果你有一些模块托管在服务器X上,你已经在服务器Y上的模块路径中标识出来,你只需要将_服务器主机前缀和路径_设置为别名,以覆盖_所有_这些模块(只要仓库名称相同)。

例如,我们使用这个方法为我们在本地 AzureDevOps Server git 仓库中托管的模块创建“友好”的模块路径,例如:

git config --global --add url."https://tfs.myorg.com/myorgcollection/_git".insteadOf "https://tfs.myorg.com"

然后,我们的模块可以将自己标识为tfs.myorg.com/somemodule.git,但是当go get尝试获取时,git 会在https://tfs.myorg.com/myorgcollection/_git/somemodule.git上查找它。

.git后缀是 Azure DevOps 的一个特殊之处)

老实说,我还没有尝试过在完全不同的服务器上使用这个方法,只是用于在同一服务器上“重写”路径,但原则上应该可以工作。所以在你的情况下:

git config --global --add url."https://gitlab.com/company/".insteadOf "https://bitbucket.org/company/"

因为这是在git层面上工作的,对于go来说,它仍然从bitbucket.org获取,即使在幕后git实际上是去gitlab.com获取。

也就是说,当你使用go get时,你需要引用_声明的_模块路径:

go get bitbucket.org/company/core

唯一的缺点是你需要在任何/所有需要从除了模块路径声明的位置go get的工作站(和构建机器)上配置别名。

英文:

Short answer: You can't.

But all may not be lost...

If you have code in both the location referenced by the module path and in some other location that for some reason you prefer to download from, you can configure a git alias to redirect the reference from the stated module path to the desired download source:

git config --global --add url.&quot;https://&lt;download path&gt;&quot;.insteadOf &quot;https://&lt;module path&gt;&quot;

The alias doesn't have to be a complete replacement. You can use an alias to replace a prefix on any module path. So if you have a number of modules hosted on server X that you have identified (in their module path) on server Y, you only need to alias the server host prefix and path to cover all of these modules (as long as the repo names are the same).

For example, we use this to create 'friendly' module paths for modules hosted in our on-prem AzureDevOps Server git repos, for example:

git config --global --add url.&quot;https://tfs.myorg.com/myorgcollection/_git&quot;.insteadOf &quot;https://tfs.myorg.com&quot;

Our modules are then able to identify themselves as tfs.myorg.com/somemodule.git but when go get attempts to fetch this git will look for it at https://tfs.myorg.com/myorgcollection/_git/somemodule.git

(the .git suffix is a necessary Azure DevOps peculiarity)

To be honest, I haven't tried this with entirely different servers, only for "rewriting" paths on the same server, but in principle it should work. So in your case:

git config --global --add url.&quot;https://gitlab.com/company/&quot;.insteadOf &quot;https://bitbucket.org/company/&quot;

Because this works at the git level of things, as far as go is concerned it is still getting from bitbucket.org even though git is going to gitlab.com under the hood.

i.e. when you go get you need to reference the declared module path:

go get bitbucket.org/company/core

The only downside of this is that you need to configure the alias on any/all workstations (and build machines) that need to go get from any location other than that declared in the module path.

huangapple
  • 本文由 发表于 2022年12月2日 08:11:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/74649513.html
匿名

发表评论

匿名网友

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

确定