]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: ignore implicit imports when the -find flag is set
authorManlio Perillo <manlio.perillo@gmail.com>
Tue, 11 May 2021 15:02:20 +0000 (17:02 +0200)
committerJay Conrod <jayconrod@google.com>
Tue, 11 May 2021 21:22:08 +0000 (21:22 +0000)
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>
src/cmd/go/internal/load/pkg.go
src/cmd/go/testdata/script/list_find_nodeps.txt [new file with mode: 0644]

index 3c7cd44ee33e7e1d081bd0caed093211d31fcdd2..a3b96702ce52863c76fec052b8873f9c8b14bf2d 100644 (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
diff --git a/src/cmd/go/testdata/script/list_find_nodeps.txt b/src/cmd/go/testdata/script/list_find_nodeps.txt
new file mode 100644 (file)
index 0000000..55f98f6
--- /dev/null
@@ -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 --