Skip to content

Commit 117b1c8

Browse files
committed
cmd/go/internal/work: remove '_test' from import paths in stacktraces when -trimpath is specified
ExampleFrames with -trimpath failed since the content of Frame's File changed when -trimpath is specified. This CL fixes the issue by adding a new field OrigImportPath to PackageInternal, which represents the original import path before adding '_test' suffix for an external test package, and always using it to create paths for the build tools. Fixes #43380 Change-Id: Ibbc947eb3ae08a7ba81f13f03af67c8745b5c69f Reviewed-on: https://go-review.googlesource.com/c/go/+/279440 Run-TryBot: Hajime Hoshi <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Hajime Hoshi <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent c26f954 commit 117b1c8

File tree

6 files changed

+148
-13
lines changed

6 files changed

+148
-13
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ type PackageInternal struct {
207207
BuildInfo string // add this info to package main
208208
TestmainGo *[]byte // content for _testmain.go
209209
Embed map[string][]string // //go:embed comment mapping
210+
OrigImportPath string // original import path before adding '_test' suffix
210211

211212
Asmflags []string // -asmflags for this package
212213
Gcflags []string // -gcflags for this package
@@ -402,6 +403,7 @@ func (p *Package) copyBuild(pp *build.Package) {
402403
p.EmbedPatterns = pp.EmbedPatterns
403404
p.TestEmbedPatterns = pp.TestEmbedPatterns
404405
p.XTestEmbedPatterns = pp.XTestEmbedPatterns
406+
p.Internal.OrigImportPath = pp.ImportPath
405407
}
406408

407409
// A PackageError describes an error loading information about a package.

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ func TestPackagesAndErrors(ctx context.Context, p *Package, cover *TestCover) (p
204204
}
205205
ptest.Internal.Embed = testEmbed
206206
ptest.EmbedFiles = str.StringList(p.EmbedFiles, p.TestEmbedFiles)
207+
ptest.Internal.OrigImportPath = p.Internal.OrigImportPath
207208
ptest.collectDeps()
208209
} else {
209210
ptest = p
@@ -233,11 +234,12 @@ func TestPackagesAndErrors(ctx context.Context, p *Package, cover *TestCover) (p
233234
Imports: ximports,
234235
RawImports: rawXTestImports,
235236

236-
Asmflags: p.Internal.Asmflags,
237-
Gcflags: p.Internal.Gcflags,
238-
Ldflags: p.Internal.Ldflags,
239-
Gccgoflags: p.Internal.Gccgoflags,
240-
Embed: xtestEmbed,
237+
Asmflags: p.Internal.Asmflags,
238+
Gcflags: p.Internal.Gcflags,
239+
Ldflags: p.Internal.Ldflags,
240+
Gccgoflags: p.Internal.Gccgoflags,
241+
Embed: xtestEmbed,
242+
OrigImportPath: p.Internal.OrigImportPath,
241243
},
242244
}
243245
if pxtestNeedsPtest {
@@ -258,12 +260,13 @@ func TestPackagesAndErrors(ctx context.Context, p *Package, cover *TestCover) (p
258260
Module: p.Module,
259261
},
260262
Internal: PackageInternal{
261-
Build: &build.Package{Name: "main"},
262-
BuildInfo: p.Internal.BuildInfo,
263-
Asmflags: p.Internal.Asmflags,
264-
Gcflags: p.Internal.Gcflags,
265-
Ldflags: p.Internal.Ldflags,
266-
Gccgoflags: p.Internal.Gccgoflags,
263+
Build: &build.Package{Name: "main"},
264+
BuildInfo: p.Internal.BuildInfo,
265+
Asmflags: p.Internal.Asmflags,
266+
Gcflags: p.Internal.Gcflags,
267+
Ldflags: p.Internal.Ldflags,
268+
Gccgoflags: p.Internal.Gccgoflags,
269+
OrigImportPath: p.Internal.OrigImportPath,
267270
},
268271
}
269272

src/cmd/go/internal/work/gc.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,11 @@ func (a *Action) trimpath() string {
290290

291291
rewriteDir := a.Package.Dir
292292
if cfg.BuildTrimpath {
293+
importPath := a.Package.Internal.OrigImportPath
293294
if m := a.Package.Module; m != nil && m.Version != "" {
294-
rewriteDir = m.Path + "@" + m.Version + strings.TrimPrefix(a.Package.ImportPath, m.Path)
295+
rewriteDir = m.Path + "@" + m.Version + strings.TrimPrefix(importPath, m.Path)
295296
} else {
296-
rewriteDir = a.Package.ImportPath
297+
rewriteDir = importPath
297298
}
298299
rewrite += a.Package.Dir + "=>" + rewriteDir + ";"
299300
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[short] skip
2+
3+
go test -trimpath -v .
4+
! stdout '[/\\]pkg_test[/\\]'
5+
stdout -count=3 '[/\\]pkg[/\\]'
6+
7+
-- go.mod --
8+
module example.com/pkg
9+
10+
go 1.17
11+
12+
-- pkg.go --
13+
package pkg
14+
15+
import "runtime"
16+
17+
func PrintFile() {
18+
_, file, _, _ := runtime.Caller(0)
19+
println(file)
20+
}
21+
22+
-- pkg_test.go --
23+
package pkg
24+
25+
import "runtime"
26+
27+
func PrintFileForTest() {
28+
_, file, _, _ := runtime.Caller(0)
29+
println(file)
30+
}
31+
32+
-- pkg_x_test.go --
33+
package pkg_test
34+
35+
import (
36+
"runtime"
37+
"testing"
38+
39+
"example.com/pkg"
40+
)
41+
42+
func TestMain(m *testing.M) {
43+
pkg.PrintFile()
44+
pkg.PrintFileForTest()
45+
PrintFileInXTest()
46+
}
47+
48+
func PrintFileInXTest() {
49+
_, file, _, _ := runtime.Caller(0)
50+
println(file)
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[short] skip
2+
3+
go test -trimpath -v .
4+
! stdout '[/\\]pkg_test[/\\]'
5+
stdout -count=2 '[/\\]pkg[/\\]'
6+
7+
-- go.mod --
8+
module example.com/pkg
9+
10+
go 1.17
11+
12+
-- main.go --
13+
package main
14+
15+
import "runtime"
16+
17+
func PrintFile() {
18+
_, file, _, _ := runtime.Caller(0)
19+
println(file)
20+
}
21+
22+
-- main_test.go --
23+
package main
24+
25+
import (
26+
"runtime"
27+
"testing"
28+
)
29+
30+
func PrintFileForTest() {
31+
_, file, _, _ := runtime.Caller(0)
32+
println(file)
33+
}
34+
35+
func TestMain(m *testing.M) {
36+
PrintFile()
37+
PrintFileForTest()
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[short] skip
2+
3+
go test -trimpath -v .
4+
! stdout '[/\\]pkg_test_test[/\\]'
5+
stdout -count=2 '[/\\]pkg_test[/\\]'
6+
7+
-- go.mod --
8+
module example.com/pkg_test
9+
10+
go 1.17
11+
12+
-- pkg.go --
13+
package pkg_test
14+
15+
import "runtime"
16+
17+
func PrintFile() {
18+
_, file, _, _ := runtime.Caller(0)
19+
println(file)
20+
}
21+
22+
-- pkg_x_test.go --
23+
package pkg_test_test
24+
25+
import (
26+
"runtime"
27+
"testing"
28+
29+
"example.com/pkg_test"
30+
)
31+
32+
func PrintFileForTest() {
33+
_, file, _, _ := runtime.Caller(0)
34+
println(file)
35+
}
36+
37+
func TestMain(m *testing.M) {
38+
pkg_test.PrintFile()
39+
PrintFileForTest()
40+
}

0 commit comments

Comments
 (0)