Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify dependency chain using Go Modules

Tags:

go

go-modules

How can I identify the import path of a module that exists in go.sum but not in go.mod? I'd like to know which module in go.mod is importing the module listed in go.sum, and the entire chain between them.

I'm in the process of removing a deprecated module, logrus, from one of my modules and my own dependencies and want to ensure none of my own code still uses it, and which other code does use it.

The Go module has both a go.mod and a go.sum file. In the go.sum file, a module github.com/sirupsen/logrus appears that does not appear in the go.mod file.

When I recreate the go.sum file by deleting go.sum and running go test -v ./..., the go.sum file is recreated with logrus.

There is no direct or indirect mention in go.mod, such as:

github.com/sirupsen/logrus v1.6.0 // indirect

go mod why returns the following:

$ go mod why github.com/sirupsen/logrus
# github.com/sirupsen/logrus
(main module does not need package github.com/sirupsen/logrus)

go mod why -m returns the following:

$ go mod why -m github.com/sirupsen/logrus
# github.com/sirupsen/logrus
(main module does not need module github.com/sirupsen/logrus)

How can I find out what module in go.mod is importing a module, logrus, which is listed in go.sum but not go.mod?

Here's the module:

  • Module: https://github.com/grokify/goauth
  • go.mod
  • go.sum
like image 547
Grokify Avatar asked Jan 12 '21 12:01

Grokify


People also ask

How do I check Go dependencies?

You can check to see if there are newer versions of dependencies you're already using in your current module. Use the go list command to display a list of your module's dependencies, along with the latest version available for that module.

Where are Go dependencies installed?

As we have learned in Go installation tutorial, standard Go packages like located inside GOROOT directory (where Go is installed). Other project-level dependencies are stored inside GOPATH.

Is Gopath required with Go modules?

As mentioned above, Go runtime still does use $GOPATH as a download directory of Go packages. To make the Google's saying correct, Go module does not entirely replace GOPATH , but replaces GOPATH for version control and package distribution.

What is the difference between a module dependency and package import graph?

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. In other words: go mod graph will give you a conservative approximation.

What is a module in go?

A module is a collection of Go packages stored in a file tree with a go.mod file at its root. The go.mod file defines the module’s module path , which is also the import path used for the root directory, and its dependency requirements , which are the other modules needed for a successful build.

How do I view dependencies in Goland?

As projects become more complex, one way to keep track of this complexity is to visualize the dependencies used. In GoLand you can do this by using the Show Diagram feature, Ctrl+Alt+Shift+U on Windows/Linux or Cmd+Opt+Shift+U on macOS.

How to change the version of a dependency in Golang?

In Golang dependency management, there are several ways of changing the version of a dependency. To start with, if you know the version of a dependency you want, you can simply navigate to the go.mod file in your project and change to your desired dependency version by hand.


Video Answer


1 Answers

go mod why github.com/sirupsen/logrus
# or 
go mod graph | grep logrus
like image 145
Oleg Butuzov Avatar answered Oct 28 '22 18:10

Oleg Butuzov