]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add go list -deps
authorRuss Cox <rsc@golang.org>
Wed, 18 Apr 2018 15:10:00 +0000 (11:10 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 25 Apr 2018 21:01:04 +0000 (21:01 +0000)
This gives an easy way to query properties of all the deps
of a set of packages, in a single go list invocation.
Go list has already done the hard work of loading these
packages, so exposing them is more efficient than
requiring a second invocation.

This will be helpful for tools asking cmd/go about build
information.

Change-Id: I90798e386246b24aad92dd13cb9e3788c7d30e91
Reviewed-on: https://go-review.googlesource.com/107776
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/alldocs.go
src/cmd/go/go_test.go
src/cmd/go/internal/list/list.go

index 0eb1a072408a26cff38b4834eef0633b1048eae1..2f1108185f1d40d6a64a7e8add6cf6cd50eb0aec 100644 (file)
 //
 // Usage:
 //
-//     go list [-e] [-f format] [-json] [build flags] [packages]
+//     go list [-deps] [-e] [-f format] [-json] [build flags] [packages]
 //
 // List lists the packages named by the import paths, one per line.
 //
 // The -json flag causes the package data to be printed in JSON format
 // instead of using the template format.
 //
+// The -deps flag causes list to iterate over not just the named packages
+// but also all their dependencies. It visits them in a depth-first post-order
+// traversal, so that a package is listed only after all its dependencies.
+//
 // The -e flag changes the handling of erroneous packages, those that
 // cannot be found or are malformed. By default, the list command
 // prints an error to standard error for each erroneous package and
 //         Writes test binary as -c would.
 //
 //     -memprofile mem.out
-//         Write a memory profile to the file after all tests have passed.
+//         Write an allocation profile to the file after all tests have passed.
 //         Writes test binary as -c would.
 //
 //     -memprofilerate n
-//         Enable more precise (and expensive) memory profiles by setting
-//         runtime.MemProfileRate. See 'go doc runtime.MemProfileRate'.
-//         To profile all memory allocations, use -test.memprofilerate=1
-//         and pass --alloc_space flag to the pprof tool.
+//         Enable more precise (and expensive) memory allocation profiles by
+//         setting runtime.MemProfileRate. See 'go doc runtime.MemProfileRate'.
+//         To profile all memory allocations, use -test.memprofilerate=1.
 //
 //     -mutexprofile mutex.out
 //         Write a mutex contention profile to the specified file
index 0fee5e0bc0fb5d0556a1bdc95d042e6acdc50223..f46c7ab1ced141f39408b81334054ddce77e55e5 100644 (file)
@@ -1902,6 +1902,21 @@ func TestGoListDeps(t *testing.T) {
        tg.tempFile("src/p1/p2/p3/p4/p.go", "package p4\n")
        tg.run("list", "-f", "{{.Deps}}", "p1")
        tg.grepStdout("p1/p2/p3/p4", "Deps(p1) does not mention p4")
+
+       tg.run("list", "-deps", "p1")
+       tg.grepStdout("p1/p2/p3/p4", "-deps p1 does not mention p4")
+
+       // Check the list is in dependency order.
+       tg.run("list", "-deps", "math")
+       want := "internal/cpu\nunsafe\nmath\n"
+       out := tg.stdout.String()
+       if !strings.Contains(out, "internal/cpu") {
+               // Some systems don't use internal/cpu.
+               want = "unsafe\nmath\n"
+       }
+       if tg.stdout.String() != want {
+               t.Fatalf("list -deps math: wrong order\nhave %q\nwant %q", tg.stdout.String(), want)
+       }
 }
 
 // Issue 4096. Validate the output of unsuccessful go install foo/quxx.
index 7435273000421cabbe44e4526ebefb2d9e4a9180..842cd9627af2bef4fa353d0fbc717d5f28270113 100644 (file)
@@ -20,7 +20,7 @@ import (
 )
 
 var CmdList = &base.Command{
-       UsageLine: "list [-e] [-f format] [-json] [build flags] [packages]",
+       UsageLine: "list [-deps] [-e] [-f format] [-json] [build flags] [packages]",
        Short:     "list packages",
        Long: `
 List lists the packages named by the import paths, one per line.
@@ -125,6 +125,10 @@ for the go/build package's Context type.
 The -json flag causes the package data to be printed in JSON format
 instead of using the template format.
 
+The -deps flag causes list to iterate over not just the named packages
+but also all their dependencies. It visits them in a depth-first post-order
+traversal, so that a package is listed only after all its dependencies.
+
 The -e flag changes the handling of erroneous packages, those that
 cannot be found or are malformed. By default, the list command
 prints an error to standard error for each erroneous package and
@@ -146,6 +150,7 @@ func init() {
        work.AddBuildFlags(CmdList)
 }
 
+var listDeps = CmdList.Flag.Bool("deps", false, "")
 var listE = CmdList.Flag.Bool("e", false, "")
 var listFmt = CmdList.Flag.String("f", "{{.ImportPath}}", "")
 var listJson = CmdList.Flag.Bool("json", false, "")
@@ -201,6 +206,15 @@ func runList(cmd *base.Command, args []string) {
                pkgs = load.Packages(args)
        }
 
+       if *listDeps {
+               // Note: This changes the order of the listed packages
+               // from "as written on the command line" to
+               // "a depth-first post-order traversal".
+               // (The dependency exploration order for a given node
+               // is alphabetical, same as listed in .Deps.)
+               pkgs = load.PackageList(pkgs)
+       }
+
        // Estimate whether staleness information is needed,
        // since it's a little bit of work to compute.
        needStale := *listJson || strings.Contains(*listFmt, ".Stale")