Skip to content

proposal: x/tools/go/packages: support a "dir=" query #42802

@jpap

Description

@jpap

Motivation

We can currently query packages.Load for a package import path with pattern=string, and a path to a Go file within a package of interest with file=path/to/file.go. It would be very useful to be able to query for a package given its directory.

go/build can do this very easily with ImportDir, but x/tools/go/packages lacks the equivalent.

Proposal

A new query "dir=path/to/package" matches the package or packages in the directory path/to/package. For example "dir=~/go/src/fmt" might return the packages "fmt" and "fmt [fmt.test]".

Workaround

Use filepath.Glob to query for all *.go files in a given directory, pick the first one, then perform a file= query. This has much more overhead due to the glob, and is very undesirable when performing a large recursive package import traversal.

Thanks for your consideration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions