imports: pass more of the environment

goimports now wants various module flags, but I forgot to set them up in
the many places we create environments. Do so.

Change-Id: Ic3817caeb8fc4d564b49006ef6ca6842b2498eaf
Reviewed-on: https://go-review.googlesource.com/c/tools/+/211581
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/cmd/goimports/goimports.go b/cmd/goimports/goimports.go
index a476a7f..2cca292 100644
--- a/cmd/goimports/goimports.go
+++ b/cmd/goimports/goimports.go
@@ -45,8 +45,12 @@
 		Fragment:  true,
 		// This environment, and its caches, will be reused for the whole run.
 		Env: &imports.ProcessEnv{
-			GOPATH: build.Default.GOPATH,
-			GOROOT: build.Default.GOROOT,
+			GOPATH:      build.Default.GOPATH,
+			GOROOT:      build.Default.GOROOT,
+			GOFLAGS:     os.Getenv("GOFLAGS"),
+			GO111MODULE: os.Getenv("GO111MODULE"),
+			GOPROXY:     os.Getenv("GOPROXY"),
+			GOSUMDB:     os.Getenv("GOSUMDB"),
 		},
 	}
 	exitCode = 0
diff --git a/imports/forward.go b/imports/forward.go
index eef2596..b4f4287 100644
--- a/imports/forward.go
+++ b/imports/forward.go
@@ -4,6 +4,7 @@
 
 import (
 	"go/build"
+	"os"
 
 	intimp "golang.org/x/tools/internal/imports"
 )
@@ -42,6 +43,10 @@
 		Env: &intimp.ProcessEnv{
 			GOPATH:      build.Default.GOPATH,
 			GOROOT:      build.Default.GOROOT,
+			GOFLAGS:     os.Getenv("GOFLAGS"),
+			GO111MODULE: os.Getenv("GO111MODULE"),
+			GOPROXY:     os.Getenv("GOPROXY"),
+			GOSUMDB:     os.Getenv("GOSUMDB"),
 			Debug:       Debug,
 			LocalPrefix: LocalPrefix,
 		},
diff --git a/internal/imports/imports.go b/internal/imports/imports.go
index e066d90..b5c9754 100644
--- a/internal/imports/imports.go
+++ b/internal/imports/imports.go
@@ -21,6 +21,7 @@
 	"io"
 	"io/ioutil"
 	"log"
+	"os"
 	"regexp"
 	"strconv"
 	"strings"
@@ -145,8 +146,12 @@
 	// Set the env if the user has not provided it.
 	if opt.Env == nil {
 		opt.Env = &ProcessEnv{
-			GOPATH: build.Default.GOPATH,
-			GOROOT: build.Default.GOROOT,
+			GOPATH:      build.Default.GOPATH,
+			GOROOT:      build.Default.GOROOT,
+			GOFLAGS:     os.Getenv("GOFLAGS"),
+			GO111MODULE: os.Getenv("GO111MODULE"),
+			GOPROXY:     os.Getenv("GOPROXY"),
+			GOSUMDB:     os.Getenv("GOSUMDB"),
 		}
 	}