Skip to content

Commit 8d11b1d

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Michael Matloob <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent dc00dc6 commit 8d11b1d

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/cmd/go/internal/list/list.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,8 +724,18 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
724724

725725
// Record non-identity import mappings in p.ImportMap.
726726
for _, p := range pkgs {
727-
for i, srcPath := range p.Internal.RawImports {
728-
path := p.Imports[i]
727+
nRaw := len(p.Internal.RawImports)
728+
for i, path := range p.Imports {
729+
var srcPath string
730+
if i < nRaw {
731+
srcPath = p.Internal.RawImports[i]
732+
} else {
733+
// This path is not within the raw imports, so it must be an import
734+
// found only within CompiledGoFiles. Those paths are found in
735+
// CompiledImports.
736+
srcPath = p.Internal.CompiledImports[i-nRaw]
737+
}
738+
729739
if path != srcPath {
730740
if p.ImportMap == nil {
731741
p.ImportMap = make(map[string]string)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ type PackageInternal struct {
194194
// Unexported fields are not part of the public API.
195195
Build *build.Package
196196
Imports []*Package // this package's direct imports
197-
CompiledImports []string // additional Imports necessary when using CompiledGoFiles (all from standard library)
198-
RawImports []string // this package's original imports as they appear in the text of the program
197+
CompiledImports []string // additional Imports necessary when using CompiledGoFiles (all from standard library); 1:1 with the end of PackagePublic.Imports
198+
RawImports []string // this package's original imports as they appear in the text of the program; 1:1 with the end of PackagePublic.Imports
199199
ForceLibrary bool // this package is a library (even if named "main")
200200
CmdlineFiles bool // package built from files listed on command line
201201
CmdlinePkg bool // package listed on command line
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Regression test for https://golang.org/issue/46462.
2+
#
3+
# The "runtime/cgo" import found in synthesized .go files (reported in
4+
# the CompiledGoFiles field) should have a corresponding entry in the
5+
# ImportMap field when a runtime/cgo variant (such as a test variant)
6+
# will be used.
7+
8+
[short] skip # -compiled can be slow (because it compiles things)
9+
[!cgo] skip
10+
11+
env CGO_ENABLED=1
12+
env GOFLAGS=-tags=netcgo # Force net to use cgo even on Windows.
13+
14+
15+
# "runtime/cgo [runtime.test]" appears in the the test dependencies of "runtime",
16+
# because "runtime/cgo" itself depends on "runtime"
17+
18+
go list -deps -test -compiled -f '{{if eq .ImportPath "net [runtime.test]"}}{{printf "%q" .Imports}}{{end}}' runtime
19+
20+
# Control case: the explicitly-imported package "sync" is a test variant,
21+
# because "sync" depends on "runtime".
22+
stdout '"sync \[runtime\.test\]"'
23+
! stdout '"sync"'
24+
25+
# Experiment: the implicitly-imported package "runtime/cgo" is also a test variant,
26+
# because "runtime/cgo" also depends on "runtime".
27+
stdout '"runtime/cgo \[runtime\.test\]"'
28+
! stdout '"runtime/cgo"'
29+
30+
31+
# Because the import of "runtime/cgo" in the cgo-generated file actually refers
32+
# to "runtime/cgo [runtime.test]", the latter should be listed in the ImportMap.
33+
# BUG(#46462): Today, it is not.
34+
35+
go list -deps -test -compiled -f '{{if eq .ImportPath "net [runtime.test]"}}{{printf "%q" .ImportMap}}{{end}}' runtime
36+
37+
stdout '"sync":"sync \[runtime\.test\]"' # control
38+
stdout '"runtime/cgo":"runtime/cgo \[runtime\.test\]"' # experiment

0 commit comments

Comments
 (0)