diff --git a/context.go b/context.go index ca86495b74..9d49c6f724 100644 --- a/context.go +++ b/context.go @@ -34,11 +34,13 @@ func NewContext() (*Ctx, error) { wd = filepath.FromSlash(wd) ctx := &Ctx{} + gopathFound := false for _, gp := range filepath.SplitList(buildContext.GOPATH) { gp = filepath.FromSlash(gp) - if filepath.HasPrefix(wd, gp) { + if !gopathFound && filepath.HasPrefix(wd, gp) { ctx.GOPATH = gp + gopathFound = true } ctx.GOPATHS = append(ctx.GOPATHS, gp) diff --git a/context_test.go b/context_test.go index 699a5c6804..1473a765ff 100644 --- a/context_test.go +++ b/context_test.go @@ -5,11 +5,13 @@ package dep import ( + "go/build" "os" "path/filepath" "runtime" "strings" "testing" + "fmt" "unicode" "github.com/golang/dep/test" @@ -33,6 +35,26 @@ func TestNewContextNoGOPATH(t *testing.T) { } } +func TestMultipleGopaths(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + + gp1 := "go/foo_new" + gp2 := "go/foo" + + project := filepath.Join(gp1, "src/bar") + h.TempDir(project) + h.TempDir(gp2) + h.Cd(h.Path(project)) + + build.Default.GOPATH = fmt.Sprintf("%s:%s", h.Path(gp1), h.Path(gp2)) + c, err := NewContext() + h.Must(err) + if c.GOPATH != h.Path(gp1) { + t.Fatalf("Incorrect GOPATH detected. Expected '%s', got '%s'", h.Path(gp1), c.GOPATH) + } +} + func TestSplitAbsoluteProjectRoot(t *testing.T) { h := test.NewHelper(t) defer h.Cleanup()