go mod tidy doesn't rewrite go.mod file (shows go: warning "all" matched no packages)

5,395 views
Skip to first unread message

vaastav...@gmail.com

unread,
Mar 31, 2022, 11:12:24 AM3/31/22
to golang-nuts
Hi all,
I am encountering this problem where when I run `go mod tidy`, it simply returns `go: warning: "all" matched no packages`. I get this error when I run `go list all` as well.
I was hoping someone could help me as to why this is happening since I haven't been able to find an explanation for this.

Kind Regards
Vaastav

Brian Candler

unread,
Mar 31, 2022, 12:30:13 PM3/31/22
to golang-nuts
Can you show the contents of the go.mod file in the directory where you run "go mod tidy"?


> I get this error when I run `go list all` as well.

Try "go list" rather than "go list all" (because in that context, "all" is interpreted as the name of a package)

vaastav...@gmail.com

unread,
Mar 31, 2022, 3:18:52 PM3/31/22
to golang-nuts
Here are the contents of the go.mod file

```
module module_name_obfuscated

go 1.18

require (
    github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
    github.com/go-redis/redis/v8 v8.11.4
    github.com/go-sql-driver/mysql v1.6.0
    github.com/otiai10/copy v1.7.0
    github.com/rabbitmq/amqp091-go v1.3.0
    github.com/tracingplane/tracingplane-go v0.0.0-20171025152126-8c4e6f79b148
    gitlab.mpi-sws.org/cld/tracing/tracing-framework-go v0.0.0-20211206181151-6edc754a9f2a
    go.mongodb.org/mongo-driver v1.7.4
    go.opentelemetry.io/otel/exporters/jaeger v1.2.0
    go.opentelemetry.io/otel/exporters/zipkin v1.6.0
    go.opentelemetry.io/otel/sdk v1.6.0
    go.opentelemetry.io/otel/trace v1.6.0
    golang.org/x/mod v0.5.1
)

require (
    github.com/cespare/xxhash/v2 v2.1.2 // indirect
    github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
    github.com/go-logr/logr v1.2.3 // indirect
    github.com/go-logr/stdr v1.2.2 // indirect
    github.com/go-stack/stack v1.8.0 // indirect
    github.com/golang/protobuf v1.5.2 // indirect
    github.com/golang/snappy v0.0.4 // indirect
    github.com/klauspost/compress v1.13.6 // indirect
    github.com/openzipkin/zipkin-go v0.4.0 // indirect
    github.com/pkg/errors v0.9.1 // indirect
    github.com/xdg-go/pbkdf2 v1.0.0 // indirect
    github.com/xdg-go/scram v1.0.2 // indirect
    github.com/xdg-go/stringprep v1.0.2 // indirect
    github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
    go.opentelemetry.io/otel v1.6.0 // indirect
    golang.org/x/crypto v0.0.0-20210920023735-84f357641f63 // indirect
    golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect
    golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
    golang.org/x/text v0.3.7 // indirect
    golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
    google.golang.org/protobuf v1.27.1 // indirect
)
```

But when I run `go mod tidy`, it simply prints out the aforementioned warning and the new go.mod file looks like this:

```
module module_name_obfuscated

go 1.18
```

Brian Candler

unread,
Apr 1, 2022, 5:36:04 AM4/1/22
to golang-nuts
Interesting.  If I put that go.mod file in an empty directory, then I see the warning:

$ go mod tidy

go: warning: "all" matched no packages
$ go mod tidy

go: warning: "all" matched no packages
> But when I run `go mod tidy`, it simply prints out the aforementioned warning and the new go.mod file looks like this

I see the same behaviour if I create a main.go file.  Then the warning vanishes and the go.mod file is tidied:

$ mv main.go.x main.go
$ cat main.go
package main

func main() {
}
$ go mod tidy
$ cat go.mod
module module_name_obfuscated

go 1.17

This of course is expected behaviour: it's the job of go mod tidy to update the go.mod file so that it matches the source code in the module. See "go help mod tidy":

"Tidy makes sure go.mod matches the source code in the module.
It adds any missing modules necessary to build the current module's
packages and dependencies, and it removes unused modules that
don't provide any relevant packages
."


So I presume the warning you see is only when go mod tidy can't find any source code in the current directory.

vaastav...@gmail.com

unread,
Apr 1, 2022, 8:15:14 AM4/1/22
to golang-nuts
I see this behaviour even in the presence of a main.go file in the same directory. This is why I am baffled as to why I am seeing this warning even if the source code is present.
It was working fine until I upgraded from go1.17 to go1.18

vaastav...@gmail.com

unread,
Apr 1, 2022, 8:30:40 AM4/1/22
to golang-nuts
Ok, I just tested this. I only see this behaviour with go 1.18. I tried using go mod tidy with go1.17 and it worked as expected.

Brian Candler

unread,
Apr 1, 2022, 9:43:30 AM4/1/22
to golang-nuts
I just updated to 1.18 (from 1.17.6) - on macOS 12.3.1 - and indeed:
- with just go.mod, I get the warning and go.mod is cleaned up
- with go.mod plus main.go, I get no warning and go.mod is cleaned up

This looks to me like a bug fix.  If there is no source code, then by definition go.mod should not have any dependent modules.

vaastav...@gmail.com

unread,
Apr 1, 2022, 10:05:57 AM4/1/22
to golang-nuts
There is definitely source code in that folder. There is a main.go file as well as 3 other packages. 
So, go.mod should not be removing all the entries from the go.mod file.

Bryan C. Mills

unread,
Apr 6, 2022, 3:17:44 PM4/6/22
to golang-nuts
Do you have a go.work file? If so, does it include the module in that directory?

What do `go env GOMOD` and `go env GOWORK` report?

Reply all
Reply to author
Forward
0 new messages