]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: bypass importGo if srcDir is in GOROOT/src
authorBryan C. Mills <bcmills@google.com>
Mon, 11 Mar 2019 21:25:09 +0000 (17:25 -0400)
committerBryan C. Mills <bcmills@google.com>
Tue, 12 Mar 2019 05:00:20 +0000 (05:00 +0000)
This fixes the builder flake observed in
https://build.golang.org/log/84fe80f8f091b9cef639b3ae2422a673f1462810,
which could be replicated by running

GOPROXY=off GOPATH=$(mktemp -d) go test go/internal/srcimporter

Updates #30228
Fixes #30760

Change-Id: Ibf8b7a2e211611960b074b74af91acd4f0196edb
Reviewed-on: https://go-review.googlesource.com/c/go/+/166977
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/go/build/build.go

index c8aa872bd2df2e9e31054e7505d445e38d655503..1be10f1fb8c367610097b972675a2863ff7c5c43 100644 (file)
@@ -1002,6 +1002,7 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode,
        }
 
        // If modules are not enabled, then the in-process code works fine and we should keep using it.
+       // TODO(bcmills): This assumes that the default is "auto" instead of "on".
        switch os.Getenv("GO111MODULE") {
        case "off":
                return errNoModules
@@ -1017,6 +1018,13 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode,
                }
        }
 
+       // If the source directory is in GOROOT, then the in-process code works fine
+       // and we should keep using it. Moreover, the 'go list' approach below doesn't
+       // take standard-library vendoring into account and will fail.
+       if _, ok := ctxt.hasSubdir(filepath.Join(ctxt.GOROOT, "src"), srcDir); ok {
+               return errNoModules
+       }
+
        // For efficiency, if path is a standard library package, let the usual lookup code handle it.
        if ctxt.GOROOT != "" {
                dir := ctxt.joinPath(ctxt.GOROOT, "src", path)
@@ -1043,7 +1051,12 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode,
        }
 
        cmd := exec.Command("go", "list", "-compiler="+ctxt.Compiler, "-tags="+strings.Join(ctxt.BuildTags, ","), "-installsuffix="+ctxt.InstallSuffix, "-f={{.Dir}}\n{{.ImportPath}}\n{{.Root}}\n{{.Goroot}}\n", path)
+
+       // TODO(bcmills): This is wrong if srcDir is in a vendor directory, or if
+       // srcDir is in some module dependency of the main module. The main module
+       // chooses what the import paths mean: individual packages don't.
        cmd.Dir = srcDir
+
        var stdout, stderr strings.Builder
        cmd.Stdout = &stdout
        cmd.Stderr = &stderr