// dirsInit starts the scanning of package directories in GOROOT and GOPATH. Any
// extra paths passed to it are included in the channel.
func dirsInit(extra ...Dir) {
+ if buildCtx.GOROOT == "" {
+ stdout, err := exec.Command("go", "env", "GOROOT").Output()
+ if err != nil {
+ if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
+ log.Fatalf("failed to determine GOROOT: $GOROOT is not set and 'go env GOROOT' failed:\n%s", ee.Stderr)
+ }
+ log.Fatalf("failed to determine GOROOT: $GOROOT is not set and could not run 'go env GOROOT':\n\t%s", err)
+ }
+ buildCtx.GOROOT = string(bytes.TrimSpace(stdout))
+ }
+
dirs.hist = make([]Dir, 0, 1000)
dirs.hist = append(dirs.hist, extra...)
dirs.scan = make(chan Dir)
gomod := string(bytes.TrimSpace(stdout))
usingModules = len(gomod) > 0
- if usingModules {
+ if usingModules && buildCtx.GOROOT != "" {
list = append(list,
Dir{dir: filepath.Join(buildCtx.GOROOT, "src"), inModule: true},
Dir{importPath: "cmd", dir: filepath.Join(buildCtx.GOROOT, "src", "cmd"), inModule: true})
}
if !usingModules {
- list = append(list, Dir{dir: filepath.Join(buildCtx.GOROOT, "src")})
+ if buildCtx.GOROOT != "" {
+ list = append(list, Dir{dir: filepath.Join(buildCtx.GOROOT, "src")})
+ }
for _, root := range splitGopath() {
list = append(list, Dir{dir: filepath.Join(root, "src")})
}
// Also convert everything to slash-separated paths for uniform handling.
path = filepath.Clean(filepath.ToSlash(pkg.build.Dir))
// Can we find a decent prefix?
- goroot := filepath.Join(buildCtx.GOROOT, "src")
- if p, ok := trim(path, filepath.ToSlash(goroot)); ok {
- return p
+ if buildCtx.GOROOT != "" {
+ goroot := filepath.Join(buildCtx.GOROOT, "src")
+ if p, ok := trim(path, filepath.ToSlash(goroot)); ok {
+ return p
+ }
}
for _, gopath := range splitGopath() {
if p, ok := trim(path, filepath.ToSlash(gopath)); ok {