Skip to content

Commit

Permalink
cmd/go: add go list -deps
Browse files Browse the repository at this point in the history
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 <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Bryan C. Mills <[email protected]>
  • Loading branch information
rsc committed Apr 25, 2018
1 parent 9c9ed9a commit 90e860f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/cmd/go/alldocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@
//
// 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.
//
Expand Down Expand Up @@ -683,6 +683,10 @@
// 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
Expand Down Expand Up @@ -1688,14 +1692,13 @@
// 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
Expand Down
15 changes: 15 additions & 0 deletions src/cmd/go/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion src/cmd/go/internal/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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, "")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 90e860f

Please sign in to comment.