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

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

Open
zejunlitg opened this issue Sep 8, 2022 · 17 comments
Open

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

zejunlitg opened this issue Sep 8, 2022 · 17 comments
Labels
gopls gopls related issues NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@zejunlitg
Copy link

zejunlitg commented Sep 8, 2022

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

@gopherbot gopherbot added this to the Untriaged milestone Sep 8, 2022
@adonovan adonovan added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 8, 2022
@adonovan
Copy link
Member

adonovan commented Sep 8, 2022

@adonovan

@findleyr
Copy link
Contributor

findleyr commented Sep 8, 2022

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
Copy link
Author

@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
Copy link
Contributor

findleyr commented Sep 8, 2022

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
Copy link
Author

zejunlitg commented Sep 8, 2022

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
Copy link
Contributor

findleyr commented Sep 8, 2022

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
Copy link
Author

zejunlitg commented Sep 8, 2022

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
Copy link
Author

zejunlitg commented Sep 8, 2022

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
Copy link
Contributor

findleyr commented Sep 9, 2022

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

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

@zejunlitg
Copy link
Author

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
Copy link
Contributor

findleyr commented Sep 9, 2022

Yes, we're using module mod

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

@zejunlitg
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

zejunlitg commented Sep 20, 2022

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.

@suzmue suzmue modified the milestones: Untriaged, vscode-go/later Oct 3, 2022
@suzmue suzmue added the gopls gopls related issues label Oct 3, 2022
@chezgi
Copy link

chezgi commented Jan 13, 2023

this happens for me also.
smaple project structure:

  • testgo.code-workspace
  • moda/[ go.mod, a.go ]
  • modb/[ go.mod, main.go]

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
Copy link

roychoo commented Dec 23, 2023

have the same issue here. is there any workaround?

@antonis19
Copy link

Try the following commands from your workspace directory:

go work init
go work use .

This worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls gopls related issues NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

8 participants