How to avoid indirect dependencies in go.mod file

Viewed 27283

I am running go build to get my go.mod file updated with library I am using "github.com/gocolly/colly v1.2.0" But I see all other dependencies saying "// indirect" at end. How to avoid getting this? Here is my go.mod file

   module prodenv

go 1.13

require (
   github.com/PuerkitoBio/goquery v1.5.1 // indirect
   github.com/antchfx/htmlquery v1.2.2 // indirect
   github.com/antchfx/xmlquery v1.2.3 // indirect
   github.com/antchfx/xpath v1.1.5 // indirect
   github.com/gobwas/glob v0.2.3 // indirect
   github.com/gocolly/colly v1.2.0
   github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
   github.com/kennygrant/sanitize v1.2.4 // indirect
   github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
   github.com/temoto/robotstxt v1.1.1 // indirect
   golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
   google.golang.org/appengine v1.6.5 // indirect
)
2 Answers

Unfortunately, you can't avoid them. Indirect dependency, is basically dependency that wasn't listed in go.mod of your direct dependency, but is still required by it.

In your case it happens, because you use github.com/gocolly/colly v1.2.0 as dependency and v1.2.0 of this package isn't a module, because it doesn't contain go.mod, so all it's dependencies are indirect and listed in your go.mod with indirect tag.

Note, that colly has go.mod in >=v2.0.0, so if you require that version, these dependencies won't be listed as indirect in your go.mod.

go mod tidy helped me remove the indirect from go mod for me.

go mod tidy ensures that the go. mod file matches the source code in the module. It adds any missing module requirements necessary to build the current module's packages and dependencies, if there are some not used dependencies go mod tidy will remove those from go.

Note : Indirect Dependencies required by the application/packages will not be removed or moved to direct dependencies. Those dependencies will still be there as indirect dependencies.

Related