Home > Software engineering >  Go module with multiple path
Go module with multiple path

Time:12-02

I have a go module that is mirrored to several locations. One is in gitlab and the other bitbucket. When trying to base it off of the new gitlab location I get:

go: gitlab.com/company/[email protected]: parsing go.mod: module declares its path as: bitbucket.org/company/core but was required as: gitlab.com/company/core

I know why this is happening, but how can I define my go.mod to be either or?

CodePudding user response:

Short answer: You can't.

But all may not be lost...

If you have code in both the location referenced by the module path and in some other location that for some reason you prefer to download from, you can configure a git alias to redirect the reference from the stated module path to the desired download source:

git config --global --add url."https://<download path>".insteadOf "https://<module path>"

The alias doesn't have to be a complete replacement. You can use an alias to replace a prefix on any module path. So if you have a number of modules hosted on server X that you have identified (in their module path) on server Y, you only need to alias the server host prefix and path to cover all of these modules (as long as the repo names are the same).

For example, we use this to create 'friendly' module paths for modules hosted in our on-prem AzureDevOps Server git repos, for example:

git config --global --add url."https://tfs.myorg.com/myorgcollection/_git".insteadOf "https://tfs.myorg.com"

Our modules are then able to identify themselves as tfs.myorg.com/somemodule.git but when go get attempts to fetch this git will look for it at https://tfs.myorg.com/myorgcollection/_git/somemodule.git

(the .git suffix is a necessary Azure DevOps peculiarity)

To be honest, I haven't tried this with entirely different servers, only for "rewriting" paths on the same server, but in principle it should work. So in your case:

git config --global --add url."https://gitlab.com/company/".insteadOf "https://bitbucket.org/company/"

Because this works at the git level of things, as far as go is concerned it is still getting from bitbucket.org even though git is going to gitlab.com under the hood.

i.e. when you go get you need to reference the declared module path:

go get bitbucket.org/company/core

The only downside of this is that you need to configure the alias on any/all workstations (and build machines) that need to go get from any location other than that declared in the module path.

  • Related