@@ -28,9 +28,14 @@ func Getenv(key string, aliases ...string) string {
2828// runGoList is a helper function for running go list with format `format` and flags `flags` on
2929// package `pkgpath`.
3030func 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 ) {
3135 args := append ([]string {"list" , "-e" , "-f" , format }, flags ... )
3236 args = append (args , pkgpath )
3337 cmd := exec .Command ("go" , args ... )
38+ cmd .Env = append (os .Environ (), additionalEnv ... )
3439 out , err := cmd .Output ()
3540
3641 if err != nil {
@@ -48,13 +53,15 @@ func runGoList(format string, pkgpath string, flags ...string) (string, error) {
4853// GetModDir gets the absolute directory of the module containing the package with path
4954// `pkgpath`. It passes the `go list` the flags specified by `flags`.
5055func 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 ... )
5259 if err != nil || mod == "<nil>" {
5360 // if the command errors or modules aren't being used, return the empty string
5461 return ""
5562 }
5663
57- modDir , err := runGoList ("{{.Module.Dir}}" , pkgpath , flags ... )
64+ modDir , err := runGoListWithEnv ("{{.Module.Dir}}" , pkgpath , [] string { "GO111MODULE=on" } , flags ... )
5865 if err != nil {
5966 return ""
6067 }
0 commit comments