go module @latest found but does not contain package
Asked Answered
S

11

72

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:

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


Spear answered 19/7, 2020 at 0:13 Comment(5)
What command produces the error, go build? or a simple go get github.com/mkideal/cli? (P.S.no need for the @latest - it is implied)Clothespress
Also show what go env reports - maybe a rogue proxy setting?Clothespress
For anyone who want to give it a try -- github.com/mkideal/cli/blob/master/_examples/015-file/main.goSpear
@Spear It works well on my machine. What's your go version and the go.mod?Morrison
See my updated OP @MorrisonSpear
A
19

Cleaning up Golang caches should do the job:

go clean -cache
go clean -modcache

For more info on how this command works, use go help clean. If you have similar problems with your test environment, run go clean -testcache.

Ahlgren answered 21/3, 2023 at 16:53 Comment(1)
It didn't worked for me, I wrote my solution: https://mcmap.net/q/273994/-go-module-latest-found-but-does-not-contain-packageSparks
A
51

Try clearing cache: go clean -modcache

For more info on how this command works, use go help clean

Abisia answered 19/11, 2020 at 6:26 Comment(3)
Hello and welcome to SO. Consider making a comment instead of an answer because this does not give any information asked by the question, but rather a tips or additional things OP can do.Universalism
Go clean worked for me but took too long and removed all downloaded packages. I wish there was another solutionRecidivism
I faced this as well, the solution was to add replace directives after the require directive, to use a version that has your package. ie replace your_packge_name => your_package_name v1.2.3Conjunctive
A
19

Cleaning up Golang caches should do the job:

go clean -cache
go clean -modcache

For more info on how this command works, use go help clean. If you have similar problems with your test environment, run go clean -testcache.

Ahlgren answered 21/3, 2023 at 16:53 Comment(1)
It didn't worked for me, I wrote my solution: https://mcmap.net/q/273994/-go-module-latest-found-but-does-not-contain-packageSparks
C
8

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.

Crescen answered 7/1, 2022 at 11:54 Comment(0)
H
6

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
Herefordshire answered 12/9, 2022 at 6:6 Comment(1)
I ran into the same problem, where the problem was the thing I was try to import had a difference between the go module path (in go.mod) and the actual directory structure in the GitHub repo.Restivo
J
6

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

Jonahjonas answered 17/2, 2023 at 16:3 Comment(0)
S
4
  • 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.

Spear answered 25/7, 2020 at 16:44 Comment(1)
go clean -modcache is key resultKatherinkatherina
A
3

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.

Aniakudo answered 9/12, 2021 at 15:41 Comment(0)
S
3

I faced the same error but with a different scenario and solution.

Scenario: I was getting this error:

go.opentelemetry.io/otel/semconv: module go.opentelemetry.io/otel@latest found (v1.13.0), but does not contain package go.opentelemetry.io/otel/semconv

Reason: It was because go.opentelemetry.io/otel/semconv existed in old version of go.opentelemetry.io/otel but it doesn't now.

Solution: I should've looked for a place which I wrongly Imported go.opentelemetry.io/otel/semconv instead of go.opentelemetry.io/otel/semconv/vX.Y.Z

Replacing the imported package fixes the problem in many cases but my problem was a little bit deeper,

I hadn't imported go.opentelemetry.io/otel/semconv directly but one of my deprecated third-party packages (Otel Jaeger) did, so I removed the third-party package and this fixed my issue.


TL;DR: I recommend people facing this issue reconsider using deprecated packages and also have a look if they've imported the correct packages.

Sparks answered 25/11, 2023 at 12:28 Comment(0)
M
2

I was having this issue with https://pkg.go.dev/github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath because I was trying to it as yamlpath "github.com/vmware-labs/yaml-jsonpath

I was getting errors like

go: finding module for package github.com/vmware-labs/yaml-jsonpath
example.com/template-test/poc imports
        github.com/vmware-labs/yaml-jsonpath: module github.com/vmware-labs/yaml-jsonpath@latest found (v0.3.2), but does not contain package github.com/vmware-labs/yaml-jsonpath
no required module provides package github.com/vmware-labs/yaml-jsonpath; to add it:
        go get github.com/vmware-labs/yaml-jsonpath

@Martin Meli's answer helped me.

I needed to import github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath instead. That resolved the issue.

$ go build
$ #...
$ go get github.com/vmware-labs/yaml-jsonpath/pkg/[email protected]
go: downloading github.com/dprotaso/go-yit v0.0.0-20191028211022-135eb7262960

Modular answered 23/5, 2023 at 12:6 Comment(0)
A
1

If you have a go package inside a go package, then to import the "inside" go package you have to use replace till that "inside" go package.

Ex: Assume this is the folder structure for the package you are importing

dir1/
  go.mod
  go.sum
  main.go
  dir2/
   go.mod
   go.sum
   main.go 

In your file.go you are importing dir2

package file 

import (
    "github.com/dir1/dir2"
)

Then this replace directive in your go.mod file is incorrect

replace (
    github.com/dir1 => ../dir1
)

Instead you must replace till the dir2 path like below

replace (
    github.com/dir1/dir2 => ../dir1/dir2
)
Appalachian answered 24/5, 2024 at 10:47 Comment(0)
D
1

I had the same issue, but none of the above solutions worked. The clue for me was the error output:

module github.com/ACCOUNT/REPO@latest found (v1.0.1), but does not contain package github.com/ACCOUNT/REPO/module

But I hadn't tagged my git repo. The solution was to tag my release higher than reported in the error:

$ git tag v1.0.2
$ git push origin v1.0.2
$ go mod tidy
Durante answered 1/7, 2024 at 21:49 Comment(2)
Thanks, have you tried to tag it the same as what it required (v1.0.1), as tagging a higher version is not feasible in normal cases.Spear
No, I didn't. Often a patch version is a non-breaking change, so I would think in many circumstances it would be fine. Since there were no existing tags in my repo (local or remote), I wanted to make sure my code base had a clean version, so I bumped the patch. For those that can't bump the version, it would be worth trying to tag with the "latest found" tag as you mention.Durante

© 2022 - 2025 — McMap. All rights reserved.