How to change Go modules path?
Asked Answered
D

2

14

I got a github repo github.com/user/somerepo.

I init it by doing go mod init **github.com/user/somerepo**

I need to change this dependecy and make it point to another github repository. Let say : github.com/user/a-different-repo.

Is there any go command that can update all the import statements in all the files?

Demonism answered 7/6, 2020 at 8:57 Comment(0)
E
15

The go mod edit -replace is your friend for that.

From the doc (Source) :

The -replace=old[@v]=new[@v] flag adds a replacement of the given module path and version pair. If the @v in old@v is omitted, a replacement without a version on the left side is added,

Here is the important part :

which applies to all versions of the old module path. If the @v in new@v is omitted, the new path should be a local module root directory, not a module path. Note that -replace overrides any redundant replacements for old[@v], so omitting @v will drop existing replacements for specific versions.

Here is the AddReplace func which is responsible for the replacement.

Now to achieve it, each of your repository must be mapped to your GOPATH. A little recall on GOPATH :

When you want a repository to become a "go gettable" package you should map this repository to your GOPATH.

As explained here when you do go get, it will first looks up in your $GOPATH an take the latest version of the package (or the specific version if you specified it in your go.mod file)

Then you can achieve your edit by doing this command :

go mod edit -replace github.com/UserA/[email protected]=github.com/UserA/[email protected]

Another way (and maybe a better one) is to do this inside the go.mod file, like so :

  module foo.bar                        

  replace github.com/UserA/foo => github.com/UserA/bar

  require (   
  github.com/UserA/foo v0.0.1
 )   

Of course, this will work only if each repository is mapped into your GOPATH.

See also here for further explanation: when-should-i-use-the-replace-directive

Eliseo answered 7/6, 2020 at 9:3 Comment(7)
I have tried that but I'm getting : unversioned new path must be local directory as a response.Demonism
How do you manage your gopath ? Did you pull the repository in your local file directory ?Eliseo
@prophecy : i update my answer with an explanation on how to achieve this....please be sure that your repository is mapped to the GOPATHEliseo
I get the error even when pointing to a valid local directory. I have to use the fully qualified directory path for it not to error: /home/user/code/github.com/company/repo instead of github.com/company/repo (both a valid github repo and local directory)Numbing
@Numbing : as I mentioned, this might be because your location (/home/user/code/) : is not mapped into your GOPATH. Can you check ?Eliseo
@Eliseo interestingly enough if you have a local relative path that mirrors a github import, say, $GOPATH/github.com/org/repo (a folder) and the same exact import github.com/org/repo (a remote github repo) - go mod edit -replace can't differentiate between the two. However, if you do go mod edit -replace github.com/org/repo=/home/user/github.com/org/repo then you will be fine. Essentially, go mod edit -replace cannot differentiate between an ambiguous relative local path github.com/org/repo (the folder) and github.com/org/repo (the github repo).Numbing
All of my problems were fixed when I did go mod edit -replace github.com/org/repo=/home/user/work/github.com/org/repo aka using a fully qualified local folder path. This helps the go compiler know that the replace is targeting a local folder and not a remote github repo.Numbing
S
10

go mod edit -module github.com/user/a-different-repo

Sodden answered 14/6, 2021 at 7:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.