golang / vscode-go

Go extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=golang.Go
Other
3.89k stars 752 forks source link

gopls: gopls does not find all reference in the workspace #2443

Open zejunlitg opened 2 years ago

zejunlitg commented 2 years ago

What version of Go, VS Code & VS Code Go extension are you using?

go version go1.13.15 linux/amd64

golang.org/x/tools/gopls v0.9.4
    golang.org/x/tools/gopls@v0.9.4 h1:YhHOxVi++ILnY+QnH9FGtRKZZrunSaR7OW8/dCp7bBk=

vscode: Version: 1.70.2 (Universal)

Version Information
* Run `go version` to get version of Go from _the VS Code integrated terminal_. - * Run `gopls -v version` to get version of Gopls from _the VS Code integrated terminal_. - * Run `code -v` or `code-insiders -v` to get version of VS Code or VS Code Insiders. - * Check your installed extensions to get the version of the VS Code Go extension - * Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > `Go: Locate Configured Go Tools` command. -

Share the Go related settings you have added/edited

see below

Run Preferences: Open Settings (JSON) command to open your settings.json file. Share all the settings with the go. or ["go"] or gopls prefixes.

{
    "go.autocompleteUnimportedPackages": true,
    "go.languageServerFlags": [

    ],
    "go.useLanguageServer": true,
    "gopls": {
    },
}

Describe the bug

Bug description: Gopls only finds the references on package level, not on module level.
References in other packages can be found/peeked ONLY AFTER I opened certain package and see the reference,
which is useless, because the point of finding all references is it finds all references for me without needing to open all the packages.

What I expect: Find all reference finds all references of a function on a module level, it should search all the packages in this module.

Steps to reproduce the behavior:

Use Find all references all Peek references feature on any function in a Go project using go module mode, this happens.

Screenshots or recordings

N/A

adonovan commented 2 years ago

@adonovan

findleyr commented 2 years ago

Gopls only finds the references on package level, not on module level.

FWIW, this is not what I observe, and is certainly not the intended behavior. Gopls should find all references within any workspace packages. In other words, if opening up a module root, gopls should find all references contained in any package inside the module. (golang/go#43144 catalogs a known bug about references in test variants, but that doesn't seem to be what is reported here).

It may be that somehow gopls is failing to compute workspace packages correctly. A more specific reproducer would be helpful.

zejunlitg commented 2 years ago

@findleyr Hi, Does Find all references only consider opened packages? Is this the expected behavior? The global search feature of vscode can find all function reference just fine, so I'm curious.

findleyr commented 2 years ago

No, find all references should consider all workspace packages. If you do not see this happening, it is either a bug or a misconfiguration (or both). If you had a small reproducer, or could share LSP logs, it would help us understand what is going on in your session.

zejunlitg commented 2 years ago

No, find all references should consider all workspace packages. If you do not see this happening, it is either a bug or a misconfiguration (or both). If you had a small reproducer, or could share LSP logs, it would help us understand what is going on in your session.

I don't really have a minimal reproducer, but I do have a workspace error, could that be related?

Also, where do I find LSP logs, where are they located?

findleyr commented 2 years ago

I don't really have a minimal reproducer, but I do have a workspace error, could that be related?

Yes, that is almost certainly related. What is your workspace error?

Instructions for how to collect logs are here: https://github.com/golang/vscode-go/blob/master/docs/troubleshooting.md#collect-gopls-information

zejunlitg commented 2 years ago

I don't really have a minimal reproducer, but I do have a workspace error, could that be related?

Yes, that is almost certainly related. What is your workspace error?

Instructions for how to collect logs are here: https://github.com/golang/vscode-go/blob/master/docs/troubleshooting.md#collect-gopls-information

err: exit status 1: stderr: build github.com/...: cannot load github.com/...: import lookup disabled by -mod=readonly
: packages.Load errorgo list

Just created another playground using go module and the find all references seem to work as expected, but my actual working repo is still not working

zejunlitg commented 2 years ago

I don't really have a minimal reproducer, but I do have a workspace error, could that be related?

Yes, that is almost certainly related. What is your workspace error?

Instructions for how to collect logs are here: https://github.com/golang/vscode-go/blob/master/docs/troubleshooting.md#collect-gopls-information

[Trace - 23:56:38.654 PM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2022/09/08 23:56:38 go/packages.Load #2: updating metadata for 593 packages\n"}

if a function in package A has been referenced in both package B and C, if I open a file where it referenced this function in package B, then Find all references can find all references in package B, but still missing all the references in package C. This is the only way I discovered to make Find all references actually find references in other packages.

findleyr commented 2 years ago

This error is surprising: cannot load github.com/....

Do you have a module path named "github.com" in your workspace?

zejunlitg commented 2 years ago

This error is surprising: cannot load github.com/....

Do you have a module path named "github.com" in your workspace?

Yes, we're using module mod, this is some package inside our module, I can confirm 'Find all reference' only works after I open certain packages, I deliberately leave only one file open and when the packages are loaded(except for this workspace error), find all reference only finds itself, but after I open other packages where this function is referenced, those references are found as well.

findleyr commented 2 years ago

Yes, we're using module mod

Sorry, I don't understand. Do you have a go.mod file containing module github.com somewhere?

zejunlitg commented 2 years ago

Yes, we're using module mod

Sorry, I don't understand. Do you have a go.mod file containing module github.com somewhere?

I do, there is a go.mod file, it's in the root directory of this repo.

findleyr commented 2 years ago

Gopls is having trouble loading your workspace. Does go list ./... from your repo root succeed for you?

Are you able to share the full logs from a gopls session?

zejunlitg commented 2 years ago

Gopls is having trouble loading your workspace. Does go list ./... from your repo root succeed for you?

Are you able to share the full logs from a gopls session?

It seems so, go list ./... does work, I can't share the full logs as this is NDA codebase. For now I simply don't use Find all references as its behavior is not as expected, I just use global search.

chezgi commented 1 year ago

this happens for me also. smaple project structure:

testgo.code-workspace:

{
  "folders": [
    {
      "path": "moda"
    },
    {
      "path": "modb"
    }
  ],
}

moda/go.mod

module github.com/test/moda
go 1.19

moda/a.go

package moda
import "fmt"
func TestFunc1() {
    fmt.Println("test")
}

modb/go.mod

module github.com/test/modb
go 1.19
replace github.com/test/moda => ../moda/
require github.com/test/moda v0.0.0-00010101000000-000000000000

modb/main.go

package main
import "github.com/test/moda"
func main() {
    moda.TestFunc1()
}

when doing "Find All References" in moda it cant find modb references, but if i use "Find All References" in modb if finds all references.

roychoo commented 10 months ago

have the same issue here. is there any workaround?

antonis19 commented 3 months ago

Try the following commands from your workspace directory:

go work init
go work use .

This worked for me.