cmd/go/internal/modfetch: move Import and Query to ../modload

Import and Query are about the set of modules viewed
through the modifying lens of the exclude and replace
directives in go.mod. Since package modload is what
manages go.mod, move them there.

Query's 'allowed' argument was meant to capture the
go.mod influence, but it fails to capture replacements,
and at that point we might as well move it where it belongs.

Import is currently ignorant of module replacements
but needs to take them into account.

This CL only moves the code and test. It does not make semantic changes.

Change-Id: I7adde3b0e6076f8ce5719e86c1f771d8dcb4b572
Reviewed-on: https://go-review.googlesource.com/122880
Reviewed-by: Bryan C. Mills <bcmills@google.com>
diff --git a/vendor/cmd/go/internal/modfetch/coderepo_test.go b/vendor/cmd/go/internal/modfetch/coderepo_test.go
index ac88471..b1790e6 100644
--- a/vendor/cmd/go/internal/modfetch/coderepo_test.go
+++ b/vendor/cmd/go/internal/modfetch/coderepo_test.go
@@ -481,56 +481,6 @@
 	return name
 }
 
-var importTests = []struct {
-	path  string
-	mpath string
-	err   string
-}{
-	{
-		path:  "golang.org/x/net/context",
-		mpath: "golang.org/x/net",
-	},
-	{
-		path:  "github.com/rsc/quote/buggy",
-		mpath: "github.com/rsc/quote",
-	},
-	{
-		path:  "golang.org/x/net",
-		mpath: "golang.org/x/net",
-	},
-	{
-		path:  "github.com/rsc/quote",
-		mpath: "github.com/rsc/quote",
-	},
-	{
-		path: "golang.org/x/foo/bar",
-		// TODO(rsc): This error comes from old go get and is terrible. Fix.
-		err: `unrecognized import path "golang.org/x/foo/bar" (parse https://golang.org/x/foo/bar?go-get=1: no go-import meta tags ())`,
-	},
-}
-
-func TestImport(t *testing.T) {
-	testenv.MustHaveExternalNetwork(t)
-
-	for _, tt := range importTests {
-		t.Run(strings.Replace(tt.path, "/", "_", -1), func(t *testing.T) {
-			repo, info, err := Import(tt.path, nil)
-			if tt.err != "" {
-				if err != nil && err.Error() == tt.err {
-					return
-				}
-				t.Fatalf("Import(%q): %v, want error %q", tt.path, err, tt.err)
-			}
-			if err != nil {
-				t.Fatalf("Import(%q): %v", tt.path, err)
-			}
-			if mpath := repo.ModulePath(); mpath != tt.mpath {
-				t.Errorf("repo.ModulePath() = %q (%v), want %q", mpath, info.Version, tt.mpath)
-			}
-		})
-	}
-}
-
 var codeRepoVersionsTests = []struct {
 	path     string
 	prefix   string
diff --git a/vendor/cmd/go/internal/modfetch/repo.go b/vendor/cmd/go/internal/modfetch/repo.go
index 5be9184..917d0ff 100644
--- a/vendor/cmd/go/internal/modfetch/repo.go
+++ b/vendor/cmd/go/internal/modfetch/repo.go
@@ -7,14 +7,12 @@
 import (
 	"fmt"
 	"os"
-	pathpkg "path"
 	"sort"
 	"time"
 
 	"cmd/go/internal/cfg"
 	"cmd/go/internal/get"
 	"cmd/go/internal/modfetch/codehost"
-	"cmd/go/internal/module"
 	"cmd/go/internal/par"
 	"cmd/go/internal/semver"
 	web "cmd/go/internal/web"
@@ -238,61 +236,6 @@
 	return code, nil
 }
 
-// Import returns the module repo and version to use to satisfy the given import path.
-// It considers a sequence of module paths starting with the import path and
-// removing successive path elements from the end. It stops when it finds a module
-// path for which the latest version of the module provides the expected package.
-// If non-nil, the allowed function is used to filter known versions of a given module
-// before determining which one is "latest".
-func Import(path string, allowed func(module.Version) bool) (Repo, *RevInfo, error) {
-	if cfg.BuildGetmode != "" {
-		return nil, nil, fmt.Errorf("import resolution disabled by -getmode=%s", cfg.BuildGetmode)
-	}
-	if traceRepo {
-		defer logCall("Import(%q, ...)", path)()
-	}
-
-	try := func(path string) (Repo, *RevInfo, error) {
-		r, err := Lookup(path)
-		if err != nil {
-			return nil, nil, err
-		}
-		info, err := Query(path, "latest", allowed)
-		if err != nil {
-			return nil, nil, err
-		}
-		_, err = r.GoMod(info.Version)
-		if err != nil {
-			return nil, nil, err
-		}
-		// TODO(rsc): Do what the docs promise: download the module
-		// source code and check that it actually contains code for the
-		// target import path. To do that efficiently we will need to move
-		// the unzipped code cache out of ../modload into this package.
-		// TODO(rsc): When this happens, look carefully at the use of
-		// modfetch.Import in modget.getQuery.
-		return r, info, nil
-	}
-
-	// Find enclosing module by walking up path element by element.
-	var firstErr error
-	for {
-		r, info, err := try(path)
-		if err == nil {
-			return r, info, nil
-		}
-		if firstErr == nil {
-			firstErr = err
-		}
-		p := pathpkg.Dir(path)
-		if p == "." {
-			break
-		}
-		path = p
-	}
-	return nil, nil, firstErr
-}
-
 // ImportRepoRev returns the module and version to use to access
 // the given import path loaded from the source code repository that
 // the original "go get" would have used, at the specific repository revision
diff --git a/vendor/cmd/go/internal/modget/get.go b/vendor/cmd/go/internal/modget/get.go
index c394417..b8713d1 100644
--- a/vendor/cmd/go/internal/modget/get.go
+++ b/vendor/cmd/go/internal/modget/get.go
@@ -501,7 +501,7 @@
 
 	// First choice is always to assume path is a module path.
 	// If that works out, we're done.
-	info, err := modfetch.Query(path, vers, modload.Allowed)
+	info, err := modload.Query(path, vers, modload.Allowed)
 	if err == nil {
 		return module.Version{Path: path, Version: info.Version}, nil
 	}
@@ -518,13 +518,13 @@
 	// If this behavior is wrong, the user can always specify the
 	// desired module path instead of a package path,
 	// and then the code above will handle it.
-	repo, info, err := modfetch.Import(path, modload.Allowed)
+	repo, info, err := modload.Import(path, modload.Allowed)
 	if err != nil {
 		return module.Version{}, err
 	}
 	if vers != "latest" {
-		// modfetch.Import returned "latest" version. Look up requested version.
-		if info, err = modfetch.Query(repo.ModulePath(), vers, modload.Allowed); err != nil {
+		// modload.Import returned "latest" version. Look up requested version.
+		if info, err = modload.Query(repo.ModulePath(), vers, modload.Allowed); err != nil {
 			return module.Version{}, err
 		}
 	}
@@ -534,7 +534,7 @@
 // isModulePath reports whether path names an actual module,
 // defined as one with an accessible latest version.
 func isModulePath(path string) bool {
-	_, err := modfetch.Query(path, "latest", modload.Allowed)
+	_, err := modload.Query(path, "latest", modload.Allowed)
 	return err == nil
 }
 
@@ -579,7 +579,7 @@
 		// For patch upgrade, query "v1.2".
 		query = semver.MajorMinor(m.Version)
 	}
-	info, err := modfetch.Query(m.Path, query, modload.Allowed)
+	info, err := modload.Query(m.Path, query, modload.Allowed)
 	if err != nil {
 		// Report error but return m, to let version selection continue.
 		// (Reporting the error will fail the command at the next base.ExitIfErrors.)
diff --git a/vendor/cmd/go/internal/modload/build.go b/vendor/cmd/go/internal/modload/build.go
index d139996..17cbcda 100644
--- a/vendor/cmd/go/internal/modload/build.go
+++ b/vendor/cmd/go/internal/modload/build.go
@@ -70,7 +70,7 @@
 // addUpdate fills in m.Update if an updated version is available.
 func addUpdate(m *modinfo.ModulePublic) {
 	if m.Version != "" {
-		if info, err := modfetch.Query(m.Path, "latest", Allowed); err == nil && info.Version != m.Version {
+		if info, err := Query(m.Path, "latest", Allowed); err == nil && info.Version != m.Version {
 			m.Update = &modinfo.ModulePublic{
 				Path:    m.Path,
 				Version: info.Version,
@@ -109,7 +109,7 @@
 	// complete fills in the extra fields in m.
 	complete := func(m *modinfo.ModulePublic) {
 		if m.Version != "" {
-			if q, err := modfetch.Query(m.Path, m.Version, nil); err != nil {
+			if q, err := Query(m.Path, m.Version, nil); err != nil {
 				m.Error = &modinfo.ModuleError{Err: err.Error()}
 			} else {
 				m.Version = q.Version
diff --git a/vendor/cmd/go/internal/modload/import.go b/vendor/cmd/go/internal/modload/import.go
new file mode 100644
index 0000000..3956f75
--- /dev/null
+++ b/vendor/cmd/go/internal/modload/import.go
@@ -0,0 +1,66 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package modload
+
+import (
+	"fmt"
+	pathpkg "path"
+
+	"cmd/go/internal/cfg"
+	"cmd/go/internal/modfetch"
+	"cmd/go/internal/module"
+)
+
+// Import returns the module repo and version to use to satisfy the given import path.
+// It considers a sequence of module paths starting with the import path and
+// removing successive path elements from the end. It stops when it finds a module
+// path for which the latest version of the module provides the expected package.
+// If non-nil, the allowed function is used to filter known versions of a given module
+// before determining which one is "latest".
+func Import(path string, allowed func(module.Version) bool) (modfetch.Repo, *modfetch.RevInfo, error) {
+	if cfg.BuildGetmode != "" {
+		return nil, nil, fmt.Errorf("import resolution disabled by -getmode=%s", cfg.BuildGetmode)
+	}
+
+	try := func(path string) (modfetch.Repo, *modfetch.RevInfo, error) {
+		r, err := modfetch.Lookup(path)
+		if err != nil {
+			return nil, nil, err
+		}
+		info, err := Query(path, "latest", allowed)
+		if err != nil {
+			return nil, nil, err
+		}
+		_, err = r.GoMod(info.Version)
+		if err != nil {
+			return nil, nil, err
+		}
+		// TODO(rsc): Do what the docs promise: download the module
+		// source code and check that it actually contains code for the
+		// target import path. To do that efficiently we will need to move
+		// the unzipped code cache out of ../modload into this package.
+		// TODO(rsc): When this happens, look carefully at the use of
+		// modfetch.Import in modget.getQuery.
+		return r, info, nil
+	}
+
+	// Find enclosing module by walking up path element by element.
+	var firstErr error
+	for {
+		r, info, err := try(path)
+		if err == nil {
+			return r, info, nil
+		}
+		if firstErr == nil {
+			firstErr = err
+		}
+		p := pathpkg.Dir(path)
+		if p == "." {
+			break
+		}
+		path = p
+	}
+	return nil, nil, firstErr
+}
diff --git a/vendor/cmd/go/internal/modload/import_test.go b/vendor/cmd/go/internal/modload/import_test.go
new file mode 100644
index 0000000..a7ca749
--- /dev/null
+++ b/vendor/cmd/go/internal/modload/import_test.go
@@ -0,0 +1,61 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package modload
+
+import (
+	"internal/testenv"
+	"strings"
+	"testing"
+)
+
+var importTests = []struct {
+	path  string
+	mpath string
+	err   string
+}{
+	{
+		path:  "golang.org/x/net/context",
+		mpath: "golang.org/x/net",
+	},
+	{
+		path:  "github.com/rsc/quote/buggy",
+		mpath: "github.com/rsc/quote",
+	},
+	{
+		path:  "golang.org/x/net",
+		mpath: "golang.org/x/net",
+	},
+	{
+		path:  "github.com/rsc/quote",
+		mpath: "github.com/rsc/quote",
+	},
+	{
+		path: "golang.org/x/foo/bar",
+		// TODO(rsc): This error comes from old go get and is terrible. Fix.
+		err: `unrecognized import path "golang.org/x/foo/bar" (parse https://golang.org/x/foo/bar?go-get=1: no go-import meta tags ())`,
+	},
+}
+
+func TestImport(t *testing.T) {
+	testenv.MustHaveExternalNetwork(t)
+
+	for _, tt := range importTests {
+		t.Run(strings.Replace(tt.path, "/", "_", -1), func(t *testing.T) {
+			repo, info, err := Import(tt.path, nil)
+			if tt.err != "" {
+				if err != nil && err.Error() == tt.err {
+					return
+				}
+				t.Fatalf("Import(%q): %v, want error %q", tt.path, err, tt.err)
+			}
+			if err != nil {
+				t.Fatalf("Import(%q): %v", tt.path, err)
+			}
+			if mpath := repo.ModulePath(); mpath != tt.mpath {
+				t.Errorf("repo.ModulePath() = %q (%v), want %q", mpath, info.Version, tt.mpath)
+			}
+		})
+	}
+}
diff --git a/vendor/cmd/go/internal/modload/init.go b/vendor/cmd/go/internal/modload/init.go
index 5f9e72b..12a9e3d 100644
--- a/vendor/cmd/go/internal/modload/init.go
+++ b/vendor/cmd/go/internal/modload/init.go
@@ -518,7 +518,7 @@
 		return vers, nil
 	}
 
-	info, err := modfetch.Query(path, vers, nil)
+	info, err := Query(path, vers, nil)
 	if err != nil {
 		return "", err
 	}
diff --git a/vendor/cmd/go/internal/modload/list.go b/vendor/cmd/go/internal/modload/list.go
index 54db550..69a832d 100644
--- a/vendor/cmd/go/internal/modload/list.go
+++ b/vendor/cmd/go/internal/modload/list.go
@@ -10,7 +10,6 @@
 	"strings"
 
 	"cmd/go/internal/base"
-	"cmd/go/internal/modfetch"
 	"cmd/go/internal/modinfo"
 	"cmd/go/internal/module"
 	"cmd/go/internal/par"
@@ -56,7 +55,7 @@
 			base.Fatalf("go: cannot use relative path %s to specify module", arg)
 		}
 		if i := strings.Index(arg, "@"); i >= 0 {
-			info, err := modfetch.Query(arg[:i], arg[i+1:], nil)
+			info, err := Query(arg[:i], arg[i+1:], nil)
 			if err != nil {
 				mods = append(mods, &modinfo.ModulePublic{
 					Path:    arg[:i],
diff --git a/vendor/cmd/go/internal/modload/load.go b/vendor/cmd/go/internal/modload/load.go
index 6d9f83e..3f67fc4 100644
--- a/vendor/cmd/go/internal/modload/load.go
+++ b/vendor/cmd/go/internal/modload/load.go
@@ -636,7 +636,7 @@
 	ld.missingMu.Unlock()
 
 	fmt.Fprintf(os.Stderr, "resolving import %q\n", path)
-	repo, info, err := modfetch.Import(path, Allowed)
+	repo, info, err := Import(path, Allowed)
 	if err != nil {
 		base.Errorf("go: %s: %v", pkg.stackText(), err)
 		return
diff --git a/vendor/cmd/go/internal/modfetch/query.go b/vendor/cmd/go/internal/modload/query.go
similarity index 95%
rename from vendor/cmd/go/internal/modfetch/query.go
rename to vendor/cmd/go/internal/modload/query.go
index 9d3a6f0..2973f81 100644
--- a/vendor/cmd/go/internal/modfetch/query.go
+++ b/vendor/cmd/go/internal/modload/query.go
@@ -2,9 +2,10 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package modfetch
+package modload
 
 import (
+	"cmd/go/internal/modfetch"
 	"cmd/go/internal/module"
 	"cmd/go/internal/semver"
 	"fmt"
@@ -28,14 +29,14 @@
 //
 // If the allowed function is non-nil, Query excludes any versions for which allowed returns false.
 //
-func Query(path, query string, allowed func(module.Version) bool) (*RevInfo, error) {
+func Query(path, query string, allowed func(module.Version) bool) (*modfetch.RevInfo, error) {
 	if allowed == nil {
 		allowed = func(module.Version) bool { return true }
 	}
 
 	// Parse query to detect parse errors (and possibly handle query)
 	// before any network I/O.
-	badVersion := func(v string) (*RevInfo, error) {
+	badVersion := func(v string) (*modfetch.RevInfo, error) {
 		return nil, fmt.Errorf("invalid semantic version %q in range %q", v, query)
 	}
 	var ok func(module.Version) bool
@@ -100,11 +101,11 @@
 		if !allowed(module.Version{Path: path, Version: vers}) {
 			return nil, fmt.Errorf("%s@%s excluded", path, vers)
 		}
-		return Stat(path, vers)
+		return modfetch.Stat(path, vers)
 
 	default:
 		// Direct lookup of semantic version or commit identifier.
-		info, err := Stat(path, query)
+		info, err := modfetch.Stat(path, query)
 		if err != nil {
 			return nil, err
 		}
@@ -115,7 +116,7 @@
 	}
 
 	// Load versions and execute query.
-	repo, err := Lookup(path)
+	repo, err := modfetch.Lookup(path)
 	if err != nil {
 		return nil, err
 	}
diff --git a/vendor/cmd/go/internal/modfetch/query_test.go b/vendor/cmd/go/internal/modload/query_test.go
similarity index 93%
rename from vendor/cmd/go/internal/modfetch/query_test.go
rename to vendor/cmd/go/internal/modload/query_test.go
index eabdf75..2f84c4f 100644
--- a/vendor/cmd/go/internal/modfetch/query_test.go
+++ b/vendor/cmd/go/internal/modload/query_test.go
@@ -2,16 +2,36 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package modfetch
+package modload
 
 import (
-	"cmd/go/internal/module"
 	"internal/testenv"
+	"io/ioutil"
+	"log"
+	"os"
 	"path"
 	"strings"
 	"testing"
+
+	"cmd/go/internal/modfetch/codehost"
+	"cmd/go/internal/module"
 )
 
+func TestMain(m *testing.M) {
+	os.Exit(testMain(m))
+}
+
+func testMain(m *testing.M) int {
+	dir, err := ioutil.TempDir("", "gitrepo-test-")
+	if err != nil {
+		log.Fatal(err)
+	}
+	defer os.RemoveAll(dir)
+
+	codehost.WorkRoot = dir
+	return m.Run()
+}
+
 var (
 	queryRepo   = "vcs-test.golang.org/git/querytest.git"
 	queryRepoV2 = queryRepo + "/v2"