]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/build: reject non-existant directories in ImportDir
authorEmmanuel Odeke <emm.odeke@gmail.com>
Wed, 11 May 2016 04:28:30 +0000 (21:28 -0700)
committerAndrew Gerrand <adg@golang.org>
Thu, 12 May 2016 20:21:29 +0000 (20:21 +0000)
Re-apply @adg's CL https://golang.org/cl/7129048 that was
previously disabled in https://golang.org/cl/7235052 because
it broke `godoc net/http` for go1.1.

Currently `godoc net/http` seems to work fine with this CL.

Fixes #3428.

Change-Id: I7df06df02fd62dededac6ec60bea62561be59cf1
Reviewed-on: https://go-review.googlesource.com/23013
Run-TryBot: Andrew Gerrand <adg@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
src/go/build/build.go
src/go/build/build_test.go

index fa258d3dc671a4d0f8a643fc34fdc08ad78f45c0..0818aa050178b9e7f5feea7486ba0384a878effd 100644 (file)
@@ -403,7 +403,11 @@ func (p *Package) IsCommand() bool {
 // ImportDir is like Import but processes the Go package found in
 // the named directory.
 func (ctxt *Context) ImportDir(dir string, mode ImportMode) (*Package, error) {
-       return ctxt.Import(".", dir, mode)
+       p, err := ctxt.Import(".", dir, mode)
+       if err == nil && !ctxt.isDir(p.Dir) {
+               err = fmt.Errorf("%q is not a directory", p.Dir)
+       }
+       return p, err
 }
 
 // NoGoError is the error used by Import to describe a directory
index c9f906a7da6d0a5b498ac771c03cdd4ea07d51d1..6bade1d318c6e706aa90e80beedf44e955420a88 100644 (file)
@@ -5,6 +5,7 @@
 package build
 
 import (
+       "fmt"
        "internal/testenv"
        "io"
        "os"
@@ -345,3 +346,13 @@ func TestImportVendorParentFailure(t *testing.T) {
                t.Fatalf("error on failed import does not mention GOROOT/src/vendor directory:\n%s", e)
        }
 }
+
+// Issue 3248
+func TestBogusDirectory(t *testing.T) {
+       const dir = "/foo/bar/baz/gopher"
+       _, err := ImportDir(dir, FindOnly)
+       want := fmt.Sprintf("%q is not a directory", filepath.FromSlash(dir))
+       if err == nil || err.Error() != want {
+               t.Errorf("got error %q, want %q", err, want)
+       }
+}