mirror of
https://github.com/golang/go
synced 2024-09-15 22:20:06 +00:00
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 golang/go#43380 Change-Id: Ibbc947eb3ae08a7ba81f13f03af67c8745b5c69f Reviewed-on: https://go-review.googlesource.com/c/go/+/279440 Run-TryBot: Hajime Hoshi <hajimehoshi@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Hajime Hoshi <hajimehoshi@gmail.com> Reviewed-by: Jay Conrod <jayconrod@google.com> Reviewed-by: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
parent
c26f954a54
commit
117b1c84d3
|
@ -207,6 +207,7 @@ type PackageInternal struct {
|
|||
BuildInfo string // add this info to package main
|
||||
TestmainGo *[]byte // content for _testmain.go
|
||||
Embed map[string][]string // //go:embed comment mapping
|
||||
OrigImportPath string // original import path before adding '_test' suffix
|
||||
|
||||
Asmflags []string // -asmflags for this package
|
||||
Gcflags []string // -gcflags for this package
|
||||
|
@ -402,6 +403,7 @@ func (p *Package) copyBuild(pp *build.Package) {
|
|||
p.EmbedPatterns = pp.EmbedPatterns
|
||||
p.TestEmbedPatterns = pp.TestEmbedPatterns
|
||||
p.XTestEmbedPatterns = pp.XTestEmbedPatterns
|
||||
p.Internal.OrigImportPath = pp.ImportPath
|
||||
}
|
||||
|
||||
// A PackageError describes an error loading information about a package.
|
||||
|
|
|
@ -204,6 +204,7 @@ func TestPackagesAndErrors(ctx context.Context, p *Package, cover *TestCover) (p
|
|||
}
|
||||
ptest.Internal.Embed = testEmbed
|
||||
ptest.EmbedFiles = str.StringList(p.EmbedFiles, p.TestEmbedFiles)
|
||||
ptest.Internal.OrigImportPath = p.Internal.OrigImportPath
|
||||
ptest.collectDeps()
|
||||
} else {
|
||||
ptest = p
|
||||
|
@ -233,11 +234,12 @@ func TestPackagesAndErrors(ctx context.Context, p *Package, cover *TestCover) (p
|
|||
Imports: ximports,
|
||||
RawImports: rawXTestImports,
|
||||
|
||||
Asmflags: p.Internal.Asmflags,
|
||||
Gcflags: p.Internal.Gcflags,
|
||||
Ldflags: p.Internal.Ldflags,
|
||||
Gccgoflags: p.Internal.Gccgoflags,
|
||||
Embed: xtestEmbed,
|
||||
Asmflags: p.Internal.Asmflags,
|
||||
Gcflags: p.Internal.Gcflags,
|
||||
Ldflags: p.Internal.Ldflags,
|
||||
Gccgoflags: p.Internal.Gccgoflags,
|
||||
Embed: xtestEmbed,
|
||||
OrigImportPath: p.Internal.OrigImportPath,
|
||||
},
|
||||
}
|
||||
if pxtestNeedsPtest {
|
||||
|
@ -258,12 +260,13 @@ func TestPackagesAndErrors(ctx context.Context, p *Package, cover *TestCover) (p
|
|||
Module: p.Module,
|
||||
},
|
||||
Internal: PackageInternal{
|
||||
Build: &build.Package{Name: "main"},
|
||||
BuildInfo: p.Internal.BuildInfo,
|
||||
Asmflags: p.Internal.Asmflags,
|
||||
Gcflags: p.Internal.Gcflags,
|
||||
Ldflags: p.Internal.Ldflags,
|
||||
Gccgoflags: p.Internal.Gccgoflags,
|
||||
Build: &build.Package{Name: "main"},
|
||||
BuildInfo: p.Internal.BuildInfo,
|
||||
Asmflags: p.Internal.Asmflags,
|
||||
Gcflags: p.Internal.Gcflags,
|
||||
Ldflags: p.Internal.Ldflags,
|
||||
Gccgoflags: p.Internal.Gccgoflags,
|
||||
OrigImportPath: p.Internal.OrigImportPath,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -290,10 +290,11 @@ func (a *Action) trimpath() string {
|
|||
|
||||
rewriteDir := a.Package.Dir
|
||||
if cfg.BuildTrimpath {
|
||||
importPath := a.Package.Internal.OrigImportPath
|
||||
if m := a.Package.Module; m != nil && m.Version != "" {
|
||||
rewriteDir = m.Path + "@" + m.Version + strings.TrimPrefix(a.Package.ImportPath, m.Path)
|
||||
rewriteDir = m.Path + "@" + m.Version + strings.TrimPrefix(importPath, m.Path)
|
||||
} else {
|
||||
rewriteDir = a.Package.ImportPath
|
||||
rewriteDir = importPath
|
||||
}
|
||||
rewrite += a.Package.Dir + "=>" + rewriteDir + ";"
|
||||
}
|
||||
|
|
51
src/cmd/go/testdata/script/test_trimpath.txt
vendored
Normal file
51
src/cmd/go/testdata/script/test_trimpath.txt
vendored
Normal file
|
@ -0,0 +1,51 @@
|
|||
[short] skip
|
||||
|
||||
go test -trimpath -v .
|
||||
! stdout '[/\\]pkg_test[/\\]'
|
||||
stdout -count=3 '[/\\]pkg[/\\]'
|
||||
|
||||
-- go.mod --
|
||||
module example.com/pkg
|
||||
|
||||
go 1.17
|
||||
|
||||
-- pkg.go --
|
||||
package pkg
|
||||
|
||||
import "runtime"
|
||||
|
||||
func PrintFile() {
|
||||
_, file, _, _ := runtime.Caller(0)
|
||||
println(file)
|
||||
}
|
||||
|
||||
-- pkg_test.go --
|
||||
package pkg
|
||||
|
||||
import "runtime"
|
||||
|
||||
func PrintFileForTest() {
|
||||
_, file, _, _ := runtime.Caller(0)
|
||||
println(file)
|
||||
}
|
||||
|
||||
-- pkg_x_test.go --
|
||||
package pkg_test
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"example.com/pkg"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
pkg.PrintFile()
|
||||
pkg.PrintFileForTest()
|
||||
PrintFileInXTest()
|
||||
}
|
||||
|
||||
func PrintFileInXTest() {
|
||||
_, file, _, _ := runtime.Caller(0)
|
||||
println(file)
|
||||
}
|
38
src/cmd/go/testdata/script/test_trimpath_main.txt
vendored
Normal file
38
src/cmd/go/testdata/script/test_trimpath_main.txt
vendored
Normal file
|
@ -0,0 +1,38 @@
|
|||
[short] skip
|
||||
|
||||
go test -trimpath -v .
|
||||
! stdout '[/\\]pkg_test[/\\]'
|
||||
stdout -count=2 '[/\\]pkg[/\\]'
|
||||
|
||||
-- go.mod --
|
||||
module example.com/pkg
|
||||
|
||||
go 1.17
|
||||
|
||||
-- main.go --
|
||||
package main
|
||||
|
||||
import "runtime"
|
||||
|
||||
func PrintFile() {
|
||||
_, file, _, _ := runtime.Caller(0)
|
||||
println(file)
|
||||
}
|
||||
|
||||
-- main_test.go --
|
||||
package main
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func PrintFileForTest() {
|
||||
_, file, _, _ := runtime.Caller(0)
|
||||
println(file)
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
PrintFile()
|
||||
PrintFileForTest()
|
||||
}
|
40
src/cmd/go/testdata/script/test_trimpath_test_suffix.txt
vendored
Normal file
40
src/cmd/go/testdata/script/test_trimpath_test_suffix.txt
vendored
Normal file
|
@ -0,0 +1,40 @@
|
|||
[short] skip
|
||||
|
||||
go test -trimpath -v .
|
||||
! stdout '[/\\]pkg_test_test[/\\]'
|
||||
stdout -count=2 '[/\\]pkg_test[/\\]'
|
||||
|
||||
-- go.mod --
|
||||
module example.com/pkg_test
|
||||
|
||||
go 1.17
|
||||
|
||||
-- pkg.go --
|
||||
package pkg_test
|
||||
|
||||
import "runtime"
|
||||
|
||||
func PrintFile() {
|
||||
_, file, _, _ := runtime.Caller(0)
|
||||
println(file)
|
||||
}
|
||||
|
||||
-- pkg_x_test.go --
|
||||
package pkg_test_test
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"example.com/pkg_test"
|
||||
)
|
||||
|
||||
func PrintFileForTest() {
|
||||
_, file, _, _ := runtime.Caller(0)
|
||||
println(file)
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
pkg_test.PrintFile()
|
||||
PrintFileForTest()
|
||||
}
|
Loading…
Reference in a new issue