'How to fix parsing go.mod module declares its path as "x" but was required as "y"

I am working on a go project, which has a dependency on original-project. I now want to change the behavior in this project by modifying original-project. So I cloned github.com/y/original-project to github.com/x/my-version and replaced all occurrence of github.com/y/original-project with github.com/x/my-version (including in mod.go).

But I keep getting this error:

go: github.com/x/[email protected]: parsing go.mod:
    module declares its path as: github.com/y/original-project
            but was required as: github.com/x/my-version

Even when I run go get -u -v -f all or github.com/x/my-version

What could I be doing wrong?

go


Solution 1:[1]

I had similar issue. I ended up removing the go.mod file in the project I was trying to import and running go mod init ... again. It fixed it.

Also, run go clean -modcache where you are importing.

Then try to go get ... your package.

Solution 2:[2]

I think the problem comes from the fact that the go.mod of your cloned version of original-project still says module github.com/y/original-project. You should use the go.mod replace directive. It is meant for cases like yours exactly.

replace github.com/y/original-project => /path/to/x/my-version

Solution 3:[3]

The trick is to update the go mod cache.

So after making the required changes in go.mod (i.e. github.com/X/Z => github.com/Y/Z), you'll then need to pull down the latest version which will update your local go mod cache.

i.e. go get github.com/Y/Z@fd02212

and then the error message will go.

Solution 4:[4]

I'm currently developing a Go package and had this issue. I initially used the module <package-name> syntax at the top of my go.mod file. Since the module wasn't downloading correctly, I switched the syntax to be module github.com/<user>/<package-name>. Unfortunately, my system was still stuck on the old download cache, even after I manually deleted the dependencies.

To fix the issue, I created a new release tag on GitHub for the project (v0.0.1-beta), and now it downloads fine. go get stores modules with the version tag, so this bypassed the issue.

Solution 5:[5]

Use the go mod edit -replace command.

Example 1:

go mod edit -replace github.com/y/original-project=github.com/x/[email protected]

Example 2:

go mod edit -replace github.com/codahale/hdrhistogram=github.com/HdrHistogram/[email protected]

From: https://github.com/HdrHistogram/hdrhistogram-go

Solution 6:[6]

The only way i found to fix this is try to find all occurrences of the old (and new, maybe) dependency and remove it manually

find ~/go | grep original-project
rm -rf ~/go/pkg/mod/cache/download/github.com/y/original-project
rm -rf ~/go/pkg/mod/cache/download/github.com/x/my-version
rm -rf ~/go/pkg/mod/github.com/y/original-project*
rm -rf ~/go/pkg/mod/github.com/x/my-version*
rm -rf ~/go/pkg/mod/cache/download/sumdb/sum.golang.org/lookup/github.com/y/original-project*
rm -rf ~/go/pkg/mod/cache/download/sumdb/sum.golang.org/lookup/github.com/x/my-version*

Then go get again

Solution 7:[7]

This worked for me:

  1. Remove go.mod file from the directory
  2. Run go clean -modcache
  3. Run go get

Solution 8:[8]

delete the go.mod and go.sum

go clean -modcache && go init 

It worked for me

Solution 9:[9]

In my case, I edited my go.mod and added:

require github.com/anyuser/anyrepo **latest**

Then, in the terminal, entered:

go mod download

Solution 10:[10]

I had the same issue with the package name, after searching a lot I found golang cache coming from golang proxy server "don't know how" but cache store on my mac cleaned by running go clean -modcache but when i use go mod download or go mod tidy it always pick the wrong version package.

after rewriting GOPROXY variable to GOPROXY="direct" everything starts working.

go env -w GOPROXY="direct"

If it starts working you can revert your GOPROXY settings and using direct will show the module downloading.

If you using Goland then make sure you have enabled Go Module Integration in settings for Autocomplete indirect modules.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vahid
Solution 2 Dimitar Dimitrov
Solution 3 Alexis Wilke
Solution 4 James
Solution 5 Greg Dubicki
Solution 6 a0s
Solution 7 Alexis Wilke
Solution 8 Jofre
Solution 9 Alexis Wilke
Solution 10