cmd/go/internal/vgo: prefer module path from Godeps/Godeps.json over GOPATH

The entry in Godeps/Godeps.json is explicitly what we're looking for:
the import path of the root directory.

The logic about deriving a module path from the current directory's
location in GOPATH should usually match, but it's less a direct statement:
for example, maybe a fork has been checked out.

If the two disagree, prefer Godeps/Godeps.json's explict statement.

Change-Id: I439684f5d34eeaf41419bbdae82139ba79498f6c
Reviewed-on: https://go-review.googlesource.com/121858
Reviewed-by: Bryan C. Mills <bcmills@google.com>
diff --git a/vendor/cmd/go/internal/modconv/convert.go b/vendor/cmd/go/internal/modconv/convert.go
index 0021478..b8cef90 100644
--- a/vendor/cmd/go/internal/modconv/convert.go
+++ b/vendor/cmd/go/internal/modconv/convert.go
@@ -11,6 +11,7 @@
 	"strings"
 	"sync"
 
+	"cmd/go/internal/base"
 	"cmd/go/internal/modfetch"
 	"cmd/go/internal/modfile"
 	"cmd/go/internal/module"
@@ -57,7 +58,7 @@
 		r := item.(module.Version)
 		repo, info, err := modfetch.ImportRepoRev(r.Path, r.Version)
 		if err != nil {
-			fmt.Fprintf(os.Stderr, "vgo: converting %s: stat %s@%s: %v\n", file, r.Path, r.Version, err)
+			fmt.Fprintf(os.Stderr, "vgo: converting %s: stat %s@%s: %v\n", base.ShortPath(file), r.Path, r.Version, err)
 			return
 		}
 		mu.Lock()
diff --git a/vendor/cmd/go/internal/vgo/init.go b/vendor/cmd/go/internal/vgo/init.go
index e8868bb..53f55d4 100644
--- a/vendor/cmd/go/internal/vgo/init.go
+++ b/vendor/cmd/go/internal/vgo/init.go
@@ -258,7 +258,7 @@
 			if convert == nil {
 				return
 			}
-			fmt.Fprintf(os.Stderr, "vgo: copying requirements from %s\n", cfg)
+			fmt.Fprintf(os.Stderr, "vgo: copying requirements from %s\n", base.ShortPath(cfg))
 			cfg = filepath.ToSlash(cfg)
 			if err := modconv.ConvertLegacyConfig(modFile, cfg, data); err != nil {
 				base.Fatalf("vgo: %v", err)
@@ -337,12 +337,6 @@
 		// Running go mod -init -module=x/y/z; return x/y/z.
 		return CmdModModule, nil
 	}
-	for _, gpdir := range filepath.SplitList(cfg.BuildContext.GOPATH) {
-		src := filepath.Join(gpdir, "src") + string(filepath.Separator)
-		if strings.HasPrefix(dir, src) {
-			return filepath.ToSlash(dir[len(src):]), nil
-		}
-	}
 
 	// Cast about for import comments,
 	// first in top-level directory, then in subdirectories.
@@ -369,10 +363,10 @@
 
 	// Look for Godeps.json declaring import path.
 	data, _ := ioutil.ReadFile(filepath.Join(dir, "Godeps/Godeps.json"))
-	var cfg struct{ ImportPath string }
-	json.Unmarshal(data, &cfg)
-	if cfg.ImportPath != "" {
-		return cfg.ImportPath, nil
+	var cfg1 struct{ ImportPath string }
+	json.Unmarshal(data, &cfg1)
+	if cfg1.ImportPath != "" {
+		return cfg1.ImportPath, nil
 	}
 
 	// Look for vendor.json declaring import path.
@@ -383,6 +377,14 @@
 		return cfg2.RootPath, nil
 	}
 
+	// Look for path in GOPATH.
+	for _, gpdir := range filepath.SplitList(cfg.BuildContext.GOPATH) {
+		src := filepath.Join(gpdir, "src") + string(filepath.Separator)
+		if strings.HasPrefix(dir, src) {
+			return filepath.ToSlash(dir[len(src):]), nil
+		}
+	}
+
 	// Look for .git/config with github origin as last resort.
 	data, _ = ioutil.ReadFile(filepath.Join(dir, ".git/config"))
 	if m := gitOriginRE.FindSubmatch(data); m != nil {
diff --git a/vendor/cmd/go/vgo_test.go b/vendor/cmd/go/vgo_test.go
index 218641c..08d4e5e 100644
--- a/vendor/cmd/go/vgo_test.go
+++ b/vendor/cmd/go/vgo_test.go
@@ -16,6 +16,7 @@
 	"strings"
 	"testing"
 
+	"cmd/go/internal/cfg"
 	"cmd/go/internal/modconv"
 	"cmd/go/internal/vgo"
 )
@@ -76,11 +77,28 @@
 	// Windows line-ending.
 	tg.must(ioutil.WriteFile(tg.path("x/x.go"), []byte("package x // import \"x\"\r\n"), 0666))
 	path, err = vgo.FindModulePath(tg.path("x"))
-	if err != nil {
-		t.Fatal(err)
+	if path != "x" || err != nil {
+		t.Fatalf("FindModulePath = %q, %v, want %q, nil", path, err, "x")
 	}
-	if path != "x" {
-		t.Fatalf("FindModulePath = %q, want %q", path, "x")
+
+	// Explicit setting in Godeps.json takes priority over implicit setting from GOPATH location.
+	tg.tempFile("gp/src/example.com/x/y/z/z.go", "package z")
+	gopath := cfg.BuildContext.GOPATH
+	defer func() {
+		cfg.BuildContext.GOPATH = gopath
+	}()
+	cfg.BuildContext.GOPATH = tg.path("gp")
+	path, err = vgo.FindModulePath(tg.path("gp/src/example.com/x/y/z"))
+	if path != "example.com/x/y/z" || err != nil {
+		t.Fatalf("FindModulePath = %q, %v, want %q, nil", path, err, "example.com/x/y/z")
+	}
+
+	tg.tempFile("gp/src/example.com/x/y/z/Godeps/Godeps.json", `
+		{"ImportPath": "unexpected.com/z"}
+	`)
+	path, err = vgo.FindModulePath(tg.path("gp/src/example.com/x/y/z"))
+	if path != "unexpected.com/z" || err != nil {
+		t.Fatalf("FindModulePath = %q, %v, want %q, nil", path, err, "unexpected.com/z")
 	}
 }