Skip to content

x/tools/gopls: "Browse gopls feature documentation" has higher priority than "Add import" #72742

Closed
@your-diary

Description

@your-diary

gopls version

Build info
----------
golang.org/x/tools/gopls v0.17.1
    golang.org/x/tools/gopls@v0.17.1 h1:Mt/DSfnnSe3dyf6MH/dZZ0iww+viHNhAFc4rEYDiOAw=
    github.com/BurntSushi/toml@v1.4.1-0.20240526193622-a339e1f7089c h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=
    github.com/google/go-cmp@v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
    golang.org/x/exp/typeparams@v0.0.0-20231108232855-2478ac86f678 h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=
    golang.org/x/mod@v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
    golang.org/x/sync@v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
    golang.org/x/telemetry@v0.0.0-20241106142447-58a1122356f5 h1:TCDqnvbBsFapViksHcHySl/sW4+rTGNIAoJJesHRuMM=
    golang.org/x/text@v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
    golang.org/x/tools@v0.27.1-0.20241219162658-575221bfbda3 h1:kgwdasJRsdDWYgWcEgMF424DiXwwXHSb3V8xVTi//i8=
    golang.org/x/vuln@v1.0.4 h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
    honnef.co/go/tools@v0.5.1 h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I=
    mvdan.cc/gofumpt@v0.7.0 h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU=
    mvdan.cc/xurls/v2@v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
go: go1.24.0

go env

AR='ar'
CC='cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='c++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/user/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/user/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/32/_3tgvq014md8jjwcg2f5j9fc0000gn/T/go-build1609517655=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/user/a/go.mod'
GOMODCACHE='/Users/user/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/user/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.24.0/libexec'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/user/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.24.0/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

  1. Open main.go below. Note fmt package is not imported.

    package main
    
    func main() {
        fmt.Println("hello world")
    }
  2. Move the cursor to the top of fmt keyword.

  3. Open the code actions panel to use Add import functionality of gopls.

What did you see happen?

Browse gopls feature documentation has higher priority than Add import.

Image

What did you expect to see?

I believe, in older versions, Add import had higher priority. (Maybe related or not related: golang/vscode-go #3498)

So

  • At least I want to change the order to make Add import the top action. Unintentionally selecting Browse gopls feature documentation opens a new window, which is very annoying (in terms of context switching etc.)

  • If possible, I want even to disable Browse gopls feature documentation completely. I never use it.

Editor and settings

I'm using Vim and coc.nvim plugin and set up the vanilla gopls in :CocConfig as suggested by the coc.nvim team:

{
    "languageserver": {
        "golang": {
            "command": "gopls",
            "rootPatterns": [
                "go.mod"
            ],
            "filetypes": [
                "go"
            ]
        }
    }
}

(I don't use plugins like vim-go or coc-go.)

Logs

Using the debug mode of coc.nvim (this and this), I captured the output of gopls when I opened the code actions panel (textDocument/codeAction).

We can see Browse gopls feature documentation is returned as the first element of the array.

Request

{
    "textDocument": {
        "uri": "file:///Users/user/a/main.go"
    },
    "range": {
        "start": {
            "line": 3,
            "character": 2
        },
        "end": {
            "line": 3,
            "character": 2
        }
    },
    "context": {
        "diagnostics": [
            {
                "range": {
                    "start": {
                        "line": 3,
                        "character": 1
                    },
                    "end": {
                        "line": 3,
                        "character": 4
                    }
                },
                "severity": 1,
                "code": "UndeclaredName",
                "codeDescription": {
                    "href": "https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#UndeclaredName"
                },
                "source": "compiler",
                "message": "undefined: fmt"
            }
        ],
        "triggerKind": 1
    }
}

Response

[
    {
        "title": "Browse gopls feature documentation",
        "kind": "gopls.doc.features",
        "command": {
            "title": "Browse gopls feature documentation",
            "command": "gopls.client_open_url",
            "arguments": [
                "https://github.com/golang/tools/blob/master/gopls/doc/features/README.md"
            ]
        }
    },
    {
        "title": "Add import:  \"fmt\"",
        "kind": "quickfix",
        "diagnostics": [
            {
                "range": {
                    "start": {
                        "line": 3,
                        "character": 1
                    },
                    "end": {
                        "line": 3,
                        "character": 4
                    }
                },
                "severity": 1,
                "code": "UndeclaredName",
                "codeDescription": {
                    "href": "https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#UndeclaredName"
                },
                "source": "compiler",
                "message": "undefined: fmt"
            }
        ],
        "edit": {
            "documentChanges": [
                {
                    "textDocument": {
                        "version": 1,
                        "uri": "file:///Users/user/a/main.go"
                    },
                    "edits": [
                        {
                            "range": {
                                "start": {
                                    "line": 1,
                                    "character": 0
                                },
                                "end": {
                                    "line": 1,
                                    "character": 0
                                }
                            },
                            "newText": "\nimport \"fmt\"\n"
                        }
                    ]
                }
            ]
        }
    },
    {
        "title": "Add test for main",
        "kind": "source.addTest",
        "data": {
            "title": "Add test for main",
            "command": "gopls.add_test",
            "arguments": [
                {
                    "uri": "file:///Users/user/a/main.go",
                    "range": {
                        "start": {
                            "line": 3,
                            "character": 1
                        },
                        "end": {
                            "line": 3,
                            "character": 1
                        }
                    }
                }
            ]
        }
    },
    {
        "title": "Browse arm64 assembly for main",
        "kind": "source.assembly",
        "command": {
            "title": "Browse arm64 assembly for main",
            "command": "gopls.assembly",
            "arguments": [
                "1",
                "a",
                "main.main"
            ]
        }
    },
    {
        "title": "Browse documentation for package main",
        "kind": "source.doc",
        "command": {
            "title": "Browse documentation for package main",
            "command": "gopls.doc",
            "arguments": [
                {
                    "Location": {
                        "uri": "file:///Users/user/a/main.go",
                        "range": {
                            "start": {
                                "line": 3,
                                "character": 1
                            },
                            "end": {
                                "line": 3,
                                "character": 1
                            }
                        }
                    },
                    "ShowDocument": true
                }
            ]
        }
    },
    {
        "title": "Organize Imports",
        "kind": "source.organizeImports",
        "edit": {
            "documentChanges": [
                {
                    "textDocument": {
                        "version": 1,
                        "uri": "file:///Users/user/a/main.go"
                    },
                    "edits": [
                        {
                            "range": {
                                "start": {
                                    "line": 1,
                                    "character": 0
                                },
                                "end": {
                                    "line": 1,
                                    "character": 0
                                }
                            },
                            "newText": "\nimport \"fmt\"\n"
                        }
                    ]
                }
            ]
        }
    }
]

Activity

added
goplsIssues related to the Go language server, gopls.
ToolsThis label describes issues relating to any tools in the x/tools repository.
on Mar 8, 2025
added this to the Unreleased milestone on Mar 8, 2025
added
ToolProposalIssues describing a requested change to a Go tool or command-line program.
on Mar 8, 2025
h9jiang

h9jiang commented on Mar 11, 2025

@h9jiang
Member

Another supported field in LSP of returned code action is IsPreferred introduced in LSP 3.15. We can work on an improvement to mark quick fix as preferred. However, it will be the client's responsibility to promote the preferred code action compares to others.

	// Marks this as a preferred action. Preferred actions are used by the `auto fix` command and can be targeted
	// by keybindings.
	//
	// A quick fix should be marked preferred if it properly addresses the underlying error.
	// A refactoring should be marked preferred if it is the most reasonable choice of actions to take.
	//
	// @since 3.15.0

See LSP

added theissue type on Mar 11, 2025
removed
ToolProposalIssues describing a requested change to a Go tool or command-line program.
on Mar 19, 2025
modified the milestones: Unreleased, gopls/v0.18.2 on Mar 19, 2025
gopherbot

gopherbot commented on Apr 4, 2025

@gopherbot
Contributor

Change https://go.dev/cl/662915 mentions this issue: gopls/internal/golang: CodeAction: place gopls doc as the last action

added a commit that references this issue on Apr 4, 2025
e850fe1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @gopherbot@jba@your-diary@h9jiang@gabyhelp

        Issue actions

          x/tools/gopls: "Browse gopls feature documentation" has higher priority than "Add import" · Issue #72742 · golang/go