cmd/go: report the imports of CompiledGoFiles in ImportMap

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>
This commit is contained in:
Bryan C. Mills 2021-06-08 15:53:08 -04:00
parent dc00dc6c6b
commit 8d11b1d117
3 changed files with 52 additions and 4 deletions

View 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)

View 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

View file

@ -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