1
0
mirror of https://github.com/golang/go synced 2024-11-13 14:50:21 -07:00

cmd/go: ignore implicit imports when the -find flag is set

The documentation of the go list -find flag says that the Deps list will
be empty. However the current implementation adds implicit imports when
supporting Cgo or SWIG and when linking a main package.

Update the documentation of PackageOpts.IgnoreImport to clarify that
both explicit and implicit imports are ignored.

Add a regression test.

Fixes #46092

Change-Id: I37847528d84adb7a18eb6ff29e4af4b4318a66fe
Reviewed-on: https://go-review.googlesource.com/c/go/+/318770
Trust: Jay Conrod <jayconrod@google.com>
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Manlio Perillo 2021-05-11 17:02:20 +02:00 committed by Jay Conrod
parent 9b84814f6e
commit 9995c6b50a
2 changed files with 69 additions and 26 deletions

View File

@ -1797,6 +1797,7 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
}
}
if !opts.IgnoreImports {
// Cgo translation adds imports of "unsafe", "runtime/cgo" and "syscall",
// except for certain packages, to avoid circular dependencies.
if p.UsesCgo() {
@ -1828,6 +1829,7 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
addImport(dep, false)
}
}
}
// Check for case-insensitive collisions of import paths.
fold := str.ToFold(p.ImportPath)
@ -2387,7 +2389,9 @@ func LoadImportWithFlags(path, srcDir string, parent *Package, stk *ImportStack,
// PackageOpts control the behavior of PackagesAndErrors and other package
// loading functions.
type PackageOpts struct {
// IgnoreImports controls whether we ignore imports when loading packages.
// IgnoreImports controls whether we ignore explicit and implicit imports
// when loading packages. Implicit imports are added when supporting Cgo
// or SWIG and when linking main packages.
IgnoreImports bool
// ModResolveTests indicates whether calls to the module loader should also

View File

@ -0,0 +1,39 @@
# Issue #46092
# go list -find should always return a package with an empty Deps list
# The linker loads implicit dependencies
go list -find -f {{.Deps}} ./cmd
stdout '\[\]'
# Cgo translation may add imports of "unsafe", "runtime/cgo" and "syscall"
go list -find -f {{.Deps}} ./cgo
stdout '\[\]'
# SWIG adds imports of some standard packages
go list -find -f {{.Deps}} ./swig
stdout '\[\]'
-- go.mod --
module listfind
-- cmd/main.go --
package main
func main() {}
-- cgo/pkg.go --
package cgopkg
/*
#include <limits.h>
*/
import "C"
func F() {
println(C.INT_MAX)
}
-- swig/pkg.go --
package swigpkg
-- swig/a.swigcxx --