Inspecting: list, graph & why
When a build pulls in a version you didn't expect, or a module you've never heard of, three commands answer the questions: go list -m (what is in the build list?), go mod graph (who requires what?) and go mod why (why is this package needed at all?).
The build list: go list -m all
The build list is the final set of module versions the go command uses: one version per module path. go list -m all prints it, the main module first:
mkdir app && cd app
go mod init example.com/app 2>/dev/null
cat > main.go <<'SRC'
package main
import (
"fmt"
"github.com/fatih/color"
"rsc.io/quote"
)
func main() {
color.NoColor = true
color.Green("%s", quote.Go())
fmt.Println("done")
}
SRC
go mod tidy
go run .
go list -m allgo: finding module for package github.com/fatih/color
go: finding module for package rsc.io/quote
go: downloading github.com/fatih/color v1.19.0
go: downloading rsc.io/quote v1.5.2
go: found github.com/fatih/color in github.com/fatih/color v1.19.0
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: downloading github.com/mattn/go-colorable v0.1.14
go: downloading github.com/mattn/go-isatty v0.0.20
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/sys v0.42.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
Don't communicate by sharing memory, share memory by communicating.
done
example.com/app
github.com/fatih/color v1.19.0
github.com/mattn/go-colorable v0.1.14
github.com/mattn/go-isatty v0.0.20
golang.org/x/sys v0.42.0
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
rsc.io/quote v1.5.2
rsc.io/sampler v1.3.0
Useful variants:
| Command | Shows |
|---|---|
go list -m -u all |
each module with the newest available version in [brackets], plus (retracted) / (deprecated) |
go list -m -versions rsc.io/sampler |
every version the proxy knows (retracted ones hidden; add -retracted) |
go list -m -json rsc.io/quote |
path, version, time, the directory in the cache, the go.mod path, replacements |
go list -m -f '{{.Path}} {{.Dir}}' rsc.io/quote |
any field, through a Go template |
go list -deps ./... |
the packages (not modules) the build compiles |
mkdir app && cd app
go mod init example.com/app 2>/dev/null
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"rsc.io/quote"\n)\n\nfunc main() { fmt.Println(quote.Hello()) }\n' > main.go
go get rsc.io/quote@v1.5.1 2>/dev/null
go list -m -u all
go list -m -versions rsc.io/sampler
go list -m -json rsc.io/quote
go list -deps ./... | grep '^[^/]*\.'example.com/app
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c [v0.43.0]
rsc.io/quote v1.5.1 [v1.5.2]
rsc.io/sampler v1.3.0 [v1.99.99]
rsc.io/sampler v1.3.0 v1.3.1 v1.99.99
{
"Path": "rsc.io/quote",
"Version": "v1.5.1",
"Time": "2018-02-14T00:58:40Z",
"Indirect": true,
"Dir": "/home/learner/go/pkg/mod/rsc.io/quote@v1.5.1",
"GoMod": "/home/learner/go/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.1.mod",
"Sum": "h1:ptSemFtffEBvMed43o25vSUpcTVcqxfXU8Jv0sfFVJs=",
"GoModSum": "h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0="
}
golang.org/x/text/internal/tag
golang.org/x/text/language
rsc.io/sampler
rsc.io/quote
example.com/app
The grep keeps only packages whose first path element contains a dot, which hides the standard library. -deps lists packages in dependency order: everything a package imports comes before it.
Who requires what: go mod graph
go mod graph prints one edge per line, requirer requirement, for the whole requirement graph: every version mentioned in any go.mod, not only the selected ones. The go@ and toolchain@ nodes are the Go version requirements.
mkdir app && cd app
go mod init example.com/app 2>/dev/null
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"github.com/fatih/color"\n)\n\nfunc main() { fmt.Println(color.New(color.Bold) != nil) }\n' > main.go
go mod tidy 2>/dev/null
go mod graph
echo "--- who requires x/sys?"
go mod graph | grep ' golang.org/x/sys@'example.com/app github.com/fatih/color@v1.19.0
example.com/app github.com/mattn/go-colorable@v0.1.14
example.com/app github.com/mattn/go-isatty@v0.0.20
example.com/app go@1.27.3
example.com/app golang.org/x/sys@v0.42.0
github.com/fatih/color@v1.19.0 github.com/mattn/go-colorable@v0.1.14
github.com/fatih/color@v1.19.0 github.com/mattn/go-isatty@v0.0.20
github.com/fatih/color@v1.19.0 golang.org/x/sys@v0.42.0
github.com/fatih/color@v1.19.0 go@1.25.0
github.com/mattn/go-colorable@v0.1.14 github.com/mattn/go-isatty@v0.0.20
github.com/mattn/go-colorable@v0.1.14 golang.org/x/sys@v0.29.0
github.com/mattn/go-isatty@v0.0.20 golang.org/x/sys@v0.6.0
go@1.27.3 toolchain@go1.27.3
golang.org/x/sys@v0.42.0 go@1.25.0
--- who requires x/sys?
example.com/app golang.org/x/sys@v0.42.0
github.com/fatih/color@v1.19.0 golang.org/x/sys@v0.42.0
github.com/mattn/go-colorable@v0.1.14 golang.org/x/sys@v0.29.0
github.com/mattn/go-isatty@v0.0.20 golang.org/x/sys@v0.6.0
Three different requirements on x/sys (v0.6.0, v0.29.0 and v0.42.0) and one selected version: v0.42.0, the highest. That is minimal version selection, visible in the raw graph. For big graphs, pipe go mod graph into a tool: golang.org/x/tools/cmd/digraph answers questions like "all paths from A to B" (go install & the tool Directive).
Why is it here: go mod why
go mod why pkg prints the shortest import chain from your module to a package; -m takes a module instead. Use it before removing or upgrading something you don't recognise:
mkdir app && cd app
go mod init example.com/app 2>/dev/null
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"rsc.io/quote"\n)\n\nfunc main() { fmt.Println(quote.Hello()) }\n' > main.go
go mod tidy 2>/dev/null
go mod why golang.org/x/text/language
go mod why -m rsc.io/sampler
go mod why github.com/google/uuid# golang.org/x/text/language
example.com/app
rsc.io/quote
rsc.io/sampler
golang.org/x/text/language
# rsc.io/sampler
example.com/app
rsc.io/quote
rsc.io/sampler
# github.com/google/uuid
(main module does not need package github.com/google/uuid)
Tip:
go mod whyfollows imports,go mod graphfollows requirements. A module can be in the graph (some dependency'sgo.modrequires it) without any of its packages being imported; thenwhy -msays the main module doesn't need it, and onlygo.sum's/go.modline mentions it.
Test yourself
Which command shows the newest available version of each dependency?
- go mod graph
- go list -m -u all
- go mod why -m all
- go version -m
go mod graph shows golang.org/x/sys@v0.29.0 and golang.org/x/sys@v0.42.0. Which one does the build use?
- Both, linked under different names
- v0.29.0, the lower one
- v0.42.0, the highest version required
- Whichever is first in go.sum
What does go mod why follow?
- Requirements in go.mod files
- Package import chains from the main module
- go.sum entries
- Git history