1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:04:40 -07:00

cmd/go: referee another vendor vs symlink fight

Avoid crash in the specific case reported in #15201 but also
print more useful error message, avoiding slice panic.

Fixes #15201.
Fixes #16167.
Fixes #16566.

Change-Id: I66499621e9678a05bc9b12b0da77906cd7027bdd
Reviewed-on: https://go-review.googlesource.com/31665
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Quentin Smith <quentin@golang.org>
This commit is contained in:
Russ Cox 2016-10-21 15:01:03 -04:00
parent 0eaa8fe03f
commit a1813fcb83
2 changed files with 37 additions and 3 deletions

View File

@ -1759,6 +1759,27 @@ func TestSymlinksVendor(t *testing.T) {
tg.run("install") tg.run("install")
} }
// Issue 15201.
func TestSymlinksVendor15201(t *testing.T) {
switch runtime.GOOS {
case "plan9", "windows":
t.Skipf("skipping symlink test on %s", runtime.GOOS)
}
tg := testgo(t)
defer tg.cleanup()
tg.tempDir("gopath/src/x/y/_vendor/src/x")
tg.must(os.Symlink("../../..", tg.path("gopath/src/x/y/_vendor/src/x/y")))
tg.tempFile("gopath/src/x/y/w/w.go", "package w\nimport \"x/y/z\"\n")
tg.must(os.Symlink("../_vendor/src", tg.path("gopath/src/x/y/w/vendor")))
tg.tempFile("gopath/src/x/y/z/z.go", "package z\n")
tg.setenv("GOPATH", tg.path("gopath/src/x/y/_vendor")+string(filepath.ListSeparator)+tg.path("gopath"))
tg.cd(tg.path("gopath/src"))
tg.run("list", "./...")
}
func TestSymlinksInternal(t *testing.T) { func TestSymlinksInternal(t *testing.T) {
switch runtime.GOOS { switch runtime.GOOS {
case "plan9", "windows": case "plan9", "windows":

View File

@ -415,13 +415,26 @@ func vendoredImportPath(parent *Package, path string) (found string) {
dir := filepath.Clean(parent.Dir) dir := filepath.Clean(parent.Dir)
root := filepath.Join(parent.Root, "src") root := filepath.Join(parent.Root, "src")
if !hasFilePathPrefix(dir, root) { if !hasFilePathPrefix(dir, root) || parent.ImportPath != "command-line-arguments" && filepath.Join(root, parent.ImportPath) != dir {
// Look for symlinks before reporting error. // Look for symlinks before reporting error.
dir = expandPath(dir) dir = expandPath(dir)
root = expandPath(root) root = expandPath(root)
} }
if !hasFilePathPrefix(dir, root) || len(dir) <= len(root) || dir[len(root)] != filepath.Separator {
fatalf("invalid vendoredImportPath: dir=%q root=%q separator=%q", dir, root, string(filepath.Separator)) if !hasFilePathPrefix(dir, root) || len(dir) <= len(root) || dir[len(root)] != filepath.Separator || parent.ImportPath != "command-line-arguments" && filepath.Join(root, parent.ImportPath) != dir {
fatalf("unexpected directory layout:\n"+
" import path: %s\n"+
" root: %s\n"+
" dir: %s\n"+
" expand root: %s\n"+
" expand dir: %s\n"+
" separator: %s",
parent.ImportPath,
filepath.Join(parent.Root, "src"),
filepath.Clean(parent.Dir),
root,
dir,
string(filepath.Separator))
} }
vpath := "vendor/" + path vpath := "vendor/" + path