diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index 40eb38f714..26b2dce0a6 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -1289,6 +1289,16 @@ func TestRelativeImportsInCommandLinePackage(t *testing.T) { tg.run(append([]string{"test"}, files...)...) } +func TestNonCanonicalImportPaths(t *testing.T) { + tg := testgo(t) + defer tg.cleanup() + tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata")) + tg.runFail("build", "canonical/d") + tg.grepStderr("package canonical/d", "did not report canonical/d") + tg.grepStderr("imports canonical/b", "did not report canonical/b") + tg.grepStderr("imports canonical/a/: non-canonical", "did not report canonical/a/") +} + func TestVersionControlErrorMessageIncludesCorrectDirectory(t *testing.T) { tg := testgo(t) defer tg.cleanup() diff --git a/src/cmd/go/pkg.go b/src/cmd/go/pkg.go index 2f5e90faf4..a779f864ee 100644 --- a/src/cmd/go/pkg.go +++ b/src/cmd/go/pkg.go @@ -394,9 +394,26 @@ func loadImport(path, srcDir string, parent *Package, stk *importStack, importPo } } + if origPath != cleanImport(origPath) { + p.Error = &PackageError{ + ImportStack: stk.copy(), + Err: fmt.Sprintf("non-canonical import path: %q should be %q", origPath, pathpkg.Clean(origPath)), + } + p.Incomplete = true + } + return p } +func cleanImport(path string) string { + orig := path + path = pathpkg.Clean(path) + if strings.HasPrefix(orig, "./") && path != ".." && path != "." && !strings.HasPrefix(path, "../") { + path = "./" + path + } + return path +} + var isDirCache = map[string]bool{} func isDir(path string) bool { diff --git a/src/cmd/go/testdata/src/canonical/a/a.go b/src/cmd/go/testdata/src/canonical/a/a.go new file mode 100644 index 0000000000..486cc4843f --- /dev/null +++ b/src/cmd/go/testdata/src/canonical/a/a.go @@ -0,0 +1,3 @@ +package a + +import _ "c" diff --git a/src/cmd/go/testdata/src/canonical/a/vendor/c/c.go b/src/cmd/go/testdata/src/canonical/a/vendor/c/c.go new file mode 100644 index 0000000000..7f96c221c2 --- /dev/null +++ b/src/cmd/go/testdata/src/canonical/a/vendor/c/c.go @@ -0,0 +1 @@ +package c diff --git a/src/cmd/go/testdata/src/canonical/b/b.go b/src/cmd/go/testdata/src/canonical/b/b.go new file mode 100644 index 0000000000..ce0f4ce303 --- /dev/null +++ b/src/cmd/go/testdata/src/canonical/b/b.go @@ -0,0 +1,3 @@ +package b + +import _ "canonical/a/" diff --git a/src/cmd/go/testdata/src/canonical/d/d.go b/src/cmd/go/testdata/src/canonical/d/d.go new file mode 100644 index 0000000000..ef7dd7dd46 --- /dev/null +++ b/src/cmd/go/testdata/src/canonical/d/d.go @@ -0,0 +1,3 @@ +package d + +import _ "canonical/b"