GOARCH string // target architecture
GOOS string // target operating system
GOROOT string // Go root
- GOPATH string // Go path
+ GOPATH string // Go paths
// Dir is the caller's working directory, or the empty string to use
// the current directory of the running process. In module mode, this is used
c.GOARCH = buildcfg.GOARCH
c.GOOS = buildcfg.GOOS
- c.GOROOT = pathpkg.Clean(runtime.GOROOT())
+ if goroot := runtime.GOROOT(); goroot != "" {
+ c.GOROOT = filepath.Clean(goroot)
+ }
c.GOPATH = envOr("GOPATH", defaultGOPATH())
c.Compiler = runtime.Compiler
}
return false
}
- if ctxt.Compiler != "gccgo" && searchVendor(ctxt.GOROOT, true) {
+ if ctxt.Compiler != "gccgo" && ctxt.GOROOT != "" && searchVendor(ctxt.GOROOT, true) {
goto Found
}
for _, root := range gopath {
}
tried.goroot = dir
}
- }
- if ctxt.Compiler == "gccgo" && goroot.IsStandardPackage(ctxt.GOROOT, ctxt.Compiler, path) {
- p.Dir = ctxt.joinPath(ctxt.GOROOT, "src", path)
- p.Goroot = true
- p.Root = ctxt.GOROOT
- goto Found
+ if ctxt.Compiler == "gccgo" && goroot.IsStandardPackage(ctxt.GOROOT, ctxt.Compiler, path) {
+ p.Dir = ctxt.joinPath(ctxt.GOROOT, "src", path)
+ p.Goroot = true
+ p.Root = ctxt.GOROOT
+ goto Found
+ }
}
for _, root := range gopath {
dir := ctxt.joinPath(root, "src", path)
return errNoModules
}
+ // If ctxt.GOROOT is not set, we don't know which go command to invoke,
+ // and even if we did we might return packages in GOROOT that we wouldn't otherwise find
+ // (because we don't know to search in 'go env GOROOT' otherwise).
+ if ctxt.GOROOT == "" {
+ return errNoModules
+ }
+
// Predict whether module aware mode is enabled by checking the value of
// GO111MODULE and looking for a go.mod file in the source directory or
// one of its parents. Running 'go env GOMOD' in the source directory would
}
// 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)
- if ctxt.isDir(dir) {
- return errNoModules
- }
+ if dir := ctxt.joinPath(ctxt.GOROOT, "src", path); ctxt.isDir(dir) {
+ return errNoModules
}
// If GO111MODULE=auto, look to see if there is a go.mod.
}
}
- cmd := exec.Command("go", "list", "-e", "-compiler="+ctxt.Compiler, "-tags="+strings.Join(ctxt.BuildTags, ","), "-installsuffix="+ctxt.InstallSuffix, "-f={{.Dir}}\n{{.ImportPath}}\n{{.Root}}\n{{.Goroot}}\n{{if .Error}}{{.Error}}{{end}}\n", "--", path)
+ goCmd := filepath.Join(ctxt.GOROOT, "bin", "go")
+ cmd := exec.Command(goCmd, "list", "-e", "-compiler="+ctxt.Compiler, "-tags="+strings.Join(ctxt.BuildTags, ","), "-installsuffix="+ctxt.InstallSuffix, "-f={{.Dir}}\n{{.ImportPath}}\n{{.Root}}\n{{.Goroot}}\n{{if .Error}}{{.Error}}{{end}}\n", "--", path)
if ctxt.Dir != "" {
cmd.Dir = ctxt.Dir
package build
import (
- "flag"
"internal/testenv"
"io"
"os"
)
func TestMain(m *testing.M) {
- flag.Parse()
- if goTool, err := testenv.GoTool(); err == nil {
- os.Setenv("PATH", filepath.Dir(goTool)+string(os.PathListSeparator)+os.Getenv("PATH"))
- }
+ Default.GOROOT = testenv.GOROOT(nil)
os.Exit(m.Run())
}
}
func TestEmptyImport(t *testing.T) {
- p, err := Import("", Default.GOROOT, FindOnly)
+ p, err := Import("", testenv.GOROOT(t), FindOnly)
if err == nil {
t.Fatal(`Import("") returned nil error.`)
}
testenv.MustHaveGoBuild(t) // really must just have source
ctxt := Default
ctxt.GOPATH = ""
- p, err := ctxt.ImportDir(filepath.Join(ctxt.GOROOT, "src/path"), 0)
+ p, err := ctxt.ImportDir(filepath.Join(testenv.GOROOT(t), "src/path"), 0)
if err != nil {
t.Fatal(err)
}