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/gopls: "Go to definition" goes to the object after the period when the cursor is at the end of a variable #47408

Closed
jsgoupil opened this issue Jul 26, 2021 · 5 comments
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@jsgoupil
Copy link

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

  • Run go version to get version of Go from the VS Code integrated terminal.
    • go version go1.16.5 windows/amd64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
Build info
----------
golang.org/x/tools/gopls v0.7.0
    golang.org/x/tools/gopls@v0.7.0 h1:JQBHW81Gsyim6iDjUwGoPeSpXrSqwen3isPJLfDfaYU=
    github.com/BurntSushi/toml@v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
    github.com/google/go-cmp@v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/mod@v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
    golang.org/x/sync@v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=   
    golang.org/x/sys@v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=    
    golang.org/x/tools@v0.1.3-0.20210608163600-9ed039809d4c h1:Pv9gNyJFYVdpUAVZYJ1BDSU4eGgXQ+0f3DIGAdolO5s=
    golang.org/x/xerrors@v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
    honnef.co/go/tools@v0.2.0 h1:ws8AfbgTX3oIczLPNPCu5166oBg9ST2vNs0rcht+mDE=
    mvdan.cc/gofumpt@v0.1.1 h1:bi/1aS/5W00E2ny5q65w9SnKpWEF/UIOqDYBILpo9rA=
    mvdan.cc/xurls/v2@v2.2.0 h1:NSZPykBXJFCetGZykLAxaL6SIpvbVy/UFEniIfHAa8A=
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.

1.58.2
c3f126316369cd610563c75b1b1725e0679adfb3
x64
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.26.0
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
Checking configured tools....
GOBIN: undefined
toolsGopath: 
gopath: C:\Users\jsgou\go
GOROOT: C:\Program Files\Go
PATH: C:\Program Files (x86)\VMware\VMware Player\bin\;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin;C:\Program Files\Microsoft MPI\Bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Python27;C:\Program Files\dotnet\;C:\Program Files (x86)\dotnet\;C:\Program Files (x86)\Common Files\Intuit\QBPOSSDKRuntime;C:\Program Files (x86)\PHP7\;C:\Program Files\Microsoft\Azure Functions Core Tools\;C:\Program Files\nodejs\;C:\Program Files\Azure Data Studio\bin;C:\Program Files\Go\bin;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;C:\TDM-GCC-64\bin;C:\Users\jsgou\AppData\Local\Microsoft\WindowsApps;;C:\Users\jsgou\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\jsgou\.dotnet\tools;C:\Users\jsgou\AppData\Local\Programs\Fiddler;C:\Program Files\Azure Data Studio\bin;C:\Users\jsgou\.dotnet\tools;C:\Users\jsgou\AppData\Roaming\npm;C:\Program Files\Azure Data Studio\bin;C:\Users\jsgou\go\bin

   gopkgs: C:\Users\jsgou\go\bin\gopkgs.exe installed
   go-outline: C:\Users\jsgou\go\bin\go-outline.exe installed
   gotests: C:\Users\jsgou\go\bin\gotests.exe installed
   gomodifytags: gomodifytags not installed
   impl: impl not installed
   goplay: goplay not installed
   dlv: C:\Users\jsgou\go\bin\dlv.exe installed
   dlv-dap: C:\Users\jsgou\go\bin\dlv-dap.exe installed
   staticcheck: C:\Users\jsgou\go\bin\staticcheck.exe installed
   gopls: C:\Users\jsgou\go\bin\gopls.exe installed

go env
Workspace Folder (project): f:\git\clients\project-technologies\project-go
	set GO111MODULE=
	set GOARCH=amd64
	set GOBIN=
	set GOCACHE=C:\Users\jsgou\AppData\Local\go-build
	set GOENV=C:\Users\jsgou\AppData\Roaming\go\env
	set GOEXE=.exe
	set GOFLAGS=
	set GOHOSTARCH=amd64
	set GOHOSTOS=windows
	set GOINSECURE=
	set GOMODCACHE=C:\Users\jsgou\go\pkg\mod
	set GONOPROXY=
	set GONOSUMDB=
	set GOOS=windows
	set GOPATH=C:\Users\jsgou\go
	set GOPRIVATE=
	set GOPROXY=https://proxy.golang.org,direct
	set GOROOT=C:\Program Files\Go
	set GOSUMDB=sum.golang.org
	set GOTMPDIR=
	set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
	set GOVCS=
	set GOVERSION=go1.16.5
	set GCCGO=gccgo
	set AR=ar
	set CC=gcc
	set CXX=g++
	set CGO_ENABLED=1
	set GOMOD=f:\git\clients\project-technologies\project-go\go.mod
	set CGO_CFLAGS=-g -O2
	set CGO_CPPFLAGS=
	set CGO_CXXFLAGS=-g -O2
	set CGO_FFLAGS=-g -O2
	set CGO_LDFLAGS=-g -O2
	set PKG_CONFIG=pkg-config
	set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\jsgou\AppData\Local\Temp\go-build69496041=/tmp/go-build -gno-record-gcc-switches

Share the Go related settings you have added/edited

"go.delveConfig": {
        "debugAdapter": "dlv-dap",
    },

Describe the bug

The Go to Definition (F12) will go to the definition of the object after the period when the cursor is located at the end of the first variable. Same thing happens when the word itself is selected.

Steps to reproduce the behavior:

Paste the following:

type MyStruct struct {
	MyField int
}

func main() {
	myVariable := &MyStruct{}
	myVariable.MyField = 2
}

Put your cursor before the "." (period), after the second instance of myVariable.

Press F12. You will end up at MyField, I would have expected to go to myVariable.
Same thing happens if you double click on the second instance of myVariable, then F12.
Same thing happens if you Right click at the end of the variable then Go to definition.

@hyangah
Copy link
Contributor

hyangah commented Jul 26, 2021

Thanks for the issue report @jsgoupil

I could reproduce this too. (both in the stable & the master) - textDocument/definition from other positions before . returns the definition of myVariable but it returns the location of MyField definition.

If func main() {
	myVariable := &MyStruct{}
	myVariable
}

In the above (incomplete) code, textDocument/definition from the end of myVariable returns the location of myField.

Transferring to the gopls issue tracker to get more attention from gopls devs.

@hyangah hyangah changed the title "Go to definition" goes to the object after the period when the cursor is at the end of a variable x/tools/gopls: "Go to definition" goes to the object after the period when the cursor is at the end of a variable Jul 26, 2021
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Jul 26, 2021
@hyangah hyangah transferred this issue from golang/vscode-go Jul 26, 2021
@gopherbot gopherbot added the gopls Issues related to the Go language server, gopls. label Jul 26, 2021
@gopherbot gopherbot added this to the Unreleased milestone Jul 26, 2021
@hyangah hyangah modified the milestones: Unreleased, gopls/v0.7.2 Jul 27, 2021
@KarthikNayak
Copy link
Contributor

AFAIK all the three situations stated, i.e.

  • Cursor behind .
  • Double clicking myVariable
  • Right clicking at the end of myVariable

Put the cursor right before the .. In that sense, the issue is really just when the cursor is behind the ..
Now to answer the bug itself, the cursor at any point is a location, with vscode's line cursor, the location when the cursor is behind the . is the . itself. This can easily be understood when comparing it to a block cursor.

Try changing the cursor from line -> block in vscode and you'll notice that the . is highlighted. So, when the cursor is right behind the . it asks gopls for jump to definition for the . and that goes to MyField. For e.g. I use a block cursor on Emacs and this is the expected behavior for me. I think this is working as expected.

@jsgoupil
Copy link
Author

I believe this is not working as expected. Especially that the variable is highlighted the same way independently where my cursor is on the variable.

image
image

Double clicking on the variable that is in a method call, navigates to the variable. Double clicking on a variable that is followed by a period goes to its field/object... not consistent.

image

I don't see any other languages doing this either.

@hyangah
Copy link
Contributor

hyangah commented Jul 29, 2021

I agree with @jsgoupil. I quickly checked a few other languages (python, javascript/typescript, c) and they don't associate the cursor at "." with the following field, which I think is more intuitive.

@gopherbot
Copy link

Change https://golang.org/cl/338789 mentions this issue: internal/lsp: find references for ident before selector

@golang golang locked and limited conversation to collaborators Aug 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants