diff --git a/extractor/util/util.go b/extractor/util/util.go index a592224672c..6d0db2f675c 100644 --- a/extractor/util/util.go +++ b/extractor/util/util.go @@ -28,9 +28,14 @@ func Getenv(key string, aliases ...string) string { // runGoList is a helper function for running go list with format `format` and flags `flags` on // package `pkgpath`. func runGoList(format string, pkgpath string, flags ...string) (string, error) { + return runGoListWithEnv(format, pkgpath, nil, flags...) +} + +func runGoListWithEnv(format string, pkgpath string, additionalEnv []string, flags ...string) (string, error) { args := append([]string{"list", "-e", "-f", format}, flags...) args = append(args, pkgpath) cmd := exec.Command("go", args...) + cmd.Env = append(os.Environ(), additionalEnv...) out, err := cmd.Output() if err != nil { @@ -48,13 +53,15 @@ func runGoList(format string, pkgpath string, flags ...string) (string, error) { // GetModDir gets the absolute directory of the module containing the package with path // `pkgpath`. It passes the `go list` the flags specified by `flags`. func GetModDir(pkgpath string, flags ...string) string { - mod, err := runGoList("{{.Module}}", pkgpath, flags...) + // enable module mode so that we can find a module root if it exists, even if go module support is + // disabled by a build + mod, err := runGoListWithEnv("{{.Module}}", pkgpath, []string{"GO111MODULE=on"}, flags...) if err != nil || mod == "" { // if the command errors or modules aren't being used, return the empty string return "" } - modDir, err := runGoList("{{.Module.Dir}}", pkgpath, flags...) + modDir, err := runGoListWithEnv("{{.Module.Dir}}", pkgpath, []string{"GO111MODULE=on"}, flags...) if err != nil { return "" }