如何从GitLab子组加载模块?

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

How to load modules from gitlab subgroup?

问题

我写了一个程序,想要封装一些逻辑。

所以我创建了一个新的模块,并将其推送到我的git上。git的链接看起来像这样:
gitlab.xxx.ru/group/subgroup/proj
但是当我尝试使用go get获取它时,出现了错误

fatal: «https://xxx.ru:@gitlab.xxx.ru/group/subgroup.git/» unreachable: URL using bad/illegal format or missing URL

Go试图加载subgroup而不是project。

我在$GOPATH/src/下创建了文件夹gitlab.xxx.ru/group/subgroup/并克隆了我的项目。

现在它显示

could not import gitlab.xxx.ru/group/subgroup/proj (no required module provides package "gitlab.xxx.ru/group/subgroup/proj")

所以,如果我理解正确,在Golang 1.16中,我不能只是将我的项目放在正确的目录中,也不能使用本地包。

如何修复从我的GitLab加载并使用ssh加载它?

谢谢。

我的proj中的go.mod文件如下。

module gitlab.xxx.ru/group/subgroup/proj

go 1.16

require (
	golang.org/x/sys v0.0.0-20210608053332-aa57babbf139
	golang.org/x/text v0.3.6
)
英文:

I wrote a program and want to encapsulate some logic.

So I did new module and pull it in my git. Link for git looks like
gitlab.xxx.ru/group/subgroup/proj
but when I tried to get it with go get, I got error

fatal: «https://xxx.ru:@gitlab.xxx.ru/group/subgroup.git/» unreachable: URL using bad/illegal format or missing URL

Go tried to load subgroup instead project.

I made folder gitlab.xxx.ru/group/subgroup/ in $GOPATH/src/ and clone my project.

And now it wrote

could not import gitlab.xxx.ru/group/subgroup/proj (no required module provides package "gitlab.xxx.ru/group/subgroup/proj")

So, if I understand correctly, in Golang 1.16 I can't just put my project in the correct directory and I can't use local packages.

How to fix loading from my GitLab and load it with ssh?

Thank you.

UDP go.mod in my proj.

module gitlab.xxx.ru/group/subgroup/proj

go 1.16

require (
	golang.org/x/sys v0.0.0-20210608053332-aa57babbf139
	golang.org/x/text v0.3.6
)

答案1

得分: 9

您可能遇到了Gitlab的预期行为,它会阻止go在计算项目依赖关系时获取子组列表。由于go get请求是未经身份验证的,因此根组之外的现有项目是不可见的,无法找到。

为了克服这个限制,Gitlab尚未提供解决方案,您可以使用以下两种方法之一:

  • 将项目放在根目录而不是子组中(不一定总是可行的)
  • 在导入您的模块的项目中,利用.git扩展名和替换指令(见下文)

如果Go知道项目所在的子组,它就能够获取该子组下的项目,前提是知道版本控制限定符(.git)。为了指示这一点,请确保使用以下格式导入项目:gitlab.xxx.ru/group/subgroup/proj.git

虽然仅仅这样就可以工作,但这将强制您在导入中使用所有这些.git。为了克服这个问题,您还需要在您的go.mod文件中添加一个replace指令,以便您可以使用原始的导入路径。

最终,导入您的模块的项目的go.mod文件应该如下所示:

require(
 gitlab.xxx.ru/group/subgroup/proj v1.7.0
)

replace(
 gitlab.xxx.ru/group/subgroup/proj => gitlab.xxx.ru/group/subgroup/proj.git v1.7.0
)

英文:

You may be hitting the intended behaviour from Gitlab which will prevent go from fetching the list of subgroups while trying to compute project dependencies. Since go get requests are unauthenticated, existing projects not under the root group are invisible and cannot be found.

To overcome this limitation, which Gitlab has yet to provide a solution, you can use one of the following two approaches:

  • Have the project located at the root and not in a subgroup (not always possible)
  • Leverage the .git extension as well as the replace directive in the project which imports your module (see below)

Go is able to fetch the project living under a subgroup if it knows the version control qualifier (.git). To indicate this, make sure you import the project using the following format gitlab.xxx.ru/group/subgroup/proj.git

While this alone works, it would force you to have all those .git in your imports. To overcome this, you also need a replace directive in your go.mod so you can use the original import path.

In the end, the project which imports your module should have a go.mod that look like this:

require(
 gitlab.xxx.ru/group/subgroup/proj v1.7.0
)

replace(
 gitlab.xxx.ru/group/subgroup/proj => gitlab.xxx.ru/group/subgroup/proj.git v1.7.0
)

huangapple
  • 本文由 发表于 2021年6月11日 00:09:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/67924850.html
匿名

发表评论

匿名网友

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

确定