]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: diagnose non-canonical import paths before compilation
authorRuss Cox <rsc@golang.org>
Fri, 21 Oct 2016 20:04:41 +0000 (16:04 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 26 Oct 2016 13:54:45 +0000 (13:54 +0000)
If we leave it for compilation sometimes the error appears first
in derived vendor paths, without any indication where they came from.
This is better.

$ go1.7 build canonical/d
cmd/go/testdata/src/canonical/a/a.go:3: non-canonical import path "canonical/a//vendor/c" (should be "canonical/a/vendor/c")
cmd/go/testdata/src/canonical/a/a.go:3: can't find import: "canonical/a//vendor/c"

$ go build canonical/d
package canonical/d
imports canonical/b
imports canonical/a/: non-canonical import path: "canonical/a/" should be "canonical/a"
$

Fixes #16954.

Change-Id: I315ccec92a00d98a08c139b3dc4e17dbc640edd0
Reviewed-on: https://go-review.googlesource.com/31668
Reviewed-by: Quentin Smith <quentin@golang.org>
src/cmd/go/go_test.go
src/cmd/go/pkg.go
src/cmd/go/testdata/src/canonical/a/a.go [new file with mode: 0644]
src/cmd/go/testdata/src/canonical/a/vendor/c/c.go [new file with mode: 0644]
src/cmd/go/testdata/src/canonical/b/b.go [new file with mode: 0644]
src/cmd/go/testdata/src/canonical/d/d.go [new file with mode: 0644]

index 40eb38f7143da65b510d4b8e0b8a3bba248b0c3f..26b2dce0a601a35c6153a0cd107b350fc1a50873 100644 (file)
@@ -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()
index 2f5e90faf42a770a49e44ae1e8c5907aa1ff5968..a779f864eece96b3ec35e82d3aaf0371684c47d3 100644 (file)
@@ -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 (file)
index 0000000..486cc48
--- /dev/null
@@ -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 (file)
index 0000000..7f96c22
--- /dev/null
@@ -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 (file)
index 0000000..ce0f4ce
--- /dev/null
@@ -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 (file)
index 0000000..ef7dd7d
--- /dev/null
@@ -0,0 +1,3 @@
+package d
+
+import _ "canonical/b"