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: Selection cursor does not begin at the top of the list of completion items #61533

Closed
anthony-S93 opened this issue Jul 23, 2023 · 5 comments
Labels
gopls/completion Issues related to auto-completion in gopls. gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@anthony-S93
Copy link

anthony-S93 commented Jul 23, 2023

gopls version

Build info
----------
golang.org/x/tools/gopls v0.12.4
    golang.org/x/tools/gopls@(devel)
    github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
    github.com/google/go-cmp@v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/exp@v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
    golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
    golang.org/x/mod@v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
    golang.org/x/sync@v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
    golang.org/x/sys@v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
    golang.org/x/text@v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
    golang.org/x/tools@v0.10.1-0.20230622221742-0622ad2359a7 h1:5PWemM67wMSPpO0Y3lOPlyvgO3z56YkZRxPFcdd300g=
    golang.org/x/vuln@v0.0.0-20230110180137-6ad3e3d07815 h1:A9kONVi4+AnuOr1dopsibH6hLi1Huy54cbeJxnq4vmU=
    honnef.co/go/tools@v0.4.2 h1:6qXr+R5w+ktL5UkwEbPp+fEvfyoMPche6GkOpGHZcLc=
    mvdan.cc/gofumpt@v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM=
    mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
go: go1.20.5

go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/anthony93/.cache/go-build"
GOENV="/home/anthony93/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/anthony93/Dev/.go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/anthony93/Dev/.go/"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.6"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build359582051=/tmp/go-build -gno-record-gcc-switches"

What did you do?

  1. Create a test directory mkdir test
  2. Create a test file inside the test directory cd test ; touch test.go
  3. Open test.go with a text editor (e.g: nvim test.go)
  4. Write some trivial code to test autocompletion.
package main

import "fmt"

func main(){
    fmt.Println()
}

What did you expect to see?

The selection cursor should start at the top of the autocompletion list.

What did you see instead?

Whenever the autocompletion list pops up, the selection cursor automatically jumps to the list item that is first in alphabetical order, not the item that is actually displayed at the top of the list. Like so:

gopls_cmp_bug.mp4

This behavior is counter-intuitive and inconsistent with all other language servers I've used. If this is by design, I would like to know how to turn it off because it's too different from how other language servers handle completions and I prefer to have a uniform completion experience across all language servers.

Editor and settings

I can consistently reproduce this behavior in Neovim with nvim-cmp as the completion engine (https://github.com/hrsh7th/nvim-cmp). The language server is launched mostly with the defaults provided by nvim-lspconfig. No additional settings except for the server capabilities required to enable completion are used.

local lspconfig = require("lspconfig")
local cmp_nvim_lsp = require("cmp_nvim_lsp")
local default_capabilities = cmp_nvim_lsp.default_capabilities()

lspconfig.gopls.setup({
    capabilities = default_capabilities
})

Please note that I have tested other language servers (pyright, tsserver, bashls, jdtls, ccls, lua_ls, etc.) using the same editor and same completion engine. gopls is the only language server to exibit this strange behavior, so it's quite unlikely for the editor or the completion engine to be the cause of this issue.

Additional Info

Operating system

OS: Arch Linux x86_64
Kernel: 6.4.4-arch1-1

Editor version

NVIM v0.9.1
Build type: Release
LuaJIT 2.1.0-beta3
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Jul 23, 2023
@gopherbot gopherbot added this to the Unreleased milestone Jul 23, 2023
@hyangah
Copy link
Contributor

hyangah commented Jul 24, 2023

I don't see this problem from vscode and the completion order shown in the video doesn't seem to match what gopls is sending.

@anthony-S93 can you please capture the lsp trace (esp textDocument/completion message)? And if possible, can you also capture the lsp textDocument/completion messages from other language servers? That will help us understand what gopls is doing differently.

@hyangah hyangah added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jul 24, 2023
@anthony-S93
Copy link
Author

anthony-S93 commented Jul 24, 2023

Thank you for the quick response. So I've dug thorugh my LSP logs and managed to isolate the logs that were produced right after I activated the completion feature shown in the video above. Here are the logs for the different language servers I re-tested:

  1. gopls
  2. pyright

I also tested tsserver, but tsserver didn't produce output logs at all when I invoked autocompletion.

Please note that logs I sent were for when the language servers were running in "single-file" mode, which simplifies the logs. In any case, gopls exhibited the same behavior with or without any root markers in the project directory.

Thank you for your time. And please let me know if there is any additional information you need.

Update:
@hyangah
Here is something else I discovered upon further testing. If I use autocompletion to complete fmt instead of typing it manually like I did in my original report, the autocompletion behaves normally. Like so:

normal_behavior_when_using_completion_on_fmt.mp4

The correct behavior did not produce any logs.

For completeness' sake, here is a video showing the autocompletion behavior when I typed the entire string "fmt" manually.

abnormal_behavior_when_typing_fmt_manually.mp4

@hyangah hyangah removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 27, 2023
@hyangah hyangah modified the milestones: Unreleased, gopls/v0.14.0 Jul 27, 2023
@adonovan adonovan added the gopls/completion Issues related to auto-completion in gopls. label Aug 31, 2023
@findleyr
Copy link
Contributor

Hi, in the logs you shared (https://pastebin.com/raw/Wm4j0FPG), I can see that 'preselect' is only set for the first item in the list. So this looks like a bug in your LSP client. I also can't reproduce in either VS Code or coc.nvim.

@findleyr findleyr added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 27, 2023
@findleyr findleyr modified the milestones: gopls/v0.14.0, gopls/backlog Sep 27, 2023
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@anthony-S93
Copy link
Author

anthony-S93 commented Feb 2, 2024

Hi, in the logs you shared (https://pastebin.com/raw/Wm4j0FPG), I can see that 'preselect' is only set for the first item in the list. So this looks like a bug in your LSP client. I also can't reproduce in either VS Code or coc.nvim.

I looked deeper into this and discovered what's causing the issue. As it turns out, the completion engine I'm currently using will choose the ordering of the items in the completion list dynamically—based on the occurence of the completion item within the document itself. Here's a brief video to demonstrate this behavior:

golang-completion-solution.mp4

However, this dynamic reordering of the completion list by the completion engine does not work well with gopls, which always assumes dictionary ordering of the completion list and then preselects the first item in said list. Both gopls and the completion engine are working on different assumptions with regards to the order of the completion list items, and that's what's causing the issue.

The Workaround:
If you're using gopls with neovim and using nvim-cmp + cmp-nvim-lsp for completion, add the following code to your nvim-cmp's config:

local cmp = require("cmp")
cmp.setup({
    -- Your own cmp config is here...
    
    -- Add the following config to turn off preselect
    preselect = cmp.PreselectMode.None
})

What this does is that it instructs nvim-cmp to ignore preselections by the LSP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/completion Issues related to auto-completion in gopls. gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants