-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Description
What version of Go are you using (go version
)?
go version go1.20.1 linux/amd64
Does this issue reproduce with the latest release?
Yes
What did you do?
x/tools/go/packages is documented as taking a special files=
query to ask for a list of packages containing a set of Go source files. In all my tests, file-based queries always return zero packages.
Here's a small example program: https://github.com/cespare/misc/blob/main/pkgfilesquery/pkgfilesquery.go
If you want to run this, clone the repo and then, from the repo root, run
go run ./pkgfilesquery
What did you expect to see?
packages.Load returned 1 packages
github.com/cespare/misc/columns
What did you see instead?
packages.Load returned 0 packages
I tried a variety of things including absolute paths but I always get 0 packages.
If I use a normal Go list pattern like ./columns
or github.com/cespare/misc/columns
rather than a file=
query, then it works fine.
I traced the inner workings of x/tools/go/packages briefly and it looks like the code initially gets the correct set of packages but no "roots" and then the subsequent "refinement" step filters the package list to zero.
There's also a documentation issue here. The docs say:
The query "file=path/to/file.go" matches the package or packages enclosing the Go source file path/to/file.go. For example "file=~/go/src/fmt/print.go" might return the packages "fmt" and "fmt [fmt.test]".
But (a) it doesn't indicate how the filenames are resolved (are they relative to the cwd or config.Dir?) and (b) it suggests that ~
might work but I don't see any evidence that ~
is implemented in the code.
/cc @matloob, the owner according to s/owners