]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: Return MultiplePackageError on importing a dir containing multiple packages
authorJens Frederich <jfrederich@gmail.com>
Wed, 15 Oct 2014 03:24:58 +0000 (23:24 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 15 Oct 2014 03:24:58 +0000 (23:24 -0400)
When the Import function in go/build encounters a directory
without any buildable Go source files, it returns a handy
NoGoError. Now if, instead it encounters multiple Go source files
from multiple packages, it returns a handy MultiplePackageError.

A new test for NoGoError and MultiplePackageError is also provided.

Fixes #8286.

LGTM=adg, rsc
R=bradfitz, rsc, adg
CC=golang-codereviews
https://golang.org/cl/155050043

src/go/build/build.go
src/go/build/build_test.go
src/go/build/testdata/empty/dummy [new file with mode: 0644]
src/go/build/testdata/multi/file.go [new file with mode: 0644]
src/go/build/testdata/multi/file_appengine.go [new file with mode: 0644]

index 3ac79808330803461731905fdee4ecfcec49fe71..7a51cf3c0607617fb746ae0c3315da5ba128c5d4 100644 (file)
@@ -417,6 +417,19 @@ func (e *NoGoError) Error() string {
        return "no buildable Go source files in " + e.Dir
 }
 
+// MultiplePackageError describes a directory containing
+// multiple buildable Go source files for multiple packages.
+type MultiplePackageError struct {
+       Dir      string   // directory containing files
+       Packages []string // package names found
+       Files    []string // corresponding files: Files[i] declares package Packages[i]
+}
+
+func (e *MultiplePackageError) Error() string {
+       // Error string limited to two entries for compatibility.
+       return fmt.Sprintf("found packages %s (%s) and %s (%s) in %s", e.Packages[0], e.Files[0], e.Packages[1], e.Files[1], e.Dir)
+}
+
 func nameExt(name string) string {
        i := strings.LastIndex(name, ".")
        if i < 0 {
@@ -675,7 +688,7 @@ Found:
                        p.Name = pkg
                        firstFile = name
                } else if pkg != p.Name {
-                       return p, fmt.Errorf("found packages %s (%s) and %s (%s) in %s", p.Name, firstFile, pkg, name, p.Dir)
+                       return p, &MultiplePackageError{p.Dir, []string{firstFile, name}, []string{p.Name, pkg}}
                }
                if pf.Doc != nil && p.Doc == "" {
                        p.Doc = doc.Synopsis(pf.Doc.Text())
index 23ce89b4bdf28379eb4903c6fd6ad422efb7f63f..43d09cbd145164676d146af0d215125a1ec32d33 100644 (file)
@@ -85,6 +85,20 @@ func TestEmptyImport(t *testing.T) {
        }
 }
 
+func TestEmptyFolderImport(t *testing.T) {
+       _, err := Import(".", "testdata/empty", 0)
+       if _, ok := err.(*NoGoError); !ok {
+               t.Fatal(`Import("testdata/empty") did not return NoGoError.`)
+       }
+}
+
+func TestMultiplePackageImport(t *testing.T) {
+       _, err := Import(".", "testdata/multi", 0)
+       if _, ok := err.(*MultiplePackageError); !ok {
+               t.Fatal(`Import("testdata/multi") did not return MultiplePackageError.`)
+       }
+}
+
 func TestLocalDirectory(t *testing.T) {
        cwd, err := os.Getwd()
        if err != nil {
diff --git a/src/go/build/testdata/empty/dummy b/src/go/build/testdata/empty/dummy
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/go/build/testdata/multi/file.go b/src/go/build/testdata/multi/file.go
new file mode 100644 (file)
index 0000000..ee946eb
--- /dev/null
@@ -0,0 +1,5 @@
+// Test data - not compiled.
+
+package main
+
+func main() {}
diff --git a/src/go/build/testdata/multi/file_appengine.go b/src/go/build/testdata/multi/file_appengine.go
new file mode 100644 (file)
index 0000000..4ea31e7
--- /dev/null
@@ -0,0 +1,5 @@
+// Test data - not compiled.
+
+package test_package
+
+func init() {}