]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: fix lack of error for Import of nonexistent local import path
authorDmitri Shuralyov <shurcooL@gmail.com>
Sat, 12 Nov 2016 04:49:41 +0000 (20:49 -0800)
committerIan Lance Taylor <iant@golang.org>
Sat, 4 Mar 2017 01:14:35 +0000 (01:14 +0000)
When calling build.Import, normally, an error is returned if the
directory doesn't exist. However, that didn't happen for local
import paths when build.FindOnly ImportMode was used.

This change fixes that, and adds tests. It also makes the error
value more consistent in all scenarios where it occurs.

When calling build.Import with a local import path, the package
can only exist in a single deterministic directory. That makes
it possible verify that directory exists earlier in the path,
and return a "cannot find package" error if it doesn't.
Previously, this occurred only when build.FindOnly ImportMode
was not set. It occurred quite late, after getting past Found
label, to line that calls ctxt.readDir. Doing so would return
an error like "no such file or directory" when the directory
does not exist.

Fixes #17863.
Updates #17888 (relevant issue I ran into while working on this CL).

Change-Id: If6a6996ac6176ac203a88bd31419748f88d89d7c
Reviewed-on: https://go-review.googlesource.com/33158
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

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

index 27bd802317bb8655e3049490bc3a29e5521ed5b4..5ced41ce45cecc222ede3a8fdf74e66781fb1461 100644 (file)
@@ -155,6 +155,7 @@ func (ctxt *Context) hasSubdir(root, dir string) (rel string, ok bool) {
        return hasSubdir(rootSym, dirSym)
 }
 
+// hasSubdir reports if dir is within root by performing lexical analysis only.
 func hasSubdir(root, dir string) (rel string, ok bool) {
        const sep = string(filepath.Separator)
        root = filepath.Clean(root)
@@ -528,6 +529,10 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
                if !ctxt.isAbsPath(path) {
                        p.Dir = ctxt.joinPath(srcDir, path)
                }
+               if !ctxt.isDir(p.Dir) {
+                       // package was not found
+                       return p, fmt.Errorf("cannot find package %q in:\n\t%s", path, p.Dir)
+               }
                // Determine canonical import path, if any.
                // Exclude results where the import path would include /testdata/.
                inTestdata := func(sub string) bool {
index a9972416ef2feb264121704b38e43e5c349d8136..9b50efe253415154dd8c171e7d41943737cafe2f 100644 (file)
@@ -300,6 +300,30 @@ func TestShellSafety(t *testing.T) {
        }
 }
 
+// Want to get a "cannot find package" error when directory for package does not exist.
+func TestImportDirNotExist(t *testing.T) {
+       testenv.MustHaveGoBuild(t) // really must just have source
+       ctxt := Default
+       ctxt.GOPATH = ""
+
+       tests := []struct {
+               label        string
+               path, srcDir string
+               mode         ImportMode
+       }{
+               {"Import(full, 0)", "go/build/doesnotexist", "", 0},
+               {"Import(local, 0)", "./doesnotexist", filepath.Join(ctxt.GOROOT, "src/go/build"), 0},
+               {"Import(full, FindOnly)", "go/build/doesnotexist", "", FindOnly},
+               {"Import(local, FindOnly)", "./doesnotexist", filepath.Join(ctxt.GOROOT, "src/go/build"), FindOnly},
+       }
+       for _, test := range tests {
+               _, err := ctxt.Import(test.path, test.srcDir, test.mode)
+               if err == nil || !strings.HasPrefix(err.Error(), "cannot find package") {
+                       t.Errorf(`%s got error: %q, want "cannot find package" error`, test.label, err)
+               }
+       }
+}
+
 func TestImportVendor(t *testing.T) {
        testenv.MustHaveGoBuild(t) // really must just have source
        ctxt := Default