build.Import doesn't find some vendored packages

41 views
Skip to first unread message

Travis Beauvais

unread,
May 1, 2016, 6:23:04 PM5/1/16
to golang-nuts
I'm working with build.Import to discover package dependencies and am having an issue where it find some packages in the vendor folder but not others. Specifically, golang/x packages. I have both oauth2 and tools in the vendor folder but it only finds oauth2 and instead finds the $GOPATH tools install. Both oauth2 and tools are installed in $GOPATH and vendor folder. tools is the only package that doesn't get found in vendor.

My code is

        wd, _ := os.Getwd() // need to pass in wd as src directory to import otherwise it always find global installs
        pkg, _ := build.Import("golang.org/x/tools", wd, build.FindOnly)
        fmt.Println("ImportPath =", pkg.ImportPath)

        pkg, _ = build.Import("golang.org/x/oauth2", wd, build.FindOnly)
        fmt.Println("ImportPath =", pkg.ImportPath)

Here is my directory structure which also shows both oauth2 and tools installed in the vendor folder and the output of the above code.

        /hom/src/github.com/travis/play • master > ls -la vendor/golang.org/x/
        drwxr-xr-x   4 travis  staff   136 May  1 14:57 .
        drwxr-xr-x   3 travis  staff   102 May  1 13:39 ..
        drwxr-xr-x  33 travis  staff  1122 May  1 13:39 oauth2
        drwxr-xr-x  24 travis  staff   816 May  1 14:56 tools
        /home/src/github.com/travis/play • master > go run vendor.go
        ImportPath = golang.org/x/tools

Am I missing something?

Travis Beauvais

unread,
May 2, 2016, 5:02:51 PM5/2/16
to golang-nuts
I think I've figured out why this is happening but it still kind of weird.

golang/x/tools is technically a package. It's just a folder. If I change the code to try and import golang.org/x/tools/go/vcs it finds the vendored version.

What's weird in my opinion is that it doesn't consider the vendored version a valid package when importing golang.org/x/tool, why does it allow the $GOPATH version? Shouldn't it return an error instead (I know I am swallowing the errors in my sample but I have checked them and none of the calls to build.Import return an error)?

Travis Beauvais

unread,
May 2, 2016, 5:05:51 PM5/2/16
to golang-nuts
Sorry, that should say "golang.org/x/tools is technically NOT a package"
Reply all
Reply to author
Forward
0 new messages