Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Handle overlapping GOPATHs properly in NewContext #379

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
package dep

import (
"go/build"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"fmt"
"unicode"

"github.com/golang/dep/test"
Expand All @@ -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()
Expand Down