June 25, 2022

Go modules path and module name difference: fix

TLDR;

Just replace the crap v1-comit-etc with your branch name.

Let's say we want to get a library from mybranch:

āžœ go get -u organisation.com/awesomelibrary@mybranch

go: downloading gitlab.com/organisation.com/awesomelibrary v1.0.11-0.20220625112132-2b37f076d8f6

go :gitlab.com/organisation.com/awesomelibrary @v1.0.11-0.20220625112132-2b37f076d8f6:

parsing go.mod: module declares its path as: organisation.com/awesomelibrary

but was required as: gitlab.com/organisation.com/awesomelibrary

Sometimes, this issue arise when path and module name are different and we have this replace in go.mod file:

replace (
organisation/awesomelibrary => gitlab.com/organisation/awesomelibrary v1.0.11-0.20220625112132-some_old_commit_hash

)

The issue occurs because go get is trying to download module by its path declared in the replace in go.mod, it goes to gitlab.com, but go.mod says that it has a different module name.

There are numbers of solution to this issue, but the simplest for me was to substitute version and commit in go.mod with a branch name. In our case it will be:

replace (
organisation/awesomelibrary => gitlab.com/organisation/awesomelibrary mybranch

)

in the paragraph above removed "some_old_commit_hash" and substituted it with mybranch.

Then we just need to run go mod tidy and it will download the branch we want.