Golang list indirect dependencies for a dependency in go.mod file

424 views Asked by At

Is there a way we can list down the indirect dependencies of a dependency in go lang go.mod file.

For example in go.mod:

require (
    github.com/blang/semver/v4 v4.0.0
    github.com/fatih/structtag v1.1.0
    github.com/go-logr/logr v1.2.3
    github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc
)


require (
    github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
    github.com/BurntSushi/toml v1.2.0 // indirect
    github.com/MakeNowJust/heredoc v1.0.0 // indirect
    github.com/Masterminds/goutils v1.1.1 // indirect
    github.com/Masterminds/semver/v3 v3.1.1 // indirect
    github.com/Masterminds/sprig/v3 v3.2.2 // indirect
    github.com/Masterminds/squirrel v1.5.3 // indirect
    github.com/Microsoft/go-winio v0.6.0 // indirect
    github.com/Microsoft/hcsshim v0.9.4 // indirect
    github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d // indirect
    github.com/adrg/xdg v0.4.0 // indirect
)

Say This dependency github.com/blang/semver/v4 needs the following list of indirect depencies:

    github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
    github.com/BurntSushi/toml v1.2.0 // indirect
    github.com/MakeNowJust/heredoc v1.0.0 // indirect
    github.com/Masterminds/goutils v1.1.1 // indirect

Is there a way I can figure out list of indirect dependencies for dependent package in golang?

I tried with following commands:

go list -m all

Above command returns list of all direct and indirect depencies for the project.

I tried using below command but it didn't worked:

 go list -m github.com/blang/semver/v4
1

There are 1 answers

0
Srijan Rastogi On

The above use-case can be fulfilled with the command go mod graph. You can also use go mod why <indirect-dependency> to know which direct dependencies are using it.