]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add list -find to find packages but not resolve imports
authorRuss Cox <rsc@golang.org>
Mon, 30 Jul 2018 17:51:35 +0000 (13:51 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 1 Aug 2018 00:35:25 +0000 (00:35 +0000)
This is needed by golang.org/x/tools/go/packages
and also gives a way to do a quicker scan for
packages with a given final path element:

go list -find .../template

Change-Id: I092f4ac5ba7af7d727eb8204379fa436667061b9
Reviewed-on: https://go-review.googlesource.com/126716
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/list/list.go
src/cmd/go/internal/load/pkg.go
src/cmd/go/testdata/script/list_find.txt [new file with mode: 0644]

index 2f671fbe6f0ab961d4e5760b5e0c3590cc5c9617..780916312bacac373ebd8382d19a94afcf7ef628 100644 (file)
@@ -167,6 +167,9 @@ a non-nil Error field; other information may or may not be missing
 The -export flag causes list to set the Export field to the name of a
 file containing up-to-date export information for the given package.
 
+The -find flag causes list to identify the named packages but not
+resolve their dependencies: the Imports and Deps lists will be empty.
+
 The -test flag causes list to report not only the named packages
 but also their test binaries (for packages with tests), to convey to
 source code analysis tools exactly how test binaries are constructed.
@@ -289,6 +292,7 @@ var (
        listE        = CmdList.Flag.Bool("e", false, "")
        listExport   = CmdList.Flag.Bool("export", false, "")
        listFmt      = CmdList.Flag.String("f", "", "")
+       listFind     = CmdList.Flag.Bool("find", false, "")
        listJson     = CmdList.Flag.Bool("json", false, "")
        listM        = CmdList.Flag.Bool("m", false, "")
        listU        = CmdList.Flag.Bool("u", false, "")
@@ -365,6 +369,9 @@ func runList(cmd *base.Command, args []string) {
                if *listExport {
                        base.Fatalf("go list -export cannot be used with -m")
                }
+               if *listFind {
+                       base.Fatalf("go list -find cannot be used with -m")
+               }
                if *listTest {
                        base.Fatalf("go list -test cannot be used with -m")
                }
@@ -397,6 +404,15 @@ func runList(cmd *base.Command, args []string) {
                base.Fatalf("go list -versions can only be used with -m")
        }
 
+       // These pairings make no sense.
+       if *listFind && *listDeps {
+               base.Fatalf("go list -deps cannot be used with -find")
+       }
+       if *listFind && *listTest {
+               base.Fatalf("go list -test cannot be used with -find")
+       }
+
+       load.IgnoreImports = *listFind
        var pkgs []*load.Package
        if *listE {
                pkgs = load.PackagesAndErrors(args)
index 691e8a537b03a413674250cf71095f0addfe688c..b112a4fb9d3bb8ee4647dce28270484520b56db6 100644 (file)
@@ -287,6 +287,7 @@ func (p *Package) copyBuild(pp *build.Package) {
        p.XTestImports = pp.XTestImports
        if IgnoreImports {
                p.Imports = nil
+               p.Internal.RawImports = nil
                p.TestImports = nil
                p.XTestImports = nil
        }
diff --git a/src/cmd/go/testdata/script/list_find.txt b/src/cmd/go/testdata/script/list_find.txt
new file mode 100644 (file)
index 0000000..dbe8fb0
--- /dev/null
@@ -0,0 +1,10 @@
+# go list -find should not report imports
+
+go list -f {{.Incomplete}} x/y/z...  # should probably exit non-zero but never has
+stdout true
+go list -find -f '{{.Incomplete}} {{.Imports}}' x/y/z...
+stdout '^false \[\]'
+
+-- x/y/z/z.go --
+package z
+import "does/not/exist"