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

x/tools/go/packages: "files=" queries don't seem to work #58726

Open
cespare opened this issue Feb 25, 2023 · 2 comments
Open

x/tools/go/packages: "files=" queries don't seem to work #58726

cespare opened this issue Feb 25, 2023 · 2 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@cespare
Copy link
Contributor

cespare commented Feb 25, 2023

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

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Feb 25, 2023
@gopherbot gopherbot added this to the Unreleased milestone Feb 25, 2023
@cespare
Copy link
Contributor Author

cespare commented Feb 25, 2023

My colleague points out that it works if I add the packages.NeedFiles mode flag. But as far as I can tell, those flags are supposed to control what fields are filled out in the returned packages, not what packages are returned. So it seems like there's a bug in the interaction of files= queries and certain modes.

@dmitshur
Copy link
Contributor

CC @matloob, @golang/tools-team.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants