I'm trying to use dep
in my project and when I attempt to add a dependency:
dep ensure -add github.com/foo/bar
I get lots of errors similar to:
ensure Solve(): No versions of github.com/foo/bar met constraints:
v1.2.3: Could not introduce github.com/foo/bar@v1.2.3, as its subpackage github.com/foo/bar
does not contain usable Go code (*build.NoGoError).. (Package is required by (root).)
Apparently such problems are generally caused by a relative import within the project in question.
How can I locate the relative imports in a large project, with vendored dependencies? Could any existing Go tools help me here?
My current solution is to crawl through the output of grep -rn '"\./' --include=\*.go .
, but that's slow going.
You can use the go list
command to find all imported packages.
go list -f '{{ join .Imports "
" }}'
Internally, relative imports are converted to an absolute path, and prefixed with _
, so this should show any relative imports in any of your packages.
go list -f '{{ join .Imports "
" }}' ./... | grep '^_'
Relative imports don't work with packages referenced by name, since those must be in the GOPATH anyways. It's likely a package you're not using, or something in _test.go
files meant to be run implicitly from within the package directory. You can check the test imports separately, however while they are expanded, they are not cleaned and prefixed with _
.
go list -f '{{ join .TestImports "
" }}' ./... | grep '\./'