cmd/go: apply same per-package flags to test and xtest builds

If package strings has a particular set of gcflags, then the strings_test
pseudo-package built as part of the test binary should inherit the same flags.

Fixes #22831.

Change-Id: I0e896b6c0f1063454300b7323f577feffbd6650b
Reviewed-on: https://go-review.googlesource.com/81496
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go
index cf6a917..abff432 100644
--- a/src/cmd/go/go_test.go
+++ b/src/cmd/go/go_test.go
@@ -5191,6 +5191,12 @@
 	tg.grepStderr("reflect", "did not rebuild reflect")
 	tg.grepStderr("compile.* -N .*-p reflect", "did not build reflect with -N flag")
 	tg.grepStderrNot("compile.* -N .*-p fmt", "incorrectly built fmt with -N flag")
+
+	tg.run("test", "-c", "-n", "-gcflags=-N", "strings")
+	tg.grepStderr("compile.* -N .*compare_test.go", "did not build strings_test package with -N flag")
+
+	tg.run("test", "-c", "-n", "-gcflags=strings=-N", "strings")
+	tg.grepStderr("compile.* -N .*compare_test.go", "did not build strings_test package with -N flag")
 }
 
 func TestGoTestMinusN(t *testing.T) {
diff --git a/src/cmd/go/internal/test/test.go b/src/cmd/go/internal/test/test.go
index 9740b45..1513a80 100644
--- a/src/cmd/go/internal/test/test.go
+++ b/src/cmd/go/internal/test/test.go
@@ -892,6 +892,11 @@
 				},
 				Imports:    ximports,
 				RawImports: rawXTestImports,
+
+				Asmflags:   p.Internal.Asmflags,
+				Gcflags:    p.Internal.Gcflags,
+				Ldflags:    p.Internal.Ldflags,
+				Gccgoflags: p.Internal.Gccgoflags,
 			},
 		}
 		if pxtestNeedsPtest {