aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2018-08-03 10:38:22 -0400
committerBryan C. Mills <bcmills@google.com>2018-08-03 15:37:24 +0000
commitac6d1564795e662b5b930c6b3d86f12351ff83d5 (patch)
tree1e32250f703ae29c6153e21d59766224a2ca142e
parent490035b0b9dd27e8868ccc5dced45a29e9b21227 (diff)
downloadgo-ac6d1564795e662b5b930c6b3d86f12351ff83d5.tar.gz
go-ac6d1564795e662b5b930c6b3d86f12351ff83d5.zip
cmd/go/internal/load: trim the " (test)" suffix from ImportStack paths
We were passing untrimmed paths to ModPackageModuleInfo, which was then failing the build because it was asked to resolve an invalid path. Fixes #26722 Change-Id: I043cc9c26f2188c5e005c0353620d9c55b339df9 Reviewed-on: https://go-review.googlesource.com/127795 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r--src/cmd/go/internal/load/pkg.go7
-rw-r--r--src/cmd/go/testdata/script/mod_test.txt18
2 files changed, 22 insertions, 3 deletions
diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go
index 1b21b632b8..f27fdc1767 100644
--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -318,7 +318,8 @@ func (p *PackageError) Error() string {
return "package " + strings.Join(p.ImportStack, "\n\timports ") + ": " + p.Err
}
-// An ImportStack is a stack of import paths.
+// An ImportStack is a stack of import paths, possibly with the suffix " (test)" appended.
+// TODO(bcmills): When the tree opens for 1.12, replace the suffixed string with a struct.
type ImportStack []string
func (s *ImportStack) Push(p string) {
@@ -986,7 +987,7 @@ func disallowInternal(srcDir string, p *Package, stk *ImportStack) *Package {
// p is in a module, so make it available based on the import path instead
// of the file path (https://golang.org/issue/23970).
parent := p.ImportPath[:i]
- importer := (*stk)[len(*stk)-2]
+ importer := strings.TrimSuffix((*stk)[len(*stk)-2], " (test)")
if str.HasPathPrefix(importer, parent) {
return p
}
@@ -1039,7 +1040,7 @@ func disallowVendor(srcDir, path string, p *Package, stk *ImportStack) *Package
// but the usual vendor visibility check will not catch them
// because the module loader presents them with an ImportPath starting
// with "golang_org/" instead of "vendor/".
- importer := (*stk)[len(*stk)-2]
+ importer := strings.TrimSuffix((*stk)[len(*stk)-2], " (test)")
if mod := ModPackageModuleInfo(importer); mod != nil {
dir := p.Dir
if relDir, err := filepath.Rel(p.Root, p.Dir); err == nil {
diff --git a/src/cmd/go/testdata/script/mod_test.txt b/src/cmd/go/testdata/script/mod_test.txt
new file mode 100644
index 0000000000..a1ee8aa256
--- /dev/null
+++ b/src/cmd/go/testdata/script/mod_test.txt
@@ -0,0 +1,18 @@
+env GO111MODULE=on
+
+cd a/
+go test
+stdout PASS
+
+-- a/go.mod --
+module github.com/user/a
+
+-- a/a.go --
+package a
+
+-- a/a_test.go --
+package a
+
+import "testing"
+
+func Test(t *testing.T) {}