Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

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?

like image 229
Finlay Weber Avatar asked Apr 19 '20 20:04

Finlay Weber


People also ask

What is module path in go?

What is Go Module. A Module is a collection of Go packages stored in a file tree under $GOPATH/pkg folder with a go. mod file at its root. This file defines the module's path, which is also the import path used for your project, and its dependency requirements, which are the other modules needed for a successful build.

How does go mod vendor work?

The go mod vendor command constructs a directory named vendor in the main module's root directory that contains copies of all packages needed to support builds and tests of packages in the main module. Packages that are only imported by tests of packages outside the main module are not included.

How do you use init mod?

Create a greetings directory for your Go module source code. Start your module using the go mod init command. Run the go mod init command, giving it your module path -- here, use example.com/greetings . If you publish a module, this must be a path from which your module can be downloaded by Go tools.


3 Answers

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.

like image 117
Vahid Avatar answered Oct 13 '22 06:10

Vahid


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
like image 29
Dimitar Dimitrov Avatar answered Oct 13 '22 07:10

Dimitar Dimitrov


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.

like image 9
James Avatar answered Oct 13 '22 07:10

James