From 27e546be86fbe98e0c19ad8a59186582cbbdff53 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 30 Jul 2018 13:51:35 -0400 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Bryan C. Mills --- src/cmd/go/internal/list/list.go | 16 ++++++++++++++++ src/cmd/go/internal/load/pkg.go | 1 + src/cmd/go/testdata/script/list_find.txt | 10 ++++++++++ 3 files changed, 27 insertions(+) create mode 100644 src/cmd/go/testdata/script/list_find.txt diff --git a/src/cmd/go/internal/list/list.go b/src/cmd/go/internal/list/list.go index 2f671fbe6f..780916312b 100644 --- a/src/cmd/go/internal/list/list.go +++ b/src/cmd/go/internal/list/list.go @@ -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) diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 691e8a537b..b112a4fb9d 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -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 index 0000000000..dbe8fb0ac9 --- /dev/null +++ b/src/cmd/go/testdata/script/list_find.txt @@ -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"