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

Go

Go Problem Overview


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/my-version@v0.5.2: 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 Solutions


Solution 1 - Go

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 - Go

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 - Go

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 - Go

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 - Go

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 - Go

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 - Go

This worked for me:

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

Solution 8 - Go

delete the go.mod and go.sum

go clean -modcache && go init 

It worked for me

Solution 9 - Go

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 - Go

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFinlay WeberView Question on Stackoverflow
Solution 1 - GoVahidView Answer on Stackoverflow
Solution 2 - GoDimitar DimitrovView Answer on Stackoverflow
Solution 3 - GoRambatinoView Answer on Stackoverflow
Solution 4 - GoJamesView Answer on Stackoverflow
Solution 5 - GoPeng WangView Answer on Stackoverflow
Solution 6 - Goa0sView Answer on Stackoverflow
Solution 7 - GoSunil KumarView Answer on Stackoverflow
Solution 8 - GoSrivignesh NagarajanView Answer on Stackoverflow
Solution 9 - GoXewusView Answer on Stackoverflow
Solution 10 - GoMandeep GillView Answer on Stackoverflow