]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: report the imports of CompiledGoFiles in ImportMap
authorBryan C. Mills <bcmills@google.com>
Tue, 8 Jun 2021 19:53:08 +0000 (15:53 -0400)
committerBryan C. Mills <bcmills@google.com>
Thu, 10 Jun 2021 19:59:02 +0000 (19:59 +0000)
Ideally we should encode the load.PackageInternal data in a way that
doesn't rely on 1:1 correlations of slices, but this is a minimal fix
to unblock Go 1.17.

Fixes #46462

Change-Id: I6e029c69f757aadc54d4be02c01d6b294c217542
Reviewed-on: https://go-review.googlesource.com/c/go/+/326610
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/go/internal/list/list.go
src/cmd/go/internal/load/pkg.go
src/cmd/go/testdata/script/list_cgo_compiled_importmap.txt [new file with mode: 0644]

index 53aaf311ec4dbced6ba8ed0e491e61ca79dd66cf..7cb9ec6d9492428aa0c9afe111fde89bf35fa536 100644 (file)
@@ -724,8 +724,18 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
 
        // Record non-identity import mappings in p.ImportMap.
        for _, p := range pkgs {
-               for i, srcPath := range p.Internal.RawImports {
-                       path := p.Imports[i]
+               nRaw := len(p.Internal.RawImports)
+               for i, path := range p.Imports {
+                       var srcPath string
+                       if i < nRaw {
+                               srcPath = p.Internal.RawImports[i]
+                       } else {
+                               // This path is not within the raw imports, so it must be an import
+                               // found only within CompiledGoFiles. Those paths are found in
+                               // CompiledImports.
+                               srcPath = p.Internal.CompiledImports[i-nRaw]
+                       }
+
                        if path != srcPath {
                                if p.ImportMap == nil {
                                        p.ImportMap = make(map[string]string)
index 738904865e245d0aea2ba0805024a3d817c51ce9..a83cc9a812b674fe863b19c16273f2f6626042f8 100644 (file)
@@ -194,8 +194,8 @@ type PackageInternal struct {
        // Unexported fields are not part of the public API.
        Build             *build.Package
        Imports           []*Package           // this package's direct imports
-       CompiledImports   []string             // additional Imports necessary when using CompiledGoFiles (all from standard library)
-       RawImports        []string             // this package's original imports as they appear in the text of the program
+       CompiledImports   []string             // additional Imports necessary when using CompiledGoFiles (all from standard library); 1:1 with the end of PackagePublic.Imports
+       RawImports        []string             // this package's original imports as they appear in the text of the program; 1:1 with the end of PackagePublic.Imports
        ForceLibrary      bool                 // this package is a library (even if named "main")
        CmdlineFiles      bool                 // package built from files listed on command line
        CmdlinePkg        bool                 // package listed on command line
diff --git a/src/cmd/go/testdata/script/list_cgo_compiled_importmap.txt b/src/cmd/go/testdata/script/list_cgo_compiled_importmap.txt
new file mode 100644 (file)
index 0000000..3d68ef3
--- /dev/null
@@ -0,0 +1,38 @@
+# Regression test for https://golang.org/issue/46462.
+#
+# The "runtime/cgo" import found in synthesized .go files (reported in
+# the CompiledGoFiles field) should have a corresponding entry in the
+# ImportMap field when a runtime/cgo variant (such as a test variant)
+# will be used.
+
+[short] skip  # -compiled can be slow (because it compiles things)
+[!cgo] skip
+
+env CGO_ENABLED=1
+env GOFLAGS=-tags=netcgo  # Force net to use cgo even on Windows.
+
+
+# "runtime/cgo [runtime.test]" appears in the the test dependencies of "runtime",
+# because "runtime/cgo" itself depends on "runtime"
+
+go list -deps -test -compiled -f '{{if eq .ImportPath "net [runtime.test]"}}{{printf "%q" .Imports}}{{end}}' runtime
+
+       # Control case: the explicitly-imported package "sync" is a test variant,
+       # because "sync" depends on "runtime".
+stdout '"sync \[runtime\.test\]"'
+! stdout '"sync"'
+
+       # Experiment: the implicitly-imported package "runtime/cgo" is also a test variant,
+       # because "runtime/cgo" also depends on "runtime".
+stdout '"runtime/cgo \[runtime\.test\]"'
+! stdout '"runtime/cgo"'
+
+
+# Because the import of "runtime/cgo" in the cgo-generated file actually refers
+# to "runtime/cgo [runtime.test]", the latter should be listed in the ImportMap.
+# BUG(#46462): Today, it is not.
+
+go list -deps -test -compiled -f '{{if eq .ImportPath "net [runtime.test]"}}{{printf "%q" .ImportMap}}{{end}}' runtime
+
+stdout '"sync":"sync \[runtime\.test\]"'                # control
+stdout '"runtime/cgo":"runtime/cgo \[runtime\.test\]"'  # experiment