You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
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.
Motivation
We can currently query
packages.Load
for a package import path withpattern=string
, and a path to a Go file within a package of interest withfile=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 withImportDir
, butx/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 afile=
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.
The text was updated successfully, but these errors were encountered: