golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.98k stars 17.67k forks source link

x/tools/gopls: go to definition within imported modules #66827

Open rustyx opened 6 months ago

rustyx commented 6 months ago

Consider a Go project with main.go looking like this:

package main

import (
    "fmt"

    "github.com/google/uuid"
)

func main() {
    id := uuid.New()
    fmt.Println(id)
}

When I go to definition (F12) of uuid.New() I end up here:

func New() UUID {
    return Must(NewRandom())
}

So far so good.

But when I try to navigate futher to Must or NewRandom, I get a popup "No definition found...".

Whereas if I navigate to fmt.Println, I can navigate further into Fprintln, doPrintln, writeByte, etc.

Why is this difference in behavior between Go SDK packages and imported modules?

Shouldn't I be able to navigate within modules the same way I do in my own code and the Go SDK?

gopls version: v0.15.2

go version: 1.22

The full test project can be found at https://github.com/rustyx/issue-66827

rustyx commented 6 months ago

I wonder if this is related to #53004.

ansaba commented 6 months ago

cc: @findleyr

findleyr commented 6 months ago

Sorry, but this didn't reproduce for me, with either VS Code or coc.nvim. (EDIT: to be clear: both of my test clients behaved as expected, and I could navigate to Must, NewRandom, etc)

Which LSP client are you using? Can you please also share the output of gopls -v version?

rustyx commented 6 months ago

I have the latest gopls.

gopls -v version ```none Build info ---------- golang.org/x/tools/gopls v0.15.3 golang.org/x/tools/gopls@v0.15.3 h1:zbdOidFrPTc8Bx0YrN5QKgJ0zCjyGi0L27sKQ/bDG5o= github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/google/go-cmp@v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y= golang.org/x/mod@v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= golang.org/x/sync@v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/telemetry@v0.0.0-20240209200032-7b892fcb8a78 h1:vcVnuftN4J4UKLRcgetjzfU9FjjgXUUYUc3JhFplgV4= golang.org/x/text@v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/tools@v0.18.1-0.20240412183611-d92ae0781217 h1:uH9jJYgeLCvblH0S+03kFO0qUDxRkbLRLFiKVVDl7ak= golang.org/x/vuln@v1.0.1 h1:KUas02EjQK5LTuIx1OylBQdKKZ9jeugs+HiqO5HormU= honnef.co/go/tools@v0.4.6 h1:oFEHCKeID7to/3autwsWfnuv69j3NsfcXbvJKuIcep8= mvdan.cc/gofumpt@v0.6.0 h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo= mvdan.cc/xurls/v2@v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8= go: go1.22.0 ```

Apologies for the confusion, indeed the issue actually arises only after I add the go/pkg/mod folder to the current workspace.

Try these Steps to reproduce:

rustyx commented 6 months ago

@findleyr LSP client is VSCode (v1.88.1) running on Ubuntu Linux 22.04 amd64. Again apologies for not mentioning it upfront. I think the issue is specific to VSCode and having go/pkg/mod mapped as a project in the current workspace.

My current LSP session state looks like this:

Session 1

From: Cache 1

Views

ID: 1 Type: GoModView Root: file:///home/user/src/issue-66827 Folder: issue-66827:file:///home/user/src/issue-66827 ID: 2 Type: AdHocView Root: file:///home/user/go/pkg/mod Folder: mod:file:///home/user/go/pkg/mod ID: 4 Type: GoModView Root: file:///home/user/go/pkg/mod/github.com/google/uuid@v1.6.0 Folder: mod:file:///home/user/go/pkg/mod

Overlays

file:///home/user/go/pkg/mod/github.com/google/uuid@v1.6.0/version4.go file:///home/user/src/issue-66827/main.go

ansaba commented 6 months ago

This issue is reproducible only when $HOME/go/pkg/mod/ is added to the workspace. Seems like gopls doesn't processes the module cache and it may not be the recommended way to navigate function definition.

findleyr commented 6 months ago

Indeed, I'll check that we're handling this correctly.

Since this is unlikely to affect a large number of users, I'll move this to a future release milestone.