模块依赖性问题共同开发了两个单独的go模块
我正在创建一个小型实用程序,我们称之为 A ,为此我需要对另一个转到项目进行小改动,我们称之为 B .
I'm creating a small utility, let's call it A, for which I require a small change to another go project, let's call it B.
首先,我将 B 分叉到> B_forked ,并创建所需的PR.作者尚未审查它,这很好,不要着急.
First I forked B into B_forked, and created the needed PR. The author hasn't reviewed it yet, which is fine, no hurry.
但是我暂时暂时不使用 A 处理我的版本 B_forked .除此以外,我还希望能够共同开发两个文件(编辑文件并使它们相互更改),而不是将 A 粘贴到 B / B_forked ).
But I would like to leave A working with my version B_forked for the time being. More than that I would like to be able to co-develop both (edit files and make them pick up each others changes, as opposed to having A stuck to a version of B/B_forked).
因此,我编辑了 A 以导入 B_forked ,并在〜/Projects/A/go.mod
中写了以下内容:
So I edited A to import B_forked and wrote the following in my ~/Projects/A/go.mod
:
module A
go 1.15
require (
... # Other packages
)
replace B_forked => ../B_forked
然后在 B_forked 中输入以下〜/Projects/B_forked/go.mod
(版本号由go自动生成):
And in B_forked I entered (the version number was generated automatically by go) the following ~/Projects/B_forked/go.mod
:
module B_forked
go 1.15
require (
B v0.0.0-00010101000000-000000000000
... # Other packages
)
replace B => ./
在我的 B_forked 版本中,我不想将代码中的所有 import B/...
替换为 import B_forked/...
(因为我希望以后将更改包含在 B 的PR中).这就是为什么我在这里使用 replace
规则的原因.
In my B_forked version I don't want to replace all import B/...
in the code with import B_forked/...
(since I want the changes to be included in a PR to B later on). That's why I use the replace
rule here.
不知何故,我不知道为什么.构建 B_forked 似乎可行:
Somehow this is not working, and I can't figure out why. Building B_forked seems to work:
$ cd ~/Projects/B_forked
$ go build
$
但是当我尝试编译 A 时,我得到了:
But when I try to compile A, I get:
$ cd ~/Projects/A
$ go build
go: found github.com/janpfeifer/webcam in github.com/janpfeifer/webcam v0.0.0-00010101000000-000000000000
go: github.com/janpfeifer/webcam@v0.0.0-00010101000000-000000000000 requires
github.com/blackjack/webcam@v0.0.0-00010101000000-000000000000: invalid version: unknown revision 000000000000
-
janpfeifer/webcam
== B_forked -
21点/网络摄像头
== B - 当我构建 B_forked 时,go自己选择了 B 的版本号,即
-
janpfeifer/webcam
== B_forked -
blackjack/webcam
== B - The version number for B, that is
blackjack/webcam
, was picked by go itself, when I built B_forked
jackjack/webcam
.
我可能误解了go模块的底层抽象(到目前为止,我在模块上花费的时间比在简单代码本身上花费的时间要多得多).有什么想法要设置吗?也许有一种我不知道的更简单的滚动方法?
I'm probably misunderstanding the underlying abstraction of go modules (by now I spent much more time on modules than on the simple code itself). Any ideas how to set this up ? Maybe there is a much easier way to roll that I'm not aware ?
非常感谢!
在仔细阅读答案和评论(@ kostix,@ volker)并进行试验之后,在我看来,最有效的方法是:
After reading through answers and comments (thx @kostix, @volker) and experimenting, what seemed to work best in my case was:
将 B 叉入 B_forked ,并在 B_forked 中有2个分支:
Fork B into B_forked, and have 2 branches in B_forked:
-
在分支 b1 中,我用来创建github的PR.
In branch b1 is the one I used to create github's PR.
在分支 b2 中,我将所有 B 的自导入内容更改为 B_forked .之后,我使用 b2 与 A 共同开发.我 git cherry-pick
将更改提交回分支 b1 .
In branch b2 I changed all the self-imports of B to B_forked. After that I used b2 to co-developed with A. I git cherry-pick
the changes submitted back to branch b1.
在 A 中,我导入了 B_forked ,并在 go.mod
中将版本设置为 b2 的HEAD.>分支,因此它可以由其他人构建,而PR不会进入存储库 B .
In A I imported B_forked, and at go.mod
I set the version to the HEAD of b2 branch, so it can be built by others while the PR doesn't make into repository B.
在开发过程中,我将 B 中的 replace 规则添加到 A 的 go.mod
中磁盘目录,因此 B_forked 中的更改将立即显示在 A 中.
During development I add in A's go.mod
a replace rule from B_forked to its local disk directory, so changes in B_forked are seen immediately in A.
进入PR到 B 后,我会将 A 的导入更改回 B .
Once the PR to B is in, I'll change back A's imports to B.