@@ -28,9 +28,14 @@ func Getenv(key string, aliases ...string) string {
28
28
// runGoList is a helper function for running go list with format `format` and flags `flags` on
29
29
// package `pkgpath`.
30
30
func runGoList (format string , pkgpath string , flags ... string ) (string , error ) {
31
+ return runGoListWithEnv (format , pkgpath , nil , flags ... )
32
+ }
33
+
34
+ func runGoListWithEnv (format string , pkgpath string , additionalEnv []string , flags ... string ) (string , error ) {
31
35
args := append ([]string {"list" , "-e" , "-f" , format }, flags ... )
32
36
args = append (args , pkgpath )
33
37
cmd := exec .Command ("go" , args ... )
38
+ cmd .Env = append (os .Environ (), additionalEnv ... )
34
39
out , err := cmd .Output ()
35
40
36
41
if err != nil {
@@ -48,13 +53,15 @@ func runGoList(format string, pkgpath string, flags ...string) (string, error) {
48
53
// GetModDir gets the absolute directory of the module containing the package with path
49
54
// `pkgpath`. It passes the `go list` the flags specified by `flags`.
50
55
func GetModDir (pkgpath string , flags ... string ) string {
51
- mod , err := runGoList ("{{.Module}}" , pkgpath , flags ... )
56
+ // enable module mode so that we can find a module root if it exists, even if go module support is
57
+ // disabled by a build
58
+ mod , err := runGoListWithEnv ("{{.Module}}" , pkgpath , []string {"GO111MODULE=on" }, flags ... )
52
59
if err != nil || mod == "<nil>" {
53
60
// if the command errors or modules aren't being used, return the empty string
54
61
return ""
55
62
}
56
63
57
- modDir , err := runGoList ("{{.Module.Dir}}" , pkgpath , flags ... )
64
+ modDir , err := runGoListWithEnv ("{{.Module.Dir}}" , pkgpath , [] string { "GO111MODULE=on" } , flags ... )
58
65
if err != nil {
59
66
return ""
60
67
}
0 commit comments