Skip to content

Commit 59b0e14

Browse files
committed
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 <[email protected]>
1 parent e3324a4 commit 59b0e14

File tree

6 files changed

+37
-0
lines changed

6 files changed

+37
-0
lines changed

src/cmd/go/go_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,16 @@ func TestRelativeImportsInCommandLinePackage(t *testing.T) {
12891289
tg.run(append([]string{"test"}, files...)...)
12901290
}
12911291

1292+
func TestNonCanonicalImportPaths(t *testing.T) {
1293+
tg := testgo(t)
1294+
defer tg.cleanup()
1295+
tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
1296+
tg.runFail("build", "canonical/d")
1297+
tg.grepStderr("package canonical/d", "did not report canonical/d")
1298+
tg.grepStderr("imports canonical/b", "did not report canonical/b")
1299+
tg.grepStderr("imports canonical/a/: non-canonical", "did not report canonical/a/")
1300+
}
1301+
12921302
func TestVersionControlErrorMessageIncludesCorrectDirectory(t *testing.T) {
12931303
tg := testgo(t)
12941304
defer tg.cleanup()

src/cmd/go/pkg.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,26 @@ func loadImport(path, srcDir string, parent *Package, stk *importStack, importPo
394394
}
395395
}
396396

397+
if origPath != cleanImport(origPath) {
398+
p.Error = &PackageError{
399+
ImportStack: stk.copy(),
400+
Err: fmt.Sprintf("non-canonical import path: %q should be %q", origPath, pathpkg.Clean(origPath)),
401+
}
402+
p.Incomplete = true
403+
}
404+
397405
return p
398406
}
399407

408+
func cleanImport(path string) string {
409+
orig := path
410+
path = pathpkg.Clean(path)
411+
if strings.HasPrefix(orig, "./") && path != ".." && path != "." && !strings.HasPrefix(path, "../") {
412+
path = "./" + path
413+
}
414+
return path
415+
}
416+
400417
var isDirCache = map[string]bool{}
401418

402419
func isDir(path string) bool {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package a
2+
3+
import _ "c"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package c
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package b
2+
3+
import _ "canonical/a/"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package d
2+
3+
import _ "canonical/b"

0 commit comments

Comments
 (0)