-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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/gopls: fails to find new std library APIs dynamically #63767
Comments
I'm not sure if it's goimports that's the problem. recording.mp4 |
For me this was only a problem when the I think gopls 0.14.0 is doing something else where it just isn't offering any completions for me at all until I save/run goimports. Once the imports are added, completions work. |
Trying to trace through what happens when I type At first I was using a main module that had some required modules. Pretty sure that got me stdlib Then I changed to an empty module ( That ultimately calls getCandidatePkgs. getCandidatePkgs does start by scanning the stdlib cache (discussed in #63641) which for me does include At that point, trying Here's what I think is happening. imports.GetAllCandidates calls getCandidatePkgs with a static rootFound callback always returning true: rootFound is called when a new root dir (eg GOROOT, GOPATH, module, module cache, etc) is found and it indicates whether the root should be walked for packages. While GetAllCandidates has a static true rootFound, getCandidatePkgs wraps it further: In module mode ModuleResolver.scan ends up being called which does: r.roots for me contains my GOROOT/src, my main module, and my GOMODCACHE. After this filtering it does not have GOROOT/src anymore. If I change rootFound in getCandidatePkgs to always return true (ignoring whether the root type is a GOROOT), my |
@danp thanks very much for that analysis! Sounds about right, and this comment that you found seems to suggest that skipping GOROOT was intentional:
Aside: this is very complicated logic, which has proven to be error-prone and hard to maintain. I've been collecting goimports issues under the |
This is one of the cases where it is not clear how imports should find the right answer. Searching through the source of the standard library is expensive. The existing code contains the state of the standard library when gopls was compiled, but occasionally, as here, that's obsolete. The expense of searching may be worth it if the search succeeds, but searching on every typo would be quite costly. We'll figure it out in the long run, but not immediately. |
Would it be workable to generate something like the bundled stdlib cache on demand and cache it keyed on the Go version in use or similar? I forget if the bundled stdlib cache describes what Go version was used to generate it. If so that could be factored in too (ie if the bundled cache was generated with Go 1.23 and that's the version in use then no need to rebuild it). |
That's one of the possibilities. (It depends on exactly what 'on demand' means.) An alternative would be to store the version-dependent caches on disk and think up a policy for when they are generated and updated. "We'll figure it out in the long run, but not immediately." |
Reminder issue, following up on #63641: it appears that gopls was not finding the new slices package in
GOROOT
until theztdlib.go
index was updated. But according to @heschi this index is meant to just be a cache. We should debug why dynamic scanning of GOROOT was not working.The text was updated successfully, but these errors were encountered: