@@ -1002,6 +1002,7 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode,
1002
1002
}
1003
1003
1004
1004
// 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".
1005
1006
switch os .Getenv ("GO111MODULE" ) {
1006
1007
case "off" :
1007
1008
return errNoModules
@@ -1017,6 +1018,13 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode,
1017
1018
}
1018
1019
}
1019
1020
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
+
1020
1028
// For efficiency, if path is a standard library package, let the usual lookup code handle it.
1021
1029
if ctxt .GOROOT != "" {
1022
1030
dir := ctxt .joinPath (ctxt .GOROOT , "src" , path )
@@ -1043,7 +1051,12 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode,
1043
1051
}
1044
1052
1045
1053
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.
1046
1058
cmd .Dir = srcDir
1059
+
1047
1060
var stdout , stderr strings.Builder
1048
1061
cmd .Stdout = & stdout
1049
1062
cmd .Stderr = & stderr
0 commit comments