go: x/tools/go/packages: vendor is not searched in GOPATH mode

What version of Go are you using (go version)?

$ go version
go version go1.11.5 linux/amd64

Does this issue reproduce with the latest release?

Reproducable on 1.12-rc1

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/vektah/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/vektah/go"
GOPROXY=""
GORACE=""
GOROOT="/home/vektah/local/go"
GOTMPDIR=""
GOTOOLDIR="/home/vektah/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build863238177=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Trying to load a package using x/tools/go/packages doesn’t seem to search vendor directories.

For your convenience this code can be fetched from https://github.com/vektah/packages-vendor-test

packages_test.go

package main_test

import (
	"github.com/external/tool"
	"golang.org/x/tools/go/packages"
	"testing"
)

func TestPackageLoading(t *testing.T) {
	// we can reference the vendored package just fine
	if tool.Foo() != true {
		t.Error("vendor imports are broken?")
	}

	// but if we try to load the package via packages
	pkgs, err := packages.Load(nil, "github.com/external/tool")
	if err != nil {
		t.Error(err.Error())
	}

	// we get an error!
	if pkgs[0].Errors != nil {
		t.Error(pkgs[0].Errors)
	}

	// and a blank package name
	if pkgs[0].Name != "tool" {
		t.Error("the package name should be set")
	}
}

vendor/github.com/external/tool/tool.go

package tool

func Foo() bool {
	return true
}

What did you expect to see?

PASS
ok      github.com/vektah/packages-vendor-test  0.001s

What did you see instead?

--- FAIL: TestPackageLoading (0.01s)
    packages_test.go:24: [-: cannot find package "github.com/external/tool" in any of:
                /home/vektah/local/go/src/github.com/external/tool (from $GOROOT)
                /home/vektah/go/src/github.com/external/tool (from $GOPATH)]
    packages_test.go:29: the package name should be set
FAIL
exit status 1
FAIL    github.com/vektah/packages-vendor-test  0.008s

This is hurting go modules adoption in http://github.com/99designs/gqlgen, Ideally I should be able to switch to packages to get modules support without losing support for vendor.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 15 (9 by maintainers)

Commits related to this issue

Most upvoted comments

I presume you’re running the tool with GO111MODULE=on?

Also, see https://golang.org/cmd/go/#hdr-Maintaining_module_requirements; go build will not use vendor/ by default when ran in module mode, unlesss -mod=vendor, is given.

Also see https://github.com/golang/go/issues/30240, which I believe would make this better and automatic in 1.13.