Skip to content

x/tools/gopls: extract function action doesn't work if the first line is a comment #54816

@ainar-g

Description

@ainar-g

gopls version

gopls -v version
Build info
----------
golang.org/x/tools/gopls v0.9.4
    golang.org/x/tools/[email protected] h1:YhHOxVi++ILnY+QnH9FGtRKZZrunSaR7OW8/dCp7bBk=
    github.com/BurntSushi/[email protected] h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
    github.com/google/[email protected] h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
    github.com/sergi/[email protected] h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/exp/[email protected] h1:7Xs2YCOpMlNqSQSmrrnhlzBXIE/bpMecZplbLePTJvE=
    golang.org/x/[email protected] h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
    golang.org/x/[email protected] h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
    golang.org/x/[email protected] h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
    golang.org/x/[email protected] h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
    golang.org/x/[email protected] h1:b68wxF4nfQjj1XTRHtjVjCximbhAwjztuzDEFGU+n9o=
    golang.org/x/[email protected] h1:BkeW9/QJhcigekDUPS9N9bIb0v7gPKKmLYeczVAqr2s=
    honnef.co/go/[email protected] h1:ytYb4rOqyp1TSa2EPvNVwtPQJctSELKaMyLfqNP4+34=
    mvdan.cc/[email protected] h1:avhhrOmv0IuvQVK7fvwV91oFSGAk5/6Po8GXTzICeu8=
    mvdan.cc/xurls/[email protected] h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
go: go1.19

go env

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

What did you do?

package main

func main() {
	// Do one thing.
	println("thing 1")

	// Do other things.
	println("thing 2")
	println("thing 3")
}

Select the line with // Do other things. and two others below it. Show range code actions (vim.lsp.buf.range_code_action() in my case).

What did you expect to see?

The Extract function action being available.

What did you see instead?

No code actions available

If I only select the two code lines, it works as expected.

Editor and settings

NVIM v0.7.2 with the latest nvim-lspconfig.

Logs

[START][2022-09-01 17:54:13] LSP logging initiated
[WARN][2022-09-01 17:54:13] ...lsp/handlers.lua:110	"The language server gopls triggers a registerCapability handler despite dynamicRegistration set to false. Report upstream, this warning is harmless"
[ERROR][2022-09-01 17:54:13] ...lsp/handlers.lua:455	'2022/09/01 17:54:13 analyzer "SA4023" failed: analysis SA4023 for package bitbucket.org/ainar-g/tmp/go/tmp panicked: interface conversion: interface {} is nil, not *nilness.Result\n'
[ERROR][2022-09-01 17:54:13] ...lsp/handlers.lua:455	'2022/09/01 17:54:13 analyzer "SA4023" failed: analysis SA4023 for package bitbucket.org/ainar-g/tmp/go/tmp panicked: interface conversion: interface {} is nil, not *nilness.Result\n'
[ERROR][2022-09-01 17:54:13] ...lsp/handlers.lua:455	'2022/09/01 17:54:13 analyzer "SA4023" failed: analysis SA4023 for package bitbucket.org/ainar-g/tmp/go/tmp panicked: interface conversion: interface {} is nil, not *nilness.Result\n'
[ERROR][2022-09-01 17:54:13] ...lsp/handlers.lua:455	'2022/09/01 17:54:13 analyzer "SA4023" failed: analysis SA4023 for package bitbucket.org/ainar-g/tmp/go/tmp panicked: interface conversion: interface {} is nil, not *nilness.Result\n'
[DEBUG][2022-09-01 17:54:28] .../lua/vim/lsp.lua:1023	"LSP[gopls]"	"client.request"	1	"textDocument/codeAction"	{  context = {    diagnostics = {}  },  range = {    end = {      character = 20,      line = 8    },    start = {      character = 0,      line = 6    }  },  textDocument = {    uri = "file:///home/ainar/dev/tmp/go/tmp/main.go"  }}	<function 1>	1
[DEBUG][2022-09-01 17:54:28] .../vim/lsp/rpc.lua:347	"rpc.send"	{  id = 2,  jsonrpc = "2.0",  method = "textDocument/codeAction",  params = {    context = {      diagnostics = {}    },    range = {      end = {        character = 20,        line = 8      },      start = {        character = 0,        line = 6      }    },    textDocument = {      uri = "file:///home/ainar/dev/tmp/go/tmp/main.go"    }  }}
[DEBUG][2022-09-01 17:54:28] .../vim/lsp/rpc.lua:454	"rpc.receive"	{  jsonrpc = "2.0",  method = "window/logMessage",  params = {    message = '2022/09/01 17:54:28 fixImports(filename="/home/ainar/dev/tmp/go/tmp/main.go"), abs="/home/ainar/dev/tmp/go/tmp/main.go", srcDir="/home/ainar/dev/tmp/go/tmp" ...\n',    type = 3  }}
[INFO][2022-09-01 17:54:28] ...lsp/handlers.lua:459	'2022/09/01 17:54:28 fixImports(filename="/home/ainar/dev/tmp/go/tmp/main.go"), abs="/home/ainar/dev/tmp/go/tmp/main.go", srcDir="/home/ainar/dev/tmp/go/tmp" ...\n'
[DEBUG][2022-09-01 17:54:28] .../vim/lsp/rpc.lua:454	"rpc.receive"	{  jsonrpc = "2.0",  method = "window/logMessage",  params = {    message = '2022/09/01 17:54:28 analyzer "SA4023" failed: analysis SA4023 for package bitbucket.org/ainar-g/tmp/go/tmp panicked: interface conversion: interface {} is nil, not *nilness.Result\n',    type = 1  }}
[ERROR][2022-09-01 17:54:28] ...lsp/handlers.lua:455	'2022/09/01 17:54:28 analyzer "SA4023" failed: analysis SA4023 for package bitbucket.org/ainar-g/tmp/go/tmp panicked: interface conversion: interface {} is nil, not *nilness.Result\n'
[DEBUG][2022-09-01 17:54:28] .../vim/lsp/rpc.lua:454	"rpc.receive"	{  id = 2,  jsonrpc = "2.0"}
[DEBUG][2022-09-01 17:54:31] .../lua/vim/lsp.lua:1023	"LSP[gopls]"	"client.request"	1	"textDocument/codeAction"	{  context = {    diagnostics = {}  },  range = {    end = {      character = 20,      line = 8    },    start = {      character = 0,      line = 7    }  },  textDocument = {    uri = "file:///home/ainar/dev/tmp/go/tmp/main.go"  }}	<function 1>	1
[DEBUG][2022-09-01 17:54:31] .../vim/lsp/rpc.lua:347	"rpc.send"	{  id = 3,  jsonrpc = "2.0",  method = "textDocument/codeAction",  params = {    context = {      diagnostics = {}    },    range = {      end = {        character = 20,        line = 8      },      start = {        character = 0,        line = 7      }    },    textDocument = {      uri = "file:///home/ainar/dev/tmp/go/tmp/main.go"    }  }}
[DEBUG][2022-09-01 17:54:31] .../vim/lsp/rpc.lua:454	"rpc.receive"	{  jsonrpc = "2.0",  method = "window/logMessage",  params = {    message = '2022/09/01 17:54:31 fixImports(filename="/home/ainar/dev/tmp/go/tmp/main.go"), abs="/home/ainar/dev/tmp/go/tmp/main.go", srcDir="/home/ainar/dev/tmp/go/tmp" ...\n',    type = 3  }}
[INFO][2022-09-01 17:54:31] ...lsp/handlers.lua:459	'2022/09/01 17:54:31 fixImports(filename="/home/ainar/dev/tmp/go/tmp/main.go"), abs="/home/ainar/dev/tmp/go/tmp/main.go", srcDir="/home/ainar/dev/tmp/go/tmp" ...\n'
[DEBUG][2022-09-01 17:54:31] .../vim/lsp/rpc.lua:454	"rpc.receive"	{  id = 3,  jsonrpc = "2.0",  result = { {      command = {        arguments = { {            Fix = "extract_function",            Range = {              end = {                character = 20,                line = 8              },              start = {                character = 0,                line = 7              }            },            URI = "file:///home/ainar/dev/tmp/go/tmp/main.go"          } },        command = "gopls.apply_fix",        title = "Extract function"      },      edit = vim.empty_dict(),      kind = "refactor.extract",      title = "Extract function"    } }}
[DEBUG][2022-09-01 17:54:31] .../vim/lsp/rpc.lua:454	"rpc.receive"	{  jsonrpc = "2.0",  method = "window/logMessage",  params = {    message = '2022/09/01 17:54:31 analyzer "SA4023" failed: analysis SA4023 for package bitbucket.org/ainar-g/tmp/go/tmp panicked: interface conversion: interface {} is nil, not *nilness.Result\n',    type = 1  }}
[ERROR][2022-09-01 17:54:32] ...lsp/handlers.lua:455	'2022/09/01 17:54:31 analyzer "SA4023" failed: analysis SA4023 for package bitbucket.org/ainar-g/tmp/go/tmp panicked: interface conversion: interface {} is nil, not *nilness.Result\n'
[INFO][2022-09-01 17:54:34] .../lua/vim/lsp.lua:1392	"exit_handler"	{ {    _on_attach = <function 1>,    attached_buffers = { true },    cancel_request = <function 2>,    commands = {},    config = {      autostart = true,      capabilities = {        callHierarchy = {          dynamicRegistration = false        },        textDocument = {          codeAction = {            codeActionLiteralSupport = {              codeActionKind = {                valueSet = { "", "Empty", "QuickFix", "Refactor", "RefactorExtract", "RefactorInline", "RefactorRewrite", "Source", "SourceOrganizeImports", "quickfix", "refactor", "refactor.extract", "refactor.inline", "refactor.rewrite", "source", "source.organizeImports" }              }            },            dataSupport = true,            dynamicRegistration = false,            resolveSupport = {              properties = { "edit" }            }          },          completion = {            completionItem = {              commitCharactersSupport = false,              deprecatedSupport = false,              documentationFormat = { "markdown", "plaintext" },              preselectSupport = false,              snippetSupport = false            },            completionItemKind = {              valueSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 }            },            contextSupport = false,            dynamicRegistration = false          },          declaration = {            linkSupport = true          },          definition = {            linkSupport = true          },          documentHighlight = {            dynamicRegistration = false          },          documentSymbol = {            dynamicRegistration = false,            hierarchicalDocumentSymbolSupport = true,            symbolKind = {              valueSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 }            }          },          hover = {            contentFormat = { "markdown", "plaintext" },            dynamicRegistration = false          },          implementation = {            linkSupport = true          },          publishDiagnostics = {            relatedInformation = true,            tagSupport = {              valueSet = { 1, 2 }            }          },          references = {            dynamicRegistration = false          },          rename = {            dynamicRegistration = false,            prepareSupport = true          },          signatureHelp = {            dynamicRegistration = false,            signatureInformation = {              activeParameterSupport = true,              documentationFormat = { "markdown", "plaintext" },              parameterInformation = {                labelOffsetSupport = true              }            }          },          synchronization = {            didSave = true,            dynamicRegistration = false,            willSave = false,            willSaveWaitUntil = false          },          typeDefinition = {            linkSupport = true          }        },        window = {          showDocument = {            support = false          },          showMessage = {            messageActionItem = {              additionalPropertiesSupport = false            }          },          workDoneProgress = true        },        workspace = {          applyEdit = true,          configuration = true,          symbol = {            dynamicRegistration = false,            hierarchicalWorkspaceSymbolSupport = true,            symbolKind = {              valueSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 }            }          },          workspaceEdit = {            resourceOperations = { "rename", "create", "delete" }          },          workspaceFolders = true        }      },      cmd = { "gopls" },      cmd_cwd = "/home/ainar/dev/tmp/go/tmp",      filetypes = { "go", "gomod", "gowork", "gotmpl" },      flags = {},      get_language_id = <function 3>,      handlers = <1>{},      init_options = vim.empty_dict(),      log_level = 2,      message_level = 2,      name = "gopls",      on_attach = <function 4>,      on_exit = <function 5>,      on_init = <function 6>,      root_dir = "/home/ainar/dev/tmp/go/tmp",      settings = {        gopls = {          analyses = {            fieldalignment = true,            nilness = true,            shadow = true,            unusedparams = true,            unusedwrite = true          },          annotations = {            bounds = true,            escape = true,            inline = true,            nil = true          },          ["build.directoryFilters"] = { "-client", "-client2", "-node_modules" },          ["formatting.gofumpt"] = true,          staticcheck = true,          ["ui.semanticTokens"] = true,          ["ui.verboseOutput"] = true        }      },      single_file_support = true,      workspace_folders = <2>{ {          name = "/home/ainar/dev/tmp/go/tmp",          uri = "file:///home/ainar/dev/tmp/go/tmp"        } },      <metatable> = <3>{        __tostring = <function 7>      }    },    handlers = <table 1>,    id = 1,    initialized = true,    is_stopped = <function 8>,    messages = {      messages = {},      name = "gopls",      progress = {        ["5577006791947779410"] = {          done = true,          message = "Finished loading packages.",          title = "Setting up workspace"        }      },      status = {}    },    name = "gopls",    notify = <function 9>,    offset_encoding = "utf-16",    request = <function 10>,    request_sync = <function 11>,    requests = {},    resolved_capabilities = {      call_hierarchy = true,      code_action = <4>{        codeActionKinds = { "quickfix", "refactor.extract", "refactor.rewrite", "source.fixAll", "source.organizeImports" }      },      code_lens = true,      code_lens_resolve = false,      completion = true,      declaration = false,      document_formatting = true,      document_highlight = true,      document_range_formatting = false,      document_symbol = true,      execute_command = true,      find_references = true,      goto_definition = true,      hover = true,      implementation = true,      rename = true,      signature_help = true,      signature_help_trigger_characters = <5>{ "(", "," },      text_document_did_change = 2,      text_document_open_close = true,      text_document_save = <6>vim.empty_dict(),      text_document_save_include_text = false,      text_document_will_save = false,      text_document_will_save_wait_until = false,      type_definition = true,      workspace_folder_properties = {        changeNotifications = "workspace/didChangeWorkspaceFolders",        supported = true      },      workspace_symbol = true    },    rpc = {      handle = <userdata 1>,      notify = <function 12>,      pid = 479364,      request = <function 13>    },    server_capabilities = {      callHierarchyProvider = true,      codeActionProvider = <table 4>,      codeLensProvider = vim.empty_dict(),      completionProvider = {        completionItem = vim.empty_dict(),        triggerCharacters = { "." }      },      definitionProvider = true,      documentFormattingProvider = true,      documentHighlightProvider = true,      documentLinkProvider = vim.empty_dict(),      documentOnTypeFormattingProvider = {        firstTriggerCharacter = ""      },      documentSymbolProvider = true,      executeCommandProvider = {        commands = { "gopls.add_dependency", "gopls.add_import", "gopls.apply_fix", "gopls.check_upgrades", "gopls.edit_go_directive", "gopls.gc_details", "gopls.generate", "gopls.generate_gopls_mod", "gopls.go_get_package", "gopls.list_imports", "gopls.list_known_packages", "gopls.regenerate_cgo", "gopls.remove_dependency", "gopls.run_tests", "gopls.run_vulncheck_exp", "gopls.start_debugging", "gopls.test", "gopls.tidy", "gopls.toggle_gc_details", "gopls.update_go_sum", "gopls.upgrade_dependency", "gopls.vendor" }      },      foldingRangeProvider = true,      hoverProvider = true,      implementationProvider = true,      inlayHintProvider = vim.empty_dict(),      referencesProvider = true,      renameProvider = {        prepareProvider = true      },      signatureHelpProvider = {        triggerCharacters = <table 5>      },      textDocumentSync = {        change = 2,        openClose = true,        save = <table 6>      },      typeDefinitionProvider = true,      workspace = {        workspaceFolders = {          changeNotifications = "workspace/didChangeWorkspaceFolders",          supported = true        }      },      workspaceSymbolProvider = true    },    stop = <function 14>,    supports_method = <function 15>,    workspaceFolders = <table 2>,    workspace_did_change_configuration = <function 16>,    workspace_folders = <table 2>  } }
[DEBUG][2022-09-01 17:54:34] .../vim/lsp/rpc.lua:347	"rpc.send"	{  id = 4,  jsonrpc = "2.0",  method = "shutdown"}
[DEBUG][2022-09-01 17:54:34] .../vim/lsp/rpc.lua:454	"rpc.receive"	{  id = 4,  jsonrpc = "2.0"}
[DEBUG][2022-09-01 17:54:34] .../vim/lsp/rpc.lua:347	"rpc.send"	{  jsonrpc = "2.0",  method = "exit"}
[DEBUG][2022-09-01 17:54:34] .../vim/lsp/rpc.lua:454	"rpc.receive"	{  jsonrpc = "2.0",  method = "window/logMessage",  params = {    message = "2022/09/01 17:54:34 Shutdown session\n\tshutdown_session=1\n",    type = 3  }}
[INFO][2022-09-01 17:54:34] ...lsp/handlers.lua:459	"2022/09/01 17:54:34 Shutdown session\n\tshutdown_session=1\n"

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions