Skip to content

Commit 27e546b

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent cdac6c2 commit 27e546b

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/cmd/go/internal/list/list.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ a non-nil Error field; other information may or may not be missing
167167
The -export flag causes list to set the Export field to the name of a
168168
file containing up-to-date export information for the given package.
169169
170+
The -find flag causes list to identify the named packages but not
171+
resolve their dependencies: the Imports and Deps lists will be empty.
172+
170173
The -test flag causes list to report not only the named packages
171174
but also their test binaries (for packages with tests), to convey to
172175
source code analysis tools exactly how test binaries are constructed.
@@ -289,6 +292,7 @@ var (
289292
listE = CmdList.Flag.Bool("e", false, "")
290293
listExport = CmdList.Flag.Bool("export", false, "")
291294
listFmt = CmdList.Flag.String("f", "", "")
295+
listFind = CmdList.Flag.Bool("find", false, "")
292296
listJson = CmdList.Flag.Bool("json", false, "")
293297
listM = CmdList.Flag.Bool("m", false, "")
294298
listU = CmdList.Flag.Bool("u", false, "")
@@ -365,6 +369,9 @@ func runList(cmd *base.Command, args []string) {
365369
if *listExport {
366370
base.Fatalf("go list -export cannot be used with -m")
367371
}
372+
if *listFind {
373+
base.Fatalf("go list -find cannot be used with -m")
374+
}
368375
if *listTest {
369376
base.Fatalf("go list -test cannot be used with -m")
370377
}
@@ -397,6 +404,15 @@ func runList(cmd *base.Command, args []string) {
397404
base.Fatalf("go list -versions can only be used with -m")
398405
}
399406

407+
// These pairings make no sense.
408+
if *listFind && *listDeps {
409+
base.Fatalf("go list -deps cannot be used with -find")
410+
}
411+
if *listFind && *listTest {
412+
base.Fatalf("go list -test cannot be used with -find")
413+
}
414+
415+
load.IgnoreImports = *listFind
400416
var pkgs []*load.Package
401417
if *listE {
402418
pkgs = load.PackagesAndErrors(args)

src/cmd/go/internal/load/pkg.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ func (p *Package) copyBuild(pp *build.Package) {
287287
p.XTestImports = pp.XTestImports
288288
if IgnoreImports {
289289
p.Imports = nil
290+
p.Internal.RawImports = nil
290291
p.TestImports = nil
291292
p.XTestImports = nil
292293
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# go list -find should not report imports
2+
3+
go list -f {{.Incomplete}} x/y/z... # should probably exit non-zero but never has
4+
stdout true
5+
go list -find -f '{{.Incomplete}} {{.Imports}}' x/y/z...
6+
stdout '^false \[\]'
7+
8+
-- x/y/z/z.go --
9+
package z
10+
import "does/not/exist"

0 commit comments

Comments
 (0)