@@ -35,7 +35,7 @@ type Context struct {
35
35
GOARCH string // target architecture
36
36
GOOS string // target operating system
37
37
GOROOT string // Go root
38
- GOPATH string // Go path
38
+ GOPATH string // Go paths
39
39
40
40
// Dir is the caller's working directory, or the empty string to use
41
41
// the current directory of the running process. In module mode, this is used
@@ -302,7 +302,9 @@ func defaultContext() Context {
302
302
303
303
c .GOARCH = buildcfg .GOARCH
304
304
c .GOOS = buildcfg .GOOS
305
- c .GOROOT = pathpkg .Clean (runtime .GOROOT ())
305
+ if goroot := runtime .GOROOT (); goroot != "" {
306
+ c .GOROOT = filepath .Clean (goroot )
307
+ }
306
308
c .GOPATH = envOr ("GOPATH" , defaultGOPATH ())
307
309
c .Compiler = runtime .Compiler
308
310
@@ -672,7 +674,7 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
672
674
}
673
675
return false
674
676
}
675
- if ctxt .Compiler != "gccgo" && searchVendor (ctxt .GOROOT , true ) {
677
+ if ctxt .Compiler != "gccgo" && ctxt . GOROOT != "" && searchVendor (ctxt .GOROOT , true ) {
676
678
goto Found
677
679
}
678
680
for _ , root := range gopath {
@@ -706,12 +708,12 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
706
708
}
707
709
tried .goroot = dir
708
710
}
709
- }
710
- if ctxt . Compiler == "gccgo" && goroot . IsStandardPackage (ctxt .GOROOT , ctxt . Compiler , path ) {
711
- p . Dir = ctxt . joinPath ( ctxt . GOROOT , "src" , path )
712
- p . Goroot = true
713
- p . Root = ctxt . GOROOT
714
- goto Found
711
+ if ctxt . Compiler == "gccgo" && goroot . IsStandardPackage ( ctxt . GOROOT , ctxt . Compiler , path ) {
712
+ p . Dir = ctxt . joinPath (ctxt .GOROOT , "src" , path )
713
+ p . Goroot = true
714
+ p . Root = ctxt . GOROOT
715
+ goto Found
716
+ }
715
717
}
716
718
for _ , root := range gopath {
717
719
dir := ctxt .joinPath (root , "src" , path )
@@ -1082,6 +1084,13 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode)
1082
1084
return errNoModules
1083
1085
}
1084
1086
1087
+ // If ctxt.GOROOT is not set, we don't know which go command to invoke,
1088
+ // and even if we did we might return packages in GOROOT that we wouldn't otherwise find
1089
+ // (because we don't know to search in 'go env GOROOT' otherwise).
1090
+ if ctxt .GOROOT == "" {
1091
+ return errNoModules
1092
+ }
1093
+
1085
1094
// Predict whether module aware mode is enabled by checking the value of
1086
1095
// GO111MODULE and looking for a go.mod file in the source directory or
1087
1096
// one of its parents. Running 'go env GOMOD' in the source directory would
@@ -1119,11 +1128,8 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode)
1119
1128
}
1120
1129
1121
1130
// For efficiency, if path is a standard library package, let the usual lookup code handle it.
1122
- if ctxt .GOROOT != "" {
1123
- dir := ctxt .joinPath (ctxt .GOROOT , "src" , path )
1124
- if ctxt .isDir (dir ) {
1125
- return errNoModules
1126
- }
1131
+ if dir := ctxt .joinPath (ctxt .GOROOT , "src" , path ); ctxt .isDir (dir ) {
1132
+ return errNoModules
1127
1133
}
1128
1134
1129
1135
// If GO111MODULE=auto, look to see if there is a go.mod.
@@ -1165,7 +1171,8 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode)
1165
1171
}
1166
1172
}
1167
1173
1168
- 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 )
1174
+ goCmd := filepath .Join (ctxt .GOROOT , "bin" , "go" )
1175
+ 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 )
1169
1176
1170
1177
if ctxt .Dir != "" {
1171
1178
cmd .Dir = ctxt .Dir
0 commit comments