Skip to content

Commit e39e43d

Browse files
aarzillikatiehockman
authored andcommitted
[release-branch.go1.11] 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 #28346 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]> (cherry picked from commit 374546d) Reviewed-on: https://go-review.googlesource.com/c/156377 Run-TryBot: Alessandro Arzilli <[email protected]>
1 parent 8455a84 commit e39e43d

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
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: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@
22

33
# -gcflags=-e applies to named packages, not dependencies
44
go build -n -v -gcflags=-e z1 z2
5-
stderr 'compile.* -e .*-p z1'
6-
stderr 'compile.* -e .*-p z2'
5+
stderr 'compile.* -e.* -p z1'
6+
stderr 'compile.* -e.* -p z2'
77
stderr 'compile.* -p y'
8-
! stderr 'compile.* -e .*-p [^z]'
8+
! stderr 'compile.* -e.* -p [^z]'
99

1010
# -gcflags can specify package=flags, and can be repeated; last match wins
1111
go build -n -v -gcflags=-e -gcflags=z1=-N z1 z2
12-
stderr 'compile.* -N .*-p z1'
13-
! stderr 'compile.* -e .*-p z1'
14-
! stderr 'compile.* -N .*-p z2'
15-
stderr 'compile.* -e .*-p z2'
12+
stderr 'compile.* -N.* -p z1'
13+
! stderr 'compile.* -e.* -p z1'
14+
! stderr 'compile.* -N.* -p z2'
15+
stderr 'compile.* -e.* -p z2'
1616
stderr 'compile.* -p y'
17-
! stderr 'compile.* -e .*-p [^z]'
18-
! stderr 'compile.* -N .*-p [^z]'
17+
! stderr 'compile.* -e.* -p [^z]'
18+
! stderr 'compile.* -N.* -p [^z]'
1919

2020
# -gcflags can have arbitrary spaces around the flags
2121
go build -n -v -gcflags=' z1 = -e ' z1
22-
stderr 'compile.* -e .*-p z1'
22+
stderr 'compile.* -e.* -p z1'
23+
24+
# -gcflags='all=-e' should apply to all packages, even with go test
25+
go test -c -n -gcflags='all=-e' z1
26+
stderr 'compile.* -e.* -p z3 '
2327

2428
# -ldflags for implicit test package applies to test binary
2529
go test -c -n -gcflags=-N -ldflags=-X=x.y=z z1
@@ -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)