1
0
mirror of https://github.com/golang/go synced 2024-11-26 16:16:57 -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,35 +1797,37 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
}
}
// Cgo translation adds imports of "unsafe", "runtime/cgo" and "syscall",
// except for certain packages, to avoid circular dependencies.
if p.UsesCgo() {
addImport("unsafe", true)
}
if p.UsesCgo() && (!p.Standard || !cgoExclude[p.ImportPath]) && cfg.BuildContext.Compiler != "gccgo" {
addImport("runtime/cgo", true)
}
if p.UsesCgo() && (!p.Standard || !cgoSyscallExclude[p.ImportPath]) {
addImport("syscall", true)
}
// SWIG adds imports of some standard packages.
if p.UsesSwig() {
addImport("unsafe", true)
if cfg.BuildContext.Compiler != "gccgo" {
if !opts.IgnoreImports {
// Cgo translation adds imports of "unsafe", "runtime/cgo" and "syscall",
// except for certain packages, to avoid circular dependencies.
if p.UsesCgo() {
addImport("unsafe", true)
}
if p.UsesCgo() && (!p.Standard || !cgoExclude[p.ImportPath]) && cfg.BuildContext.Compiler != "gccgo" {
addImport("runtime/cgo", true)
}
addImport("syscall", true)
addImport("sync", true)
if p.UsesCgo() && (!p.Standard || !cgoSyscallExclude[p.ImportPath]) {
addImport("syscall", true)
}
// TODO: The .swig and .swigcxx files can use
// %go_import directives to import other packages.
}
// SWIG adds imports of some standard packages.
if p.UsesSwig() {
addImport("unsafe", true)
if cfg.BuildContext.Compiler != "gccgo" {
addImport("runtime/cgo", true)
}
addImport("syscall", true)
addImport("sync", true)
// The linker loads implicit dependencies.
if p.Name == "main" && !p.Internal.ForceLibrary {
for _, dep := range LinkerDeps(p) {
addImport(dep, false)
// TODO: The .swig and .swigcxx files can use
// %go_import directives to import other packages.
}
// The linker loads implicit dependencies.
if p.Name == "main" && !p.Internal.ForceLibrary {
for _, dep := range LinkerDeps(p) {
addImport(dep, false)
}
}
}
@ -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 --