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

Closed
Warashi opened this issue Aug 27, 2016 · 4 comments
Closed

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

Warashi opened this issue Aug 27, 2016 · 4 comments

Comments

@Warashi
Copy link
Contributor

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)?
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/sawada/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.7/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.7/libexec/pkg/tool/darwin_amd64"
CC="clang"
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"
CXX="clang++"
CGO_ENABLED="1"
  1. What did you do?
package main

import (
    "fmt"
    "go/build"
    "os"
)

func main() {
    gopath := os.Getenv("GOPATH")
    _, err := build.Import("bar", gopath+"/src/foo", build.FindOnly)
    if err != nil {
        fmt.Println(err)
    }
}
$ 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?
    nothing
  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)
@quentinmit
Copy link
Contributor

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?

@Warashi
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?

@quentinmit
Copy link
Contributor

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

https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo/edit

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
@quentinmit
Copy link
Contributor

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.
Projects
None yet
Development

No branches or pull requests

3 participants