x/vgo: fix ignoring tags on build, test, ...

Before, `Tags` was initialized without -tags build flag.

Now, commands like "vgo build -tags tag1" include files
with build tags.

For golang/go#24053 (`vgo list -m` does not update dependencies like `vgo list`)

Change-Id: I1945afb928534563311c86483aea8fdd02490a0a
Reviewed-on: https://go-review.googlesource.com/113896
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
diff --git a/vendor/cmd/go/internal/imports/scan_test.go b/vendor/cmd/go/internal/imports/scan_test.go
index 9774fce..cb0a46a 100644
--- a/vendor/cmd/go/internal/imports/scan_test.go
+++ b/vendor/cmd/go/internal/imports/scan_test.go
@@ -11,7 +11,7 @@
 )
 
 func TestScan(t *testing.T) {
-	imports, testImports, err := ScanDir(filepath.Join(runtime.GOROOT(), "src/encoding/json"), Tags)
+	imports, testImports, err := ScanDir(filepath.Join(runtime.GOROOT(), "src/encoding/json"), Tags())
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/vendor/cmd/go/internal/imports/tags.go b/vendor/cmd/go/internal/imports/tags.go
index 9608c31..ba0ca94 100644
--- a/vendor/cmd/go/internal/imports/tags.go
+++ b/vendor/cmd/go/internal/imports/tags.go
@@ -6,7 +6,14 @@
 
 import "cmd/go/internal/cfg"
 
-var Tags = loadTags()
+var tags map[string]bool
+
+func Tags() map[string]bool {
+	if tags == nil {
+		tags = loadTags()
+	}
+	return tags
+}
 
 func loadTags() map[string]bool {
 	tags := map[string]bool{
diff --git a/vendor/cmd/go/internal/vgo/load.go b/vendor/cmd/go/internal/vgo/load.go
index fb508de..0f11570 100644
--- a/vendor/cmd/go/internal/vgo/load.go
+++ b/vendor/cmd/go/internal/vgo/load.go
@@ -214,7 +214,7 @@
 
 	ld.pkgdir[realPath] = dir
 
-	imports, testImports, err := imports.ScanDir(dir, imports.Tags)
+	imports, testImports, err := imports.ScanDir(dir, imports.Tags())
 	if err != nil {
 		base.Errorf("vgo: %s [%s]: %v", ld.stackText(), dir, err)
 		return
diff --git a/vendor/cmd/go/internal/vgo/search.go b/vendor/cmd/go/internal/vgo/search.go
index 301c3e5..4ca84cc 100644
--- a/vendor/cmd/go/internal/vgo/search.go
+++ b/vendor/cmd/go/internal/vgo/search.go
@@ -138,7 +138,7 @@
 			if !match(name) {
 				return nil
 			}
-			if _, _, err := imports.ScanDir(path, imports.Tags); err == imports.ErrNoGo {
+			if _, _, err := imports.ScanDir(path, imports.Tags()); err == imports.ErrNoGo {
 				return nil
 			}
 
diff --git a/vendor/cmd/go/vgo_test.go b/vendor/cmd/go/vgo_test.go
index ed00e37..b1c32dd 100644
--- a/vendor/cmd/go/vgo_test.go
+++ b/vendor/cmd/go/vgo_test.go
@@ -102,3 +102,36 @@
 	tg.cd(tg.path("x/y"))
 	tg.run("-vgo", "build")
 }
+
+func TestTags(t *testing.T) {
+	// Test that build tags are used. See golang.org/issue/24053.
+	tg := testgo(t)
+	defer tg.cleanup()
+	tg.makeTempdir()
+
+	tg.must(os.MkdirAll(tg.path("x"), 0777))
+	tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
+		module x
+	`), 0666))
+	tg.must(ioutil.WriteFile(tg.path("x/x.go"), []byte(`// +build tag1
+
+		package y
+	`), 0666))
+	tg.must(ioutil.WriteFile(tg.path("x/y.go"), []byte(`// +build tag2
+
+		package y
+	`), 0666))
+	tg.cd(tg.path("x"))
+
+	tg.runFail("-vgo", "list", "-f={{.GoFiles}}")
+	tg.grepStderr("no Go source files", "no Go source files without tags")
+
+	tg.run("-vgo", "list", "-f={{.GoFiles}}", "-tags=tag1")
+	tg.grepStdout(`\[x.go\]`, "Go source files for tag1")
+
+	tg.run("-vgo", "list", "-f={{.GoFiles}}", "-tags", "tag2")
+	tg.grepStdout(`\[y.go\]`, "Go source files for tag2")
+
+	tg.run("-vgo", "list", "-f={{.GoFiles}}", "-tags", "tag1 tag2")
+	tg.grepStdout(`\[x.go y.go\]`, "Go source files for tag1 and tag2")
+}