go module #latest found but does not contain package - go

I'm trying to make use of go module for the first time. What exactly the following error message is telling me?
module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli
module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli/ext
It happens during go build, whereas go get is just fine:
$ go get -v github.com/mkideal/cli
go: github.com/mkideal/cli upgrade => v0.2.2
but not go get -v ./..., which gave me the same error as above. My proxy setting looks OK:
$ go env | grep GOPROXY
GOPROXY="https://proxy.golang.org,direct"
Is it a problem of the go module/package I'm trying to use, or my own code's problem? -- I took a look at
https://github.com/mkideal/cli/blob/master/go.mod and it seems fine to me.
See the following update for details.
How can I overcome the situation? (I'm getting the same error message for my own repo as well)
UPDATE:
Here is the full log how I'm getting the above error:
prepare /tmp/015-file from https://github.com/mkideal/cli/blob/master/_examples/015-file
do go mod init
then go build
Now the details:
$ cd /tmp/015-file
$ GO111MODULE=on
$ go mod init github.com/mkideal/cli/015-file
go: creating new go.mod: module github.com/mkideal/cli/015-file
$ cat go.mod
module github.com/mkideal/cli/015-file
go 1.14
$ go build
go: finding module for package github.com/mkideal/cli
go: finding module for package github.com/mkideal/cli/ext
main.go:6:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli
main.go:7:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli/ext
$ go get -v github.com/mkideal/cli
go: github.com/mkideal/cli upgrade => v0.2.2
$ go get -v ./...
go: finding module for package github.com/mkideal/cli
go: finding module for package github.com/mkideal/cli/ext
go: finding module for package github.com/mkideal/cli
go: finding module for package github.com/mkideal/cli/ext
main.go:6:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli
main.go:7:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli/ext
$ go version
go version go1.14.1 linux/amd64

Try clearing cache:
go clean -modcache
For more info on how this command works, use go help clean

In my case cleaning cache didn't help.
Running go install in a project root printed no Go files in ... and that was the root cause, in the same time running go install gitlab.com/.... printed info about a missing package.
What had to be done was creating a go file in a project root directory with main function.

Update to go version go1.14.3 linux/amd64
Clear go module cache
don't know which one solved the problem (or both), now AOK.

I had the same error, but in my case I was attempting to import a module that made available only resource files, and no go pkgs. Adding an empty go file in the module with a package declaration solved it.

In my case, go.mod files were under src, after moving the go.mod file into one level up, then it works
Refer the Samples below,
directory structure when "package not found" error
dir1/src/
main.go
go.mod
go.sum
directory structure after fix
dir1/
go.mod
go.sum
src/
main.go

I had a similar problem. In my case the package name was not matching the name of the folder it resided in.

Related

go: module found but does not contain package

I am trying to install the net package for go but get "does not contain package error".
Terminal screenshot:
I have consulted: go module #latest found but does not contain package but none of the solutions seem to work for me.
I am using go version go1.18.5 linux/amd64
You have to initialize your module with go mod init in the project root directory
For local codebase
go mod init test
OR for hosted codebase e.g. github repo: test, github user: radiant
go mod init github.com/radiant/test
It will produce a go.mod file.
Then you can get the required package as:
go get golang.org/x/net
go mod tidy
Then import and use the net packages.
Hope this helps.
go get -u golang.org/x/net
go install used for executable packages

Go mod use last version, not commit

I want to use library according to last commit not to last release. So I got that version:
$ go get github.com/epsagon/epsagon-go#636ea43
Now in packages cache I have:
$ cd /Users/sgetman/go/pkg/mod/github.com/epsagon && ls
epsagon-go#v1.14.0 epsagon-go#v1.14.1-0.20201105151128-636ea43d1943
But when I try go build, go test, go mod tidy:
go: finding module for package github.com/epsagon/epsagon-go/epsagon/wrappers/gin
github.com/nexmoinc/neru-runtimelib/router imports
github.com/epsagon/epsagon-go/epsagon/wrappers/gin: module github.com/epsagon/epsagon-go#latest found (v1.14.0), but does not contain package github.com/epsagon/epsagon-go/epsagon/wrappers/gin
At the same time my go.mod:
require (
...
github.com/epsagon/epsagon-go v1.14.1-0.20201105151128-636ea43d1943
...
)
go.sum:
github.com/epsagon/epsagon-go v1.14.0 h1:Tq7qyoyDs2aUCc/UsQEHFt89aXVdUmjWXHwvS5kfSC4=
github.com/epsagon/epsagon-go v1.14.1-0.20201105151128-636ea43d1943 h1:kJGvRsqRfo1h8vEEGajWa+szA9965Epw83Fm3UmmwEc=
github.com/epsagon/epsagon-go v1.14.1-0.20201105151128-636ea43d1943/go.mod h1:Q73D3EhfzqmQa6m6Xi5n0Ugw9l6XSNGCzMcozsFMD1c=
Could you please help me to sort why go mod relys on the latest version, not version I provided?
The go command is checking the latest version because package github.com/nexmoinc/neru-runtimelib/router contains an import statement like import "github.com/epsagon/epsagon-go/epsagon/wrappers/gin".
github.com/epsagon/epsagon-go v1.14.1-0.20201105151128-636ea43d1943 does not contain such a package, so the go command is trying to figure out whether it can upgrade that module to a newer version in order to find the imported package.
If you run go build -mod=readonly, you will hopefully get a clearer error message. (Note that -mod=readonly will be the default as of Go 1.16: see https://tip.golang.org/doc/go1.16#modules.)
These steps worked for me although I do not know if they are the recommended ones
Comment out all instances of that import path in all files
remove go.sum
did go mod tidy
did go mod vendor
did go get -u -d
uncommented the lines in all files again
did go mod tidy again

go.mod has post-v0 module path "git.example.com/owner/repo/v3" at revision ...?

My coworker pushed a tag v3.0.1 before updating go.mod to have /v3 suffix (https://github.com/golang/go/wiki/Modules#releasing-modules-v2-or-higher). I have updated module path (go.mod) and all import paths (*.go) to fix it, tagged as v3.0.2.
Now the problem is:
go get -v git.example.com/owner/repo#v3.0.2
go: finding git.example.com/owner/repo v3.0.2
go: git.example.com/owner/repo#v0.0.0-20190722053407-d85c4f69ad17: go.mod has post-v0 module path "git.example.com/owner/repo/v3" at revision
d85c4f69ad17
Found this: go build keeps complaining that: go.mod has post-v0 module path
So, I deleted both v3.0.0 and v3.0.1 tags, pointed it to the latest commit, re-pushed but the problem still stand.
I noticed that go.mod still refered to the old version as an indirect dependency:
require (
git.example.com/owner.repo v0.1.2 // indirect
Even if I changed it to /v3 v3.0.2 it will be restored to v0.1.12 automatically.
Why?
Did I miss something?
Tue Jul 23 05:54:56 +07 2019
rm go.*
go mod init git.example.com/dependent/project
go mod tidy
and go.mod is updated correctly now:
require (
- git.example.com/owner/repo v0.1.2
+ git.example.com/owner/repo/v3 v3.0.2
but go get -v git.example.com/owner/repo#v3.0.2 still returned the error:
go: finding git.example.com/owner/repo v3.0.2
go: git.example.com/owner/repo#v0.0.0-20190722053407-d85c4f69ad17: go.mod has post-v0 module path "git.example.com/owner/repo/v3" at revision
d85c4f69ad17
(d85c4f69ad17 is the latest commit in master)
I noticed that there are both v0.1.2 and v3.0.2 in go.sum:
git.example.com/owner/repo v0.1.2 h1:mCGJEmyrFDTCGkRfUIORpqdrNkSONQ6K+AcTNgxqveY=
git.example.com/owner/repo v0.1.2/go.mod h1:FfUKnyPrARCtAXQZ3BQVJI7h2eJ0UpQBMLg4bNs4Kdc=
git.example.com/owner/repo/v3 v3.0.2 h1:mJtDKLeiP8vMRSZo08i/k/KDbIoZTlKW2aWu7DUBvMM=
git.example.com/owner/repo/v3 v3.0.2/go.mod h1:64LE0ts0Lk9InIQyhPYGmnxs6LZIl6H4Iorl1EXfqxo=
Please pay attention to my go get command:
go get -v git.example.com/owner/repo#v3.0.2
It should be:
go get -v git.example.com/owner/repo/v3#v3.0.2
for example you can replace repository with this hack: https://github.com/golang/go/wiki/Modules
require {
...
}
replace git.example.com/owner.repo v0.1.2 => git.example.com/owner.repo v3.0.2
or you can use go get at the commit hash you want:
go get git.example.com/owner.repo#af044c0995fe
go get will correctly update the dependency files (go.mod, go.sum).
For more information: https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies
or for last example you should clean cache
remove go.mod and go.sum
go cache clean
go mod vendor
Expanding on the answer from #quanta...
You are doing:
go get -v git.example.com/owner/repo#v3.0.2
Because it is a v3 module, the go get command should include a /v3 before the #:
go get -v git.example.com/owner/repo/v3#v3.0.2
Once a v3.x.y package is a module with its own go.mod, then whenenver you are operating with modules enabled, you pretty much always include the /v3 whenever you reference the v3.x.y module, including in:
arguments to go get on the command line
import statements in .go code for the consumer
require statements in a consumer's go.mod
replace or exclude statements in a consumer's go.mod
the module line of the v3 module's go.mod file
internal import statements in .go code inside the v3 module importing other packages in the v3 module
etc.
One way to think about it is that the module's name is now effectively git.example.com/owner/repo/v3, where its name includes the trailing /v3.
If you are a consumer of a vN module and need to update your import paths in your .go files to include the vN, then
github.com/marwan-at-work/mod is a commonly used tool from the community that automates adding the /vN in all the required spots. Separately, it also automates placing the /vN in all the required spots if you are a module author for a v2+ module.
From the "Semantic Import Versioning" section of the Go modules wiki:
If the module is version v2 or higher, the major version of the module must be included as a /vN at the end of the module paths used in go.mod files (e.g., module github.com/my/mod/v2, require github.com/my/mod/v2 v2.0.0) and in the package import path (e.g., import "github.com/my/mod/v2/mypkg").
I may have had a similiar issue where I updated a module to use the /v2 import path but go getting the module always returned an error about invalid go.mod
The solution was to go get -u github.com/<me>/<pkg>/v2

parsing go.mod: unexpected module path "golang.org/x/lint"

I have a package that I've uploaded to Github and am I am go getting the package on another server and keep getting the error:
go: github.com/golang/lint#v0.0.0-20190313153728-d0100b6bd8b3: parsing go.mod: unexpected module path "golang.org/x/lint"
I've deleted go.mod, go.sum and the vendor directory and updated all packages using
go get -u ./...
golang.org/x/lint is not in go.mod or go.sum or vendor directory yet I still get the error when trying to go get the package on my other server.
Note that, May 2021 (two years later) x/lint is fully deprecated. See issue 38968 and CL 318190.
Your error was seen in issue 32412 (related to issue 27900).
And go get -u has been fixed (CL 174099 "make get -u upgrade only modules providing packages")

Cannot determine module path for source directory

I have go.mod file inside root/src/abc. And in root/build-scripts I have a script which does go get. As I am using Go 1.11 I have not used the go path instead the mod file in root/src/abc takes care of other imports except for the packages that are being used in build script which gives error:
go: cannot determine module path for source directory.
Any suggestions?
It's hard to say anything with certainty without seeing the actual commands you run, by it seems your scripts do not change the working directory, and therefore the go commands they execute are not in the module's root folder or any of its subfolders.
Quoting from Command Go: The go.mod file:
A module version is defined by a tree of source files, with a go.mod file in its root. When the go command is run, it looks in the current directory and then successive parent directories to find the go.mod marking the root of the main (current) module.
So your scripts should change the working directory to root/src/abc or any of its subfolders, else the go command will not find the go.mod file.
According to Go documents, go get is deprecated since Go 1.17. However, running command go install may give similar errors. Here is my case. When I ran go install in a directory where there' s a go.mod file, the error was gone. You may try to create a new go.mod and check whether or not the issue is there.
% go install
go: cannot find main module, but found .git/config in /Users/xxxx/Documents/learn-terraform-lambda-api-gateway
to create a module there, run:
cd .. && go mod init
% go mod init
go: cannot determine module path for source directory /Users/xxxx/Documents/learn-terraform-lambda-api-gateway/hello-world
(outside GOPATH, module path must be specified)
Example usage:
'go mod init example.com/m' to initialize a v0 or v1 module
'go mod init example.com/m/v2' to initialize a v2 module
Run 'go help mod init' for more information.
% go mod init example.com/m
go: creating new go.mod: module example.com/m
go: to add module requirements and sums:
go mod tidy
% go mod tidy
go: finding module for package github.com/aws/aws-lambda-go/lambda
go: found github.com/aws/aws-lambda-go/lambda in github.com/aws/aws- lambda-go v1.32.0
go: downloading github.com/stretchr/testify v1.6.1
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
% go install

Resources