]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: ImportDir reject directories that don't exist
authorAndrew Gerrand <adg@golang.org>
Wed, 23 Jan 2013 00:28:32 +0000 (11:28 +1100)
committerAndrew Gerrand <adg@golang.org>
Wed, 23 Jan 2013 00:28:32 +0000 (11:28 +1100)
Fixes #3428.

R=dave, remyoudompheng, rsc
CC=golang-dev
https://golang.org/cl/7129048

src/pkg/go/build/build.go
src/pkg/go/build/build_test.go

index e2a47a556a70bc14cec8c93f2334a449a78a91f5..6c65b3da635d7689f751f6b578ad1f7f3f12a232 100644 (file)
@@ -321,7 +321,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 d8cf98840d71f33ec435ce2f3cf2d13699661294..77f55ad4094d55d957500a3bf2504ec40bf5571c 100644 (file)
@@ -5,6 +5,7 @@
 package build
 
 import (
+       "fmt"
        "os"
        "path/filepath"
        "runtime"
@@ -89,6 +90,16 @@ func TestLocalDirectory(t *testing.T) {
        }
 }
 
+// golang.org/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", dir)
+       if err == nil || err.Error() != want {
+               t.Error("got error %q, want %q", err, want)
+       }
+}
+
 func TestShouldBuild(t *testing.T) {
        const file1 = "// +build tag1\n\n" +
                "package main\n"