mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00: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:
parent
9b84814f6e
commit
9995c6b50a
2 changed files with 69 additions and 26 deletions
|
|
@ -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",
|
if !opts.IgnoreImports {
|
||||||
// except for certain packages, to avoid circular dependencies.
|
// Cgo translation adds imports of "unsafe", "runtime/cgo" and "syscall",
|
||||||
if p.UsesCgo() {
|
// except for certain packages, to avoid circular dependencies.
|
||||||
addImport("unsafe", true)
|
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 || !cgoExclude[p.ImportPath]) && cfg.BuildContext.Compiler != "gccgo" {
|
||||||
}
|
|
||||||
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" {
|
|
||||||
addImport("runtime/cgo", true)
|
addImport("runtime/cgo", true)
|
||||||
}
|
}
|
||||||
addImport("syscall", true)
|
if p.UsesCgo() && (!p.Standard || !cgoSyscallExclude[p.ImportPath]) {
|
||||||
addImport("sync", true)
|
addImport("syscall", true)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: The .swig and .swigcxx files can use
|
// SWIG adds imports of some standard packages.
|
||||||
// %go_import directives to import other 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.
|
// TODO: The .swig and .swigcxx files can use
|
||||||
if p.Name == "main" && !p.Internal.ForceLibrary {
|
// %go_import directives to import other packages.
|
||||||
for _, dep := range LinkerDeps(p) {
|
}
|
||||||
addImport(dep, false)
|
|
||||||
|
// 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
|
// PackageOpts control the behavior of PackagesAndErrors and other package
|
||||||
// loading functions.
|
// loading functions.
|
||||||
type PackageOpts struct {
|
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
|
IgnoreImports bool
|
||||||
|
|
||||||
// ModResolveTests indicates whether calls to the module loader should also
|
// ModResolveTests indicates whether calls to the module loader should also
|
||||||
|
|
|
||||||
39
src/cmd/go/testdata/script/list_find_nodeps.txt
vendored
Normal file
39
src/cmd/go/testdata/script/list_find_nodeps.txt
vendored
Normal 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 --
|
||||||
Loading…
Add table
Add a link
Reference in a new issue