]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/work: remove '_test' from import paths in stacktraces when -trimpath...
authorHajime Hoshi <hajimehoshi@gmail.com>
Fri, 25 Dec 2020 17:25:13 +0000 (02:25 +0900)
committerHajime Hoshi <hajimehoshi@gmail.com>
Mon, 12 Apr 2021 15:17:33 +0000 (15:17 +0000)
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>
src/cmd/go/internal/load/pkg.go
src/cmd/go/internal/load/test.go
src/cmd/go/internal/work/gc.go
src/cmd/go/testdata/script/test_trimpath.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_trimpath_main.txt [new file with mode: 0644]
src/cmd/go/testdata/script/test_trimpath_test_suffix.txt [new file with mode: 0644]

index a6d730d0d82e9e5e6ded1219955222d6668bf989..00c50bf38f1afbb6b4215318a6526e32be781aaf 100644 (file)
@@ -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.
index fc89bd0250b4b438d472ac186d8c8160e9b4c435..9385f4118214c139bd439af25d885c4155454b02 100644 (file)
@@ -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,
                },
        }
 
index 7ccba4a56155809490696794f7a5914cc38c2801..60d0b29a21ed0eae046f67cd410696594431a2b0 100644 (file)
@@ -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 + ";"
        }
diff --git a/src/cmd/go/testdata/script/test_trimpath.txt b/src/cmd/go/testdata/script/test_trimpath.txt
new file mode 100644 (file)
index 0000000..065f9ce
--- /dev/null
@@ -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)
+}
diff --git a/src/cmd/go/testdata/script/test_trimpath_main.txt b/src/cmd/go/testdata/script/test_trimpath_main.txt
new file mode 100644 (file)
index 0000000..c076212
--- /dev/null
@@ -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()
+}
diff --git a/src/cmd/go/testdata/script/test_trimpath_test_suffix.txt b/src/cmd/go/testdata/script/test_trimpath_test_suffix.txt
new file mode 100644 (file)
index 0000000..6cbad83
--- /dev/null
@@ -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()
+}