Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/build: build.Import doesn't import package if srcDir is provided #29952

Closed
tobiaszheller opened this issue Jan 27, 2019 · 2 comments
Closed

Comments

@tobiaszheller
Copy link

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

go version go1.11.5 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/theller/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/theller/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11.5/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/jd/6ps0d4r53hxcbfl_g94kcvhc0000gn/T/go-build812346700=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I was trying to import package by using ( *Context) Import method from go/build package with package name as path and parent directory path to package as srcDir.
https://play.golang.org/p/HT524Fr32hz

What did you expect to see?

No error after importing package.

What did you see instead?

cannot find package "httptest" in any of:
	/usr/local/go/src/httptest (from $GOROOT)
	($GOPATH not set. For more details see: 'go help gopath')

The same applies to packages from GOPATH.

Current implementation:

dir := ctxt.joinPath(ctxt.GOROOT, "src", path)

I think if srcDir is provided it should be used instead of "src" constant in Import method.

src := "src"
if srcDir != "" {
	if sub, ok := ctxt.hasSubdir(ctxt.GOROOT, srcDir); ok {
		src = sub
	}
}
dir := ctxt.joinPath(ctxt.GOROOT, src, path)
@mvdan
Copy link
Member

mvdan commented Jan 27, 2019

Have you tried using https://godoc.org/golang.org/x/tools/go/packages? For most use cases, go/build is deprecated in its favor. And you'll also get Go module support.

@tobiaszheller
Copy link
Author

Then I will close it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants