Commit 59b0e147 authored by Russ Cox's avatar Russ Cox

cmd/go: diagnose non-canonical import paths before compilation

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/31668Reviewed-by: 's avatarQuentin Smith <quentin@golang.org>
parent e3324a4b
......@@ -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()
......
......@@ -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 {
......
package b
import _ "canonical/a/"
package d
import _ "canonical/b"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment