导入间接的传统存储库

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

Go importing legacy repository in indirect

问题

所以我有一个多仓库项目,计划从头开始重新制作它,只保留我认为有用的功能,所以我决定将代码克隆到具有清晰提交历史的新仓库中。

遗留项目的组织结构如下:

我首先尝试将这3个代码库克隆到3个新的仓库中:

然后,我发布了它们的v0.0.1版本,这样我就可以使用replace将go.mod指向正确的“新”版本,像这样:

replace github.com/org/legacy-utils v1.0.0 => github.com/org/new-utils v0.0.1

这是我的本地文件树:

❯ tree
.
├── new-core
├── new-utils
├── new-engine

不幸的是,replace并没有按照我预期的那样工作,new仓库中的某些内部引用仍然指向运行时的legacy,而legacy项目将在后续被移除,所以这是不允许的。

为了弄清楚原因,我决定使用go-imports-rename将所有三个项目的文件中的导入进行重命名,并将它们指向正确的新版本。

然后,我删除了go.mod和go.sum,分别对这三个仓库运行了以下命令:

  • go clean -modcache
  • go mod init github.com/org/new-...

提交并重新发布了utils和engine的新的v0.0.1预发布版本。这时事情变得奇怪起来...

尽管在new-utils中我没有任何对遗留仓库的引用,但new-engine仍然告诉我legacy-utils是new-utils的间接导入。

我的engine的go.mod文件如下所示:


go 1.20

require (
	github.com/org/new-utils v0.0.1
    ... // 省略了一些额外的导入
)

require (
    github.com/org/legacy-utils v1.0.0 // indirect
)

它不应该有任何对遗留仓库的引用。

由于这是我目前工作的公司的私有项目,我不能提供关于项目本身的具体信息。

英文:

So I have this multi-repo project and am planning to almost remake it from ground up, just keeping the features I think are useful, so I decided to clone the code to new repositories with clean commit history.

The legacy project was organized like this:

I first tried cloning these 3 code bases into 3 newer repositories:

I then released a v0.0.1 of them just so I could point the go.mod to the correct "new" versions using replace like this:

replace github.com/org/legacy-utils v1.0.0 => github.com/org/new-utils v0.0.1

This is my local file tree:

❯ tree
.
├── new-core
├── new-utils
├── new-engine

Unfortunately, replace didn't worked as I expected, and some parts of the inner references of new repositories still points to the legacy in runtime, the legacy project will be removed further on so this could not happen.

Trying to figure out why, I decided to rename all imports in the files of all three projects using go-imports-rename and point them to the correctly newer versions.

I then removed the go.mod and go.sum, ran:

  • go clean -modcache
  • go mod init github.com/org/new-...

for the three repos respectively.

Commited and re-released a newer v0.0.1 pre-release version of utils and engine. This is where things got weird...

Although I don't have ANY reference to the legacy repositories in the new-utils, the new-engine still tells me that the legacy-utils is a indirect import in new-utils.

my go.mod of the engine looks like this:

module github.com/org/new-engine

go 1.20

require (
	github.com/org/new-utils v0.0.1
    ... // omitted some extra imports
)

require (
    github.com/org/legacy-utils v1.0.0 // indirect
)

It should not have any reference to the legacy repositories.

Since this is a private project of the company I currently work on, I can't give any specific info about the project itself.

答案1

得分: 1

根据@JimB的最后一条评论,我决定检查我的存储库中的所有标签。我删除了所有的新发布和标签,重新执行了go clean -modcache命令,并检查了GOPATH pkg文件夹是否有一些缓存的内容。

删除了utils和engine的go.sum和go.mod文件。

重新执行了go mod init命令,并在运行go mod tidy之前使用go get命令获取了正确的私有版本。

这样导入就正确了,我能够推送到主分支并在我的核心项目中正确使用这些导入。

谢谢@JimB。

英文:

So based in the last comment by @JimB.

I decided to check on all the tags in my repos. I deleted all new releases and tags, reexecuted go clean -modcache and checked the GOPATH pkg folders if there were some cached stuff.

Removed go.sum and go.mod for both utils and engine.

Re-executed the go mod init and executed go get to the correct private versions before running go mod tidy.

This way the imports got right and I was able to push to the main branch and use those imports correctly in my core project.

Thank you @JimB

huangapple
  • 本文由 发表于 2023年6月24日 21:35:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76546312.html
匿名

发表评论

匿名网友

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

确定