Skip to content

Commit 374546d

Browse files
aarzilliJay Conrod
authored and
Jay Conrod
committed
cmd/go: respect gcflags, ldflags in 'go test'
Fixes bug introduced by https://golang.org/cl/129059 where gcflags='all=...' and ldflags='all=...' would not be applied to some packages built by 'go test'. LoadImport used to set gcflags/ldflags for the Package objects it created, in https://golang.org/cl/129059 this code was factored out to setToolFlags. The codepath of `go build` was updated to call setToolFlags appropriatley, but the codepath of `go test -c` wasn't, resulting in gcflags/ldflags being applied inconsistently when building tests. This commit changes TestPackagesFor to call setToolFlags on the package objects it creates. Fixes #27681 Change-Id: Idcbec0c989ee96ec066207184611f08818873e8d Reviewed-on: https://go-review.googlesource.com/c/136275 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent abd1dde commit 374546d

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/cmd/go/internal/load/test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ func TestPackagesFor(p *Package, cover *TestCover) (pmain, ptest, pxtest *Packag
227227
}
228228
}
229229

230+
allTestImports := make([]*Package, 0, len(pmain.Internal.Imports)+len(imports)+len(ximports))
231+
allTestImports = append(allTestImports, pmain.Internal.Imports...)
232+
allTestImports = append(allTestImports, imports...)
233+
allTestImports = append(allTestImports, ximports...)
234+
setToolFlags(allTestImports...)
235+
230236
// Do initial scan for metadata needed for writing _testmain.go
231237
// Use that metadata to update the list of imports for package main.
232238
// The list of imports is used by recompileForTest and by the loop

src/cmd/go/testdata/script/gcflags_patterns.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ stderr 'compile.* -p y'
2121
go build -n -v -gcflags=' z1 = -e ' z1
2222
stderr 'compile.* -e .*-p z1'
2323

24+
# -gcflags='all=-N -l' should apply to all packages, even with go test
25+
go test -c -n -gcflags='all=-N -l' z1
26+
stderr 'compile.* -N -l .*-p z3 '
27+
2428
# -ldflags for implicit test package applies to test binary
2529
go test -c -n -gcflags=-N -ldflags=-X=x.y=z z1
2630
stderr 'compile.* -N .*z_test.go'
@@ -58,11 +62,15 @@ import _ "z2"
5862
-- z1/z_test.go --
5963
package z1_test
6064
import "testing"
65+
import _ "z3"
6166
func Test(t *testing.T) {}
6267

6368
-- z2/z.go --
6469
package z2
6570

71+
-- z3/z.go --
72+
package z3
73+
6674
-- y/y.go --
6775
package y
6876

0 commit comments

Comments
 (0)