Search code examples
gitgogoimports

Go importing legacy repository in 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.


Solution

  • 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