1
0
mirror of https://github.com/golang/go synced 2024-11-17 17:54:48 -07:00

go/build: change the search order for "vendor/" paths based on srcDir

If srcDir is within GOROOT, prefer GOROOT.
Otherwise, prefer GOPATH.

The attached tests may seem a bit strange; they will make more sense
in a followup CL.

Updates #16333
Updates #30241
Updates #30228

Change-Id: Ic5f1334cce5e242d7f49080aba083bcf2080dee3
Reviewed-on: https://go-review.googlesource.com/c/go/+/164619
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Bryan C. Mills 2019-02-28 16:04:43 -05:00
parent 10156b6783
commit 514c5593f0
2 changed files with 80 additions and 11 deletions

View File

@ -0,0 +1,41 @@
env GO111MODULE=off
[!gc] skip
# A package importing 'net/http' should resolve its dependencies
# to the package 'vendor/golang.org/x/net/http2/hpack' within GOROOT.
cd importnethttp
go list -deps -f '{{.ImportPath}} {{.Dir}}'
stdout ^internal/x/net/http2/hpack
stdout $GOROOT[/\\]src[/\\]internal[/\\]x[/\\]net[/\\]http2[/\\]hpack
! stdout $GOPATH[/\\]src[/\\]vendor
# In the presence of $GOPATH/src/vendor/golang.org/x/net/http2/hpack,
# a package in GOPATH importing 'golang.org/x/net/http2/hpack' should
# resolve its dependencies in GOPATH/src.
cd ../issue16333
go build .
go list -deps -f '{{.ImportPath}} {{.Dir}}' .
stdout $GOPATH[/\\]src[/\\]vendor[/\\]golang.org[/\\]x[/\\]net[/\\]http2[/\\]hpack
! stdout $GOROOT[/\\]src[/\\]vendor
go list -test -deps -f '{{.ImportPath}} {{.Dir}}' .
stdout $GOPATH[/\\]src[/\\]vendor[/\\]golang.org[/\\]x[/\\]net[/\\]http2[/\\]hpack
! stdout $GOROOT[/\\]src[/\\]vendor
-- issue16333/issue16333.go --
package vendoring17
import _ "golang.org/x/net/http2/hpack"
-- issue16333/issue16333_test.go --
package vendoring17
import _ "testing"
import _ "golang.org/x/net/http2/hpack"
-- importnethttp/http.go --
package importnethttp
import _ "net/http"
-- $GOPATH/src/vendor/golang.org/x/net/http2/hpack/hpack.go --
package hpack

View File

@ -647,18 +647,28 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
// Determine directory from import path.
if ctxt.GOROOT != "" {
dir := ctxt.joinPath(ctxt.GOROOT, "src", path)
if ctxt.Compiler != "gccgo" {
isDir := ctxt.isDir(dir)
binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(ctxt.GOROOT, pkga))
if isDir || binaryOnly {
p.Dir = dir
p.Goroot = true
p.Root = ctxt.GOROOT
goto Found
}
// If the package path starts with "vendor/", only search GOROOT before
// GOPATH if the importer is also within GOROOT. That way, if the user has
// vendored in a package that is subsequently included in the standard
// distribution, they'll continue to pick up their own vendored copy.
gorootFirst := srcDir == "" || !strings.HasPrefix(path, "vendor/")
if !gorootFirst {
_, gorootFirst = ctxt.hasSubdir(ctxt.GOROOT, srcDir)
}
if gorootFirst {
dir := ctxt.joinPath(ctxt.GOROOT, "src", path)
if ctxt.Compiler != "gccgo" {
isDir := ctxt.isDir(dir)
binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(ctxt.GOROOT, pkga))
if isDir || binaryOnly {
p.Dir = dir
p.Goroot = true
p.Root = ctxt.GOROOT
goto Found
}
}
tried.goroot = dir
}
tried.goroot = dir
}
if ctxt.Compiler == "gccgo" && goroot.IsStandardPackage(ctxt.GOROOT, ctxt.Compiler, path) {
p.Dir = ctxt.joinPath(ctxt.GOROOT, "src", path)
@ -678,6 +688,24 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
tried.gopath = append(tried.gopath, dir)
}
// If we tried GOPATH first due to a "vendor/" prefix, fall back to GOPATH.
// That way, the user can still get useful results from 'go list' for
// standard-vendored paths passed on the command line.
if ctxt.GOROOT != "" && tried.goroot == "" {
dir := ctxt.joinPath(ctxt.GOROOT, "src", path)
if ctxt.Compiler != "gccgo" {
isDir := ctxt.isDir(dir)
binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(ctxt.GOROOT, pkga))
if isDir || binaryOnly {
p.Dir = dir
p.Goroot = true
p.Root = ctxt.GOROOT
goto Found
}
}
tried.goroot = dir
}
// package was not found
var paths []string
format := "\t%s (vendor tree)"