Search code examples
gogo-modules

How to track down where a dependency comes from in Go modules?


I'd have a Go module, say github.com/myorg/mymodule, of which I'd like to update the version in another module. However, if I try to go get -u it, I get an unexpected module path error:

> go get -u github.com/myorg/mymodule
go: sourcegraph.com/sourcegraph/[email protected]: parsing go.mod: unexpected module path "github.com/sourcegraph/go-diff"
go get: error loading module requirements

The fix for this has been documented at https://github.com/sourcegraph/go-diff/issues/35: this module needs to be imported as github.com/sourcegraph/go-diff/diff, not github.com/sourcegraph/go-diff.

The problem is, I don't know where to apply this fix - that is, which dependency is importing this sub-dependency the wrong way.

In particular, go-diff does not appear in the go.mod, it only appears in go.sum (in several different modules):

> grep go-diff go.mod
> grep go-diff go.sum
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
sourcegraph.com/sourcegraph/go-diff v0.5.0/go.mod h1:kuch7UrkMzY0X+p9CRK03kfuPQ2zzQcaEFbx8wA8rck=

I've tried using go mod why, but this simply reports that the module is not needed:

> go mod why sourcegraph.com/sourcegraph/go-diff
# sourcegraph.com/sourcegraph/go-diff
(main module does not need package sourcegraph.com/sourcegraph/go-diff)

What is even more puzzling is that the error message mentions [email protected], whereas the go.sum contains go-diff v0.5.0.

All in all, how can I track down where this 'mis-import' of the go-diff dependency is happening so that I can update my version of that module?


Solution

  • I was able to 'patch' this problem by running go get github.com/sourcegraph/go-diff and adding this replace directive (cf. https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive) in the module in which I was trying to update the dependency:

    replace sourcegraph.com/sourcegraph/go-diff => github.com/sourcegraph/go-diff v0.5.1
    

    After that, go get -u github.com/myorg/mymodule ran without errors.