neovim / nvim-lspconfig

Quickstart configs for Nvim LSP
Apache License 2.0
10.3k stars 2.04k forks source link

Linting works after activating the LSP twice using Autocommand FileType. #1142

Closed Cartogy closed 3 years ago

Cartogy commented 3 years ago

Installed nvim-lspconfig via pathogen

When requesting the Lspconfig for a filetype via

autocmd FileType cpp lua require('lspconfig').clangd.setup{}

it does not activate linting. Only after the autocommand is called twice will the linting work.

autocmd FileType cpp lua require('lspconfig').clangd.setup{}
autocmd FileType cpp lua require('lspconfig').clangd.setup{}

Yet when calling the require command without the FileType autocommand, it activates linting:

lua require('lspconfig').clangd.setup{}

Temporary solution

I found a workaround that activates the lsp depending on the filetype.

if &filetype ==# "cpp"
    lua require('lspconfig').clangd.setup{}
endif

How to reproduce the problem from neovim startup

Install:

Actual behaviour

LSP does not show linting

Expected behaviour

Have LSP show linting

Minimal init.vim or init.lua and code sample

" When executing via Autocommand, the 'require' needs to be called twice.
augroup CPP
    autocmd!
    autocmd FileType cpp lua require('lspconfig').clangd.setup{}
    "autocmd FileType cpp lua require('lspconfig').clangd.setup{}
augroup END

augroup PY
    autocmd!
    autocmd FileType python lua require('lspconfig').pyright.setup{}
    "autocmd FileType python lua require('lspconfig').pyright.setup{}
augroup END

" Only need to call 'require' once when not executing via an autocommand

" lua require('lspconfig').clangd.setup{}
" lua require('lspconfig').pyright.setup{}

Python Code for testing

The LSP should show '"var" is not defined'

var

C++ Code for testing

The LSP should show 'Use of undeclared identifier 'endl'; did you mean 'std::endl'? (fix available)

#include <iostream>

int main()
{
    cout << "hello" << endl;
}

Health check

Checkhealth result

LSP log

Log file Not executing request ``` [ START ] 2021-08-12T20:39:46-0500 ] LSP logging initiated [ INFO ] 2021-08-12T20:39:56-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp.lua:1215 ] "exit_handler" {} ``` When executing the request once. ``` [ START ] 2021-08-12T21:08:43-0500 ] LSP logging initiated [ INFO ] 2021-08-12T21:08:50-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp.lua:1215 ] "exit_handler" {} ``` <-- When executing twice ``` [ START ] 2021-08-12T20:56:47-0500 ] LSP logging initiated [ INFO ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:316 ] "Starting RPC client" { args = { "--background-index" }, cmd = "clangd", extra = {}} [ DEBUG ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp.lua:828 ] "LSP[clangd]" "initialize_params" { capabilities = { callHierarchy = { dynamicRegistration = false }, offsetEncoding = { "utf-8", "utf-16" }, textDocument = { codeAction = { codeActionLiteralSupport = { codeActionKind = { valueSet = { "", "Empty", "QuickFix", "Refactor", "RefactorExtract", "RefactorInline", "RefactorRewrite", "Source", "SourceOrganizeImports", "quickfix", "refactor", "refactor.extract", "refactor.inline", "refactor.rewrite", "source", "source.organizeImports" } } }, dynamicRegistration = false }, 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, editsNearCursor = true }, 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 } }, clientInfo = { name = "Neovim", version = "0.6.0" }, initializationOptions = vim.empty_dict(), processId = 33797, rootPath = "", rootUri = "file:///", trace = "off", workspaceFolders = { { name = "", uri = "file:///" } }} [ DEBUG ] 2021-08-12T20:56:47-0500 ] .../nvim/runtime/lua/vim/lsp/rpc.lua:395 ] "rpc.send.payload" { id = 1, jsonrpc = "2.0", method = "initialize", params = { capabilities = { callHierarchy = { dynamicRegistration = false }, offsetEncoding = { "utf-8", "utf-16" }, textDocument = { codeAction = { codeActionLiteralSupport = { codeActionKind = { valueSet = { "", "Empty", "QuickFix", "Refactor", "RefactorExtract", "RefactorInline", "RefactorRewrite", "Source", "SourceOrganizeImports", "quickfix", "refactor", "refactor.extract", "refactor.inline", "refactor.rewrite", "source", "source.organizeImports" } } }, dynamicRegistration = false }, 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, editsNearCursor = true }, 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 } }, clientInfo = { name = "Neovim", version = "0.6.0" }, initializationOptions = vim.empty_dict(), processId = 33797, rootPath = "", rootUri = "file:///", trace = "off", workspaceFolders = { { name = "", uri = "file:///t" } } }} [ ERROR ] 2021-08-12T20:56:47-0500 ] .../nvim/runtime/lua/vim/lsp/rpc.lua:462 ] "rpc" "clangd" "stderr" "I[20:56:47.482] clangd version 12.0.1\nI[20:56:47.482] PID: 33799\nI[20:56:47.482] Working directory: /build/bin\nI[20:56:47.482] argv[0]: clangd\nI[20:56:47.482] argv[1]: --background-index\n" [ ERROR ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:462 ] "rpc" "clangd" "stderr" "I[20:56:47.483] Starting LSP over stdin/stdout\n" [ ERROR ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:462 ] "rpc" "clangd" "stderr" "I[20:56:47.483] <-- initialize(1)\n" [ ERROR ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:462 ] "rpc" "clangd" "stderr" "I[20:56:47.484] --> reply:initialize(1) 1 ms\n" [ DEBUG ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:496 ] "decoded" { id = 1, jsonrpc = "2.0", result = { capabilities = { astProvider = true, callHierarchyProvider = true, codeActionProvider = { codeActionKinds = { "quickfix", "refactor", "info" } }, compilationDatabase = { automaticReload = true }, completionProvider = { allCommitCharacters = { " ", "\t", "(", ")", "[", "]", "{", "}", "<", ">", ":", ";", ",", "+", "-", "/", "*", "%", "^", "&", "#", "?", ".", "=", '"', "'", "|" }, resolveProvider = false, triggerCharacters = { ".", "<", ">", ":", '"', "/" } }, declarationProvider = true, definitionProvider = true, documentFormattingProvider = true, documentHighlightProvider = true, documentLinkProvider = { resolveProvider = false }, documentOnTypeFormattingProvider = { firstTriggerCharacter = "\n", moreTriggerCharacter = {} }, documentRangeFormattingProvider = true, documentSymbolProvider = true, executeCommandProvider = { commands = { "clangd.applyFix", "clangd.applyTweak" } }, hoverProvider = true, implementationProvider = true, memoryUsageProvider = true, referencesProvider = true, renameProvider = { prepareProvider = true }, selectionRangeProvider = true, semanticTokensProvider = { full = { delta = true }, legend = { tokenModifiers = {}, tokenTypes = { "variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "enum", "enumMember", "type", "dependent", "dependent", "namespace", "typeParameter", "concept", "type", "macro", "comment" } }, range = false }, signatureHelpProvider = { triggerCharacters = { "(", "," } }, textDocumentSync = { change = 2, openClose = true, save = true }, typeHierarchyProvider = true, workspaceSymbolProvider = true }, offsetEncoding = "utf-8", serverInfo = { name = "clangd", version = "clangd version 12.0.1" } }} [ DEBUG ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:395 ] "rpc.send.payload" { jsonrpc = "2.0", method = "initialized", params = { [true] = 6 }} [ DEBUG ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp.lua:855 ] "LSP[clangd]" "server_capabilities" { astProvider = true, callHierarchyProvider = true, codeActionProvider = { codeActionKinds = { "quickfix", "refactor", "info" } }, compilationDatabase = { automaticReload = true }, completionProvider = { allCommitCharacters = { " ", "\t", "(", ")", "[", "]", "{", "}", "<", ">", ":", ";", ",", "+", "-", "/", "*", "%", "^", "&", "#", "?", ".", "=", '"', "'", "|" }, resolveProvider = false, triggerCharacters = { ".", "<", ">", ":", '"', "/" } }, declarationProvider = true, definitionProvider = true, documentFormattingProvider = true, documentHighlightProvider = true, documentLinkProvider = { resolveProvider = false }, documentOnTypeFormattingProvider = { firstTriggerCharacter = "\n", moreTriggerCharacter = {} }, documentRangeFormattingProvider = true, documentSymbolProvider = true, executeCommandProvider = { commands = { "clangd.applyFix", "clangd.applyTweak" } }, hoverProvider = true, implementationProvider = true, memoryUsageProvider = true, referencesProvider = true, renameProvider = { prepareProvider = true }, selectionRangeProvider = true, semanticTokensProvider = { full = { delta = true }, legend = { tokenModifiers = {}, tokenTypes = { "variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "enum", "enumMember", "type", "dependent", "dependent", "namespace", "typeParameter", "concept", "type", "macro", "comment" } }, range = false }, signatureHelpProvider = { triggerCharacters = { "(", "," } }, textDocumentSync = { change = 2, openClose = true, save = true }, typeHierarchyProvider = true, workspaceSymbolProvider = true} [ INFO ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp.lua:856 ] "LSP[clangd]" "initialized" { resolved_capabilities = { call_hierarchy = true, code_action = { codeActionKinds = { "quickfix", "refactor", "info" } }, code_lens = false, code_lens_resolve = false, completion = true, declaration = true, document_formatting = true, document_highlight = true, document_range_formatting = true, 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 = { "(", "," }, text_document_did_change = 2, text_document_open_close = true, text_document_save = true, text_document_save_include_text = false, text_document_will_save = false, text_document_will_save_wait_until = false, type_definition = false, workspace_folder_properties = { changeNotifications = false, supported = false }, workspace_symbol = true }} [ DEBUG ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:395 ] "rpc.send.payload" { jsonrpc = "2.0", method = "textDocument/didOpen", params = { textDocument = { languageId = "cpp", text = '#include \n\nint main() \n{\n\tint x = 32;\n\n\tstd::cout << "hello" << std::endl;\n}\n', uri = "file:///", version = 0 } }} [ DEBUG ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/handlers.lua:455 ] "default_handler" "textDocument/publishDiagnostics" { bufnr = 1, client_id = 1, params = { diagnostics = {}, uri = "file:///" }} [ ERROR ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:462 ] "rpc" "clangd" "stderr" "I[20:56:47.491] <-- initialized\nI[20:56:47.491] <-- textDocument/didOpen\n" [ ERROR ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:462 ] "rpc" "clangd" "stderr" "I[20:56:47.492] Failed to find compilation database for \n" [ ERROR ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:462 ] "rpc" "clangd" "stderr" "I[20:56:47.492] ASTWorker building file version 0 with command clangd fallback\n[]\n/usr/bin/clang -fsyntax-only -resource-dir=/usr/lib/clang/12.0.1\n" [ ERROR ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:462 ] "rpc" "clangd" "stderr" "I[20:56:47.978] --> textDocument/publishDiagnostics\n" [ DEBUG ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:496 ] "decoded" { jsonrpc = "2.0", method = "textDocument/publishDiagnostics", params = { diagnostics = {}, uri = "file:///", version = 0 }} [ DEBUG ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp.lua:676 ] "notification" "textDocument/publishDiagnostics" { diagnostics = {}, uri = "file:///", version = 0} [ DEBUG ] 2021-08-12T20:56:47-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/handlers.lua:455 ] "default_handler" "textDocument/publishDiagnostics" { client_id = 1, params = { diagnostics = {}, uri = "file:///", version = 0 }} [ DEBUG ] 2021-08-12T20:56:48-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:395 ] "rpc.send.payload" { jsonrpc = "2.0", method = "textDocument/didClose", params = { textDocument = { uri = "file:///" } }} [ ERROR ] 2021-08-12T20:56:48-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:462 ] "rpc" "clangd" "stderr" "I[20:56:48.898] <-- textDocument/didClose\n" [ ERROR ] 2021-08-12T20:56:48-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:462 ] "rpc" "clangd" "stderr" "I[20:56:48.898] --> textDocument/publishDiagnostics\n" [ DEBUG ] 2021-08-12T20:56:48-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/rpc.lua:496 ] "decoded" { jsonrpc = "2.0", method = "textDocument/publishDiagnostics", params = { diagnostics = {}, uri = "file:///" }} [ DEBUG ] 2021-08-12T20:56:48-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp.lua:676 ] "notification" "textDocument/publishDiagnostics" { diagnostics = {}, uri = "file:///"} [ DEBUG ] 2021-08-12T20:56:48-0500 ] .../bin/share/nvim/runtime/lua/vim/lsp/handlers.lua:455 ] "default_handler" "textDocument/publishDiagnostics" { client_id = 1, params = { diagnostics = {}, uri = "file:///" }} ```
mjlbach commented 3 years ago

You are lazy loading a lazy load. Your filetype autocommand is calling a function which maps a filetype autocommand which can't be called because the buffer filetype trigger was already called (and which mapped it).

Cartogy commented 3 years ago

I see. Thanks for the quick response. There is a lot for me to learn.

Congee commented 1 year ago

Those who use lazy.nvim could face the same problem https://github.com/folke/lazy.nvim/issues/880 for the same reason