Skip to content

Commit 9fedec7

Browse files
author
Bryan C. Mills
committed
go/build: bypass importGo if srcDir is in GOROOT/src
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 8b2a2d0 commit 9fedec7

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/go/build/build.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode,
10021002
}
10031003

10041004
// If modules are not enabled, then the in-process code works fine and we should keep using it.
1005+
// TODO(bcmills): This assumes that the default is "auto" instead of "on".
10051006
switch os.Getenv("GO111MODULE") {
10061007
case "off":
10071008
return errNoModules
@@ -1017,6 +1018,13 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode,
10171018
}
10181019
}
10191020

1021+
// If the source directory is in GOROOT, then the in-process code works fine
1022+
// and we should keep using it. Moreover, the 'go list' approach below doesn't
1023+
// take standard-library vendoring into account and will fail.
1024+
if _, ok := ctxt.hasSubdir(filepath.Join(ctxt.GOROOT, "src"), srcDir); ok {
1025+
return errNoModules
1026+
}
1027+
10201028
// For efficiency, if path is a standard library package, let the usual lookup code handle it.
10211029
if ctxt.GOROOT != "" {
10221030
dir := ctxt.joinPath(ctxt.GOROOT, "src", path)
@@ -1043,7 +1051,12 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode,
10431051
}
10441052

10451053
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)
1054+
1055+
// TODO(bcmills): This is wrong if srcDir is in a vendor directory, or if
1056+
// srcDir is in some module dependency of the main module. The main module
1057+
// chooses what the import paths mean: individual packages don't.
10461058
cmd.Dir = srcDir
1059+
10471060
var stdout, stderr strings.Builder
10481061
cmd.Stdout = &stdout
10491062
cmd.Stderr = &stderr

0 commit comments

Comments
 (0)