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 finds empty directories as packages #16898

Warashi opened this issue Aug 27, 2016 · 4 comments

go/build: build.Import finds empty directories as packages #16898

Warashi opened this issue Aug 27, 2016 · 4 comments


Copy link

Warashi commented Aug 27, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
go version go1.7 darwin/amd64
  1. What operating system and processor architecture are you using (go env)?
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/49/rrmkxj1n1db593lnjqln1__h0000gn/T/go-build349947113=/tmp/go-build -gno-record-gcc-switches -fno-common"
  1. What did you do?
package main

import (

func main() {
    gopath := os.Getenv("GOPATH")
    _, err := build.Import("bar", gopath+"/src/foo", build.FindOnly)
    if err != nil {
$ mkdir -p $GOPATH/src/foo
$ cd $GOPATH/src/foo
$ mkdir -p vendor/bar
$ cat ABOVE_CODE > main.go
$ go run main.go
  1. What did you expect to see?
  2. What did you see instead?
cannot find package "bar" in any of:
    /Users/sawada/go/src/foo/vendor/bar (vendor tree)
    /usr/local/Cellar/go/1.7/libexec/src/bar (from $GOROOT)
    /Users/sawada/go/src/bar (from $GOPATH)
Copy link

An empty directory is not a package. Quoth ref/spec: "A package in turn is constructed from one or more source files"

I'm going to close this as WAI. Was there a particular reason you expected it to find the empty bar directory?

Copy link
Contributor Author

Warashi commented Sep 7, 2016

But when bar directory is in $GOPATH/src, build.Import finds bar and returned err is nil.
So there is difference between $GOPATH/src and vendor.
Isn't this a problem?

Copy link

That does seem odd, I agree. The behavior seems to match the documentation, though. This is also covered in the vendoring spec:

If there is a source directory d/vendor, then, when compiling a source file within the subtree rooted at d, import "p" is interpreted as import "d/vendor/p" if that path names a directory containing at least one file with a name ending in “.go”.

So if anything, the bug is that build.Import /does/ find the non-vendored directory.

/cc @rsc Is this WAI?

@quentinmit quentinmit reopened this Sep 7, 2016
@quentinmit quentinmit changed the title go/build: build.Import don't find vendored empty directory go/build: build.Import finds empty directories as packages Sep 7, 2016
Copy link

I spoke to @rsc about this and it is WAI. build.Import finds empty directories because it might be used to figure out where a package "should" live (e.g. so that you can then place files in that directory). Whereas it does not find empty directories in vendor/ because they would shadow non-vendored non-empty directories. For example, if you vendor net/http/httptest, you will have an "empty" vendor/net/http directory (save the httptest subdirectory), and you do not want that empty vendor/net/http to replace the standard library's net/http.

Again, is there a particular place where you feel this logic breaks something?

@golang golang locked and limited conversation to collaborators Sep 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

No branches or pull requests

3 participants