15

In Go 1.17 go.mod has two sections, direct dependencies and indirect dependencies, however, there is no indication how indirect dependencies are related to direct dependencies.

How can I find out for a particuar indirect dependency what module or modules use it?

blackgreen
  • 34,072
  • 23
  • 111
  • 129
dimus
  • 8,712
  • 10
  • 45
  • 56

1 Answers1

29

go mod why -m $MODULE will give you one (arbitrarily-chosen) chain of imports from a package in your module to a package in $MODULE. However, it does not natively report all such paths.

go list -json all does expose enough information to identify those paths, but it does not provide an easy way to present import chains for human consumption. However, some third-party tools (such as goda and gomod) can transform or query the output from go list with more structure. (See their documentation for query syntax and examples.)

bcmills
  • 4,391
  • 24
  • 34
  • 1
    Is `go mod graph` an acceptable alternative? – blackgreen Aug 25 '21 at 19:22
  • Not really, no. A module dependency is only added to the `// indirect` section if some package is imported from it, but `go mod graph` doesn't examine the package import graph at all — it only reports the dependencies between modules, not the packages within those modules. – bcmills Aug 25 '21 at 19:27
  • In other words: `go mod graph` will give you a conservative approximation. (It will report some module-to-module edges that result from packages that aren't actually relevant to your program.) – bcmills Aug 25 '21 at 19:28
  • 2
    Relevant, but perhaps not as useful to the OP's intent in asking the question. They're asking “for a particuar indirect dependency what module or modules use it”, but for the `indirect` modules in the `go.mod` file the only uses that really matter are packages transitively imported by the main module. It really depends on which question they're trying to answer: `go mod graph` answers “why this particular version?”, while the package-import graph answers “why this module at all?”. – bcmills Aug 25 '21 at 19:36