Commit 27e546be authored by Russ Cox's avatar Russ Cox

cmd/go: add list -find to find packages but not resolve imports

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: 's avatarBryan C. Mills <bcmills@google.com>
parent cdac6c22
...@@ -167,6 +167,9 @@ a non-nil Error field; other information may or may not be missing ...@@ -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 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. 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 The -test flag causes list to report not only the named packages
but also their test binaries (for packages with tests), to convey to but also their test binaries (for packages with tests), to convey to
source code analysis tools exactly how test binaries are constructed. source code analysis tools exactly how test binaries are constructed.
...@@ -289,6 +292,7 @@ var ( ...@@ -289,6 +292,7 @@ var (
listE = CmdList.Flag.Bool("e", false, "") listE = CmdList.Flag.Bool("e", false, "")
listExport = CmdList.Flag.Bool("export", false, "") listExport = CmdList.Flag.Bool("export", false, "")
listFmt = CmdList.Flag.String("f", "", "") listFmt = CmdList.Flag.String("f", "", "")
listFind = CmdList.Flag.Bool("find", false, "")
listJson = CmdList.Flag.Bool("json", false, "") listJson = CmdList.Flag.Bool("json", false, "")
listM = CmdList.Flag.Bool("m", false, "") listM = CmdList.Flag.Bool("m", false, "")
listU = CmdList.Flag.Bool("u", false, "") listU = CmdList.Flag.Bool("u", false, "")
...@@ -365,6 +369,9 @@ func runList(cmd *base.Command, args []string) { ...@@ -365,6 +369,9 @@ func runList(cmd *base.Command, args []string) {
if *listExport { if *listExport {
base.Fatalf("go list -export cannot be used with -m") base.Fatalf("go list -export cannot be used with -m")
} }
if *listFind {
base.Fatalf("go list -find cannot be used with -m")
}
if *listTest { if *listTest {
base.Fatalf("go list -test cannot be used with -m") base.Fatalf("go list -test cannot be used with -m")
} }
...@@ -397,6 +404,15 @@ func runList(cmd *base.Command, args []string) { ...@@ -397,6 +404,15 @@ func runList(cmd *base.Command, args []string) {
base.Fatalf("go list -versions can only be used with -m") 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 var pkgs []*load.Package
if *listE { if *listE {
pkgs = load.PackagesAndErrors(args) pkgs = load.PackagesAndErrors(args)
......
...@@ -287,6 +287,7 @@ func (p *Package) copyBuild(pp *build.Package) { ...@@ -287,6 +287,7 @@ func (p *Package) copyBuild(pp *build.Package) {
p.XTestImports = pp.XTestImports p.XTestImports = pp.XTestImports
if IgnoreImports { if IgnoreImports {
p.Imports = nil p.Imports = nil
p.Internal.RawImports = nil
p.TestImports = nil p.TestImports = nil
p.XTestImports = nil p.XTestImports = nil
} }
......
# 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"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment