Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jpap opened this issue Nov 24, 2020 · 2 comments
Open

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

jpap opened this issue Nov 24, 2020 · 2 comments

Comments

@jpap
Copy link
Contributor

jpap commented Nov 24, 2020

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.

@gopherbot gopherbot added this to the Proposal milestone Nov 24, 2020
@mvdan
Copy link
Member

mvdan commented Nov 24, 2020

Can you clarify what the specific use case is here? Personally, I've never needed to build an absolute path to a directory; I've always used the implicit . as in go build, or relative patterns as in go build ./foo.

@jpap
Copy link
Contributor Author

jpap commented Nov 24, 2020

I've refactored my recursive scan, keying on the package import path instead of the package dir, which means the directory lookup is only required at the start to derive the top-level package import path. Based on your comment, I've realized that I could specify the package directory in Config.Dir and use . as the pattern: that works, thank you!

The somewhat vague description in the GoDoc "Load passes most patterns directly to the underlying build tool..." did not make it obvious to me that we could use a . pattern to locate a package.

As an alternative to the above proposal, perhaps the GoDoc can be enhanced to mention . patterns (and their relative variants), perhaps going further to mention that the Config.Dir can be used with . to locate a package by directory as well.

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Jan 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

3 participants