Fix Go commands all giving "go: error loading module requirements"? - go

I am trying to install the dependencies for a Go project that uses Go modules. I have GO111MODULES=on in my environment.
No matter what command I run (go build, go get, go clean --modcache, ...) it fails with output something like:
sdgluck$ go build .
go: finding cloud.google.com/go v0.26.0
go: cloud.google.com/go#v0.26.0: unknown revision refs/tags/v0.26.0
go: error loading module requirements
The last line seems to appear in the output for every command, alongside some mention of cloud.google.com/go.
How can I fix this and just get Go to install the modules for this project?

As stated in a comment below, the proper command to clear the Go modules cache is go clean -modcache (just one dash).
If that still doesn't work then you can try manually clearing your Go mod folder.
For example, if your GOPATH was /Users/spongebob/go:
rm -rf /Users/spongebob/go/pkg/mod

Related

Golangļ¼šmodule declares its path as: bou.ke/monkey, but was required as: github.com/bouk/monkey [duplicate]

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?
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
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.
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.
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.
Use the go mod edit -replace command.
Example 1:
go mod edit -replace github.com/y/original-project=github.com/x/my-version#v0.5.2
Example 2:
go mod edit -replace github.com/codahale/hdrhistogram=github.com/HdrHistogram/hdrhistogram-go#v0.9.0
From: https://github.com/HdrHistogram/hdrhistogram-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
In my case, I didn't want to just fix on local, but be sure it worked for everyone.
After changing values in go.mod (it was better to delete the go.mod and go.sum files then run
go mod init github.com/organization/new-package-name
go mod tidy
go get github.com/organization/new-package-name
Above didn't really solve the problem for me, nor did go clean -modcache.
What worked for me instead was deleting then recreating the tags on the newly forked project. (suggestion: Delete all tags from a Git repository)
Then create a new tag.
git tag v1.0.0 -m "new version"
git push origin v1.0.0
After this, you can just install via
go get github.com/organization/new-package-name#latest
OR
go get github.com/organization/new-package-name#v1.0.0
delete the go.mod and go.sum
go clean -modcache && go init
It worked for me
In my case, I edited my go.mod and added:
require github.com/anyuser/anyrepo **latest**
Then, in the terminal, entered:
go mod download
This worked for me:
Remove go.mod file from the directory
Run go clean -modcache
Run go get
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.
What worked for me was
Changing module github.com/otherperson/therepo -> module github.com/me/therepo in the go.mod
then running
go get github.com/myfork#latest_commit_hash
I just did this and it worked.
go get -u "github.com/org/package#latest"
Actually I had the same error, I'm importing a private repo of mine from Github and the problem was that the name of the package ( first line in go.mod ) not the same name as the repo url. So we need to make sure that they are the same.
Error:
Github repo url: github.com/Mansouri147/private-repo
First line go.mod: module mansouri147/different-name
Success:
Github repo url: github.com/Mansouri147/private-repo
First line go.mod: module github.com/Mansouri147/private-repo
i just input go clean -modcache and input go mod tidy, then solve.

How can I update this outdated Go module to fix the the "but was required as" error? [duplicate]

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?
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
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.
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.
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.
Use the go mod edit -replace command.
Example 1:
go mod edit -replace github.com/y/original-project=github.com/x/my-version#v0.5.2
Example 2:
go mod edit -replace github.com/codahale/hdrhistogram=github.com/HdrHistogram/hdrhistogram-go#v0.9.0
From: https://github.com/HdrHistogram/hdrhistogram-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
In my case, I didn't want to just fix on local, but be sure it worked for everyone.
After changing values in go.mod (it was better to delete the go.mod and go.sum files then run
go mod init github.com/organization/new-package-name
go mod tidy
go get github.com/organization/new-package-name
Above didn't really solve the problem for me, nor did go clean -modcache.
What worked for me instead was deleting then recreating the tags on the newly forked project. (suggestion: Delete all tags from a Git repository)
Then create a new tag.
git tag v1.0.0 -m "new version"
git push origin v1.0.0
After this, you can just install via
go get github.com/organization/new-package-name#latest
OR
go get github.com/organization/new-package-name#v1.0.0
delete the go.mod and go.sum
go clean -modcache && go init
It worked for me
In my case, I edited my go.mod and added:
require github.com/anyuser/anyrepo **latest**
Then, in the terminal, entered:
go mod download
This worked for me:
Remove go.mod file from the directory
Run go clean -modcache
Run go get
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.
What worked for me was
Changing module github.com/otherperson/therepo -> module github.com/me/therepo in the go.mod
then running
go get github.com/myfork#latest_commit_hash
I just did this and it worked.
go get -u "github.com/org/package#latest"
Actually I had the same error, I'm importing a private repo of mine from Github and the problem was that the name of the package ( first line in go.mod ) not the same name as the repo url. So we need to make sure that they are the same.
Error:
Github repo url: github.com/Mansouri147/private-repo
First line go.mod: module mansouri147/different-name
Success:
Github repo url: github.com/Mansouri147/private-repo
First line go.mod: module github.com/Mansouri147/private-repo
i just input go clean -modcache and input go mod tidy, then solve.

How to fix parsing go.mod module declares its path as "github.com/xx" but was required as "git-codecommit.cn-northwest-1.amazonaws.com.cn" [duplicate]

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?
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
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.
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.
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.
Use the go mod edit -replace command.
Example 1:
go mod edit -replace github.com/y/original-project=github.com/x/my-version#v0.5.2
Example 2:
go mod edit -replace github.com/codahale/hdrhistogram=github.com/HdrHistogram/hdrhistogram-go#v0.9.0
From: https://github.com/HdrHistogram/hdrhistogram-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
In my case, I didn't want to just fix on local, but be sure it worked for everyone.
After changing values in go.mod (it was better to delete the go.mod and go.sum files then run
go mod init github.com/organization/new-package-name
go mod tidy
go get github.com/organization/new-package-name
Above didn't really solve the problem for me, nor did go clean -modcache.
What worked for me instead was deleting then recreating the tags on the newly forked project. (suggestion: Delete all tags from a Git repository)
Then create a new tag.
git tag v1.0.0 -m "new version"
git push origin v1.0.0
After this, you can just install via
go get github.com/organization/new-package-name#latest
OR
go get github.com/organization/new-package-name#v1.0.0
delete the go.mod and go.sum
go clean -modcache && go init
It worked for me
In my case, I edited my go.mod and added:
require github.com/anyuser/anyrepo **latest**
Then, in the terminal, entered:
go mod download
This worked for me:
Remove go.mod file from the directory
Run go clean -modcache
Run go get
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.
What worked for me was
Changing module github.com/otherperson/therepo -> module github.com/me/therepo in the go.mod
then running
go get github.com/myfork#latest_commit_hash
I just did this and it worked.
go get -u "github.com/org/package#latest"
Actually I had the same error, I'm importing a private repo of mine from Github and the problem was that the name of the package ( first line in go.mod ) not the same name as the repo url. So we need to make sure that they are the same.
Error:
Github repo url: github.com/Mansouri147/private-repo
First line go.mod: module mansouri147/different-name
Success:
Github repo url: github.com/Mansouri147/private-repo
First line go.mod: module github.com/Mansouri147/private-repo
i just input go clean -modcache and input go mod tidy, then solve.

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/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?
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
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.
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.
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.
Use the go mod edit -replace command.
Example 1:
go mod edit -replace github.com/y/original-project=github.com/x/my-version#v0.5.2
Example 2:
go mod edit -replace github.com/codahale/hdrhistogram=github.com/HdrHistogram/hdrhistogram-go#v0.9.0
From: https://github.com/HdrHistogram/hdrhistogram-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
In my case, I didn't want to just fix on local, but be sure it worked for everyone.
After changing values in go.mod (it was better to delete the go.mod and go.sum files then run
go mod init github.com/organization/new-package-name
go mod tidy
go get github.com/organization/new-package-name
Above didn't really solve the problem for me, nor did go clean -modcache.
What worked for me instead was deleting then recreating the tags on the newly forked project. (suggestion: Delete all tags from a Git repository)
Then create a new tag.
git tag v1.0.0 -m "new version"
git push origin v1.0.0
After this, you can just install via
go get github.com/organization/new-package-name#latest
OR
go get github.com/organization/new-package-name#v1.0.0
delete the go.mod and go.sum
go clean -modcache && go init
It worked for me
In my case, I edited my go.mod and added:
require github.com/anyuser/anyrepo **latest**
Then, in the terminal, entered:
go mod download
This worked for me:
Remove go.mod file from the directory
Run go clean -modcache
Run go get
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.
What worked for me was
Changing module github.com/otherperson/therepo -> module github.com/me/therepo in the go.mod
then running
go get github.com/myfork#latest_commit_hash
I just did this and it worked.
go get -u "github.com/org/package#latest"
Actually I had the same error, I'm importing a private repo of mine from Github and the problem was that the name of the package ( first line in go.mod ) not the same name as the repo url. So we need to make sure that they are the same.
Error:
Github repo url: github.com/Mansouri147/private-repo
First line go.mod: module mansouri147/different-name
Success:
Github repo url: github.com/Mansouri147/private-repo
First line go.mod: module github.com/Mansouri147/private-repo
i just input go clean -modcache and input go mod tidy, then solve.

`go install` adds record in `go.mod`

I'm a bit confused about how Go modules work on installing binaries using go install.
I tried to install (https://github.com/joho/godotenv) binary by executing go install github.com/joho/godotenv/cmd/godotenv and I found out that it adds a record in the go.mod.
I'm lost as we don't use this package in the code and after running go mod tidy it gets deleted (as it is not it the code).
Can someone explain is it expected behaviour of go modules?
Secondly, how I could avoid adding it to the go.mod as we only need to install and execute the binary?
Thanks.
Go version: go version go1.13.4 darwin/amd64
Command go: The go.mod file:
The go command automatically updates go.mod each time it uses the module graph, to make sure go.mod always accurately reflects reality and is properly formatted.
The go tool will update go.mod automatically when it detects dependencies are inaccurate when performing a build.
When you install github.com/joho/godotenv/cmd/godotenv from your module, this install requires at least the package in question being built / installed (and also its dependencies, transitively).
You may safely run go mod tidy to undo the recording of this "one-time" dependency.
In general if you want to disallow the go tool to update the go.mod file, you may use the -mod=readonly flag, but that would fail go install ("can't load package: package xxx: import lookup disabled by -mod=readonly"). You can read more about this here: Go Wiki: Go modules: Can I control when go.mod gets updated and when the go tools use the network to satisfy dependencies?
Alternatively, if you want to avoid this, build / install your tools outside of your module. You may use a "dummy" module for this.

Resources