如何使用本地版本的Glide依赖项?

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

How to use local versions of glide dependencies?

问题

我正在处理一个使用Glide进行依赖管理的Go项目。我需要对项目本身以及项目使用的几个依赖进行更改。在将每个项目的更改单独提交之前,我需要一起测试这些更改。

我该如何告诉Glide(或Go)使用这些项目的本地版本(例如在$GOPATH/src/...中找到的版本),而不是下载并将其放入vendor目录中?

举个例子:

  • github.com/hyperledger/burrow 依赖于:
  • github.com/tendermint/tendermint,它又依赖于:
  • github.com/tendermint/go-crypto

我需要对这三个项目进行一些跨项目的更改。我想通过在burrow项目中执行操作来测试这些更改,但我需要它执行我本地的开发版本的tendermintgo-crypto

英文:

I'm working on a Go project that uses Glide for dependency management. I need to make changes to the project, but also to several dependencies used by the project. I need to test these changes together, before committing the alterations to each project separately.

How can I tell Glide (or Go) to use local versions of these projects (e.g. found in $GOPATH/src/...) rather than pulling down something and stuffing it in the vendor directory)?

To give an example:

  • github.com/hyperledger/burrow depends on:
  • github.com/tendermint/tendermint, which depends on:
  • github.com/tendermint/go-crypto

I need to make some changes spanning all three projects. I want to test the changes by executing things in the burrow project, but I need it to execute my development versions of tendermint and go-crypto, which I have locally.

答案1

得分: 12

如果您想测试位于"$GOPATH/src/..."下的依赖项,只需暂时删除项目根目录中的所有glide文件(glide.yaml、glide.lock等)。
然后,如果您需要再次使用'vendor'依赖项,可以将这些文件放回原处。

更新1

我也在寻找相同的解决方案,然后我发现glide文档中指定了一个在glide.yaml中用于导入的额外有用的参数。
根据官方文档

> repo: 如果包名不是存储库位置,或者这是一个私有存储库,可以在此处指定。
包将从存储库检出,并放在包名指定的位置。
这允许使用分支。

在这种情况下,您只需将代码推送到某个地方(我猜是GitHub或Gitlab,用于您的$GOPATH/src/github.com/hyperledger/burrow的私有存储库),然后编辑您的glide.yaml:

<!-- language-all: none -->

- package: github.com/tendermint/tendermint &lt;-- 供应商名称依赖项
  repo:    github.com/myrepo/tendermint   &lt;-- 您的远程分支
  version: vx.x.x 或您的sha提交代码

这样,您就可以从官方版本切换到您的分支并进行测试。
要切换回官方版本,只需删除或注释掉repo和version属性:

- package: github.com/tendermint/tendermint &lt;-- 供应商名称依赖项
#  repo:    github.com/myrepo/tendermint   &lt;-- 您的远程分支
#  version: vx.x.x 或您的sha提交代码

我现在正在使用这种方式测试我的分支,您无需更改代码中的导入路径,希望对您有所帮助。

更新2

另一种有用的方法是使用glide mirror

> 镜像提供了将存储库位置替换为原始存储库的镜像位置的能力。
当您想要为持续集成(CI)系统创建缓存或者想要在本地位置上处理依赖项时,这非常有用。

我认为这是最好的解决方案之一,例如,在命令行中输入:

$ glide mirror set github.com/tendermint/tendermint file:///User/yourname/Gospace/src/github.com/tendermint/tendermint

这将在您的GLIDE_HOME中创建一个mirror.yaml文件(如果不存在,则将放置在您的$USER/.glide文件夹下)。
现在,您可以测试您的本地版本(在GOPATH下)而无需分叉您的项目(如上所述)。
完成测试后,只需将其删除:

$ glide mirror remove github.com/tendermint/tendermint
英文:

If you want to test the dependencies under '$GOPATH/src/...' just temporally remove every glide file in the root of your project (glide.yaml, glide.lock,etc...).
Then you can put back those files if you need to use 'vendor' dependencies again.

Update 1

I was looking for the same solution for my projects then I figured out that glide documentation specifies an extra useful parameter on glide.yaml for imports.
According to the official documentation:

> repo: If the package name isn't the repo location or
this is a private repository it can go here.
The package will be checked out from the repo
and put where the package name specifies.
This allows using forks.

In that case you just push your code somewhere (GitHub or Gitlab for private repos of your $GOPATH/src/github.com/hyperledger/burrow I guess) then edit your glide.yaml:

<!-- language-all: none -->

- package: github.com/tendermint/tendermint &lt;-- vendor name dependencies
  repo:    github.com/myrepo/tendermint   &lt;-- your remote fork
  version: vx.x.x or your sha commit code

In this way you can switch from your official version to your fork and make your tests.
To turn back to your official version just remove or comment repo and version attributes:

- package: github.com/tendermint/tendermint &lt;-- vendor name dependencies
#  repo:    github.com/myrepo/tendermint   &lt;-- your remote fork
#  version: vx.x.x or your sha commit code

I'm testing my forks in this way now and you don't need to change your import paths into your code, hope this helps.

Update 2

Another useful way is to use glide mirror:

> Mirrors provide the ability to replace a repo location with
another location that's a mirror of the original.
This is useful when you want to have a cache for your continuous
integration (CI) system or if you want to work on a dependency in
a local location.

I guess this one is the best solution, for example, on command line type:

$ glide mirror set github.com/tendermint/tendermint file:///User/yourname/Gospace/src/github.com/tendermint/tendermint

This will create a mirror.yaml in your GLIDE_HOME (if not exists then will be placed under your $USER/.glide folder).
Now you can test your local version (under GOPATH) without fork your project (as I wrote above).
Once you finished your tests just remove it:

$ glide mirror remove github.com/tendermint/tendermint

答案2

得分: 4

鉴于Go的供应商首先在项目根目录的/vendor文件夹中查找软件包,如果找不到,则在GOPATH中查找,而Glide 安装依赖项到/vendor文件夹意味着您只需使用glide install设置项目,然后手动从项目的/vendor文件夹中删除已被本地修改(或将要修改)的依赖项,Go供应商将会选择在GOPATH下找到的本地修改版本。

一切测试完毕后,您首先需要提交所有修改过的依赖项,然后返回到项目并修改glide.yaml文件以使用这些依赖项的新版本。

这应该可以工作。

英文:

Given the Go vendoring is first looking up packages in your project root /vendor folder and when not found then in the GOPATH and that Glide installs your dependencies to the /vendor folder means that you can just set up your project with glide install, then manually remove the dependencies that have been locally modified (or you are going to modify) in their local installations from your project's /vendor folder and Go vendoring will then pick your locally modified versions found under GOPATH.

Once you have tested everything, you first have to commit all the modified dependencies and then head back to your project and modify the glide.yaml file to use the new versions of these dependencies.

This should work.

huangapple
  • 本文由 发表于 2017年6月28日 20:39:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/44802725.html
匿名

发表评论

匿名网友

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

确定