mirror of
https://github.com/golang/go
synced 2024-09-30 11:18:33 -06:00
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/31668 Reviewed-by: Quentin Smith <quentin@golang.org>
This commit is contained in:
parent
e3324a4b66
commit
59b0e14760
@ -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 {
|
||||
|
3
src/cmd/go/testdata/src/canonical/a/a.go
vendored
Normal file
3
src/cmd/go/testdata/src/canonical/a/a.go
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
package a
|
||||
|
||||
import _ "c"
|
1
src/cmd/go/testdata/src/canonical/a/vendor/c/c.go
vendored
Normal file
1
src/cmd/go/testdata/src/canonical/a/vendor/c/c.go
vendored
Normal file
@ -0,0 +1 @@
|
||||
package c
|
3
src/cmd/go/testdata/src/canonical/b/b.go
vendored
Normal file
3
src/cmd/go/testdata/src/canonical/b/b.go
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
package b
|
||||
|
||||
import _ "canonical/a/"
|
3
src/cmd/go/testdata/src/canonical/d/d.go
vendored
Normal file
3
src/cmd/go/testdata/src/canonical/d/d.go
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
package d
|
||||
|
||||
import _ "canonical/b"
|
Loading…
Reference in New Issue
Block a user