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

The diagnostics of sumneko_lua does not appear after :e #946

Closed
nyngwang opened this issue Feb 8, 2022 · 4 comments
Closed

The diagnostics of sumneko_lua does not appear after :e #946

nyngwang opened this issue Feb 8, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@nyngwang
Copy link

nyngwang commented Feb 8, 2022

confirmed from neovim/nvim-lspconfig#1701

Update

To save your time read the first comment by @mjlbach below: #946 (comment)

Description

As title. How to make things clear? This is the DEMO:

(This might be helpful but I'm not sure: neovim/nvim-lspconfig#1192)

lua_bug.mov

To Reproduce

As shown in the DEMO.

Expected behavior

As shown in the DEMO.

Environment (please complete the following information):

  • OS: macOS Big Sur 11.6.2
  • Is WSL remote? No
  • Client: neovim

Additional context
Add any other context about the problem here.

Neovim version:

NVIM v0.7.0-dev+1036-gbcea73299
Build type: Release
LuaJIT 2.1.0-beta3

nvim-lspconfig, which commit used

nvim-lspconfig/commit/2008c5cebf2b84c5e5f8a566480b022ab2e7ebab

LSP log


[WARN][2022-02-09 06:09:02] .../lua/vim/lsp.lua:812	"server_request: no handler found for"	"workspace/semanticTokens/refresh"
[WARN][2022-02-09 06:09:02] .../lua/vim/lsp.lua:812	"server_request: no handler found for"	"workspace/semanticTokens/refresh"
[WARN][2022-02-09 06:09:02] .../lua/vim/lsp.lua:812	"server_request: no handler found for"	"workspace/semanticTokens/refresh"
[WARN][2022-02-09 06:09:02] .../lua/vim/lsp.lua:812	"server_request: no handler found for"	"workspace/semanticTokens/refresh"
[START][2022-02-09 06:09:29] LSP logging initiated
[WARN][2022-02-09 06:09:30] .../lua/vim/lsp.lua:812	"server_request: no handler found for"	"workspace/semanticTokens/refresh"
[WARN][2022-02-09 06:09:30] .../lua/vim/lsp.lua:812	"server_request: no handler found for"	"workspace/semanticTokens/refresh"
[START][2022-02-09 06:14:08] LSP logging initiated
[WARN][2022-02-09 06:14:13] .../lua/vim/lsp.lua:812	"server_request: no handler found for"	"workspace/semanticTokens/refresh"
[WARN][2022-02-09 06:14:13] .../lua/vim/lsp.lua:812	"server_request: no handler found for"	"workspace/semanticTokens/refresh"
@mjlbach
Copy link
Contributor

mjlbach commented Feb 8, 2022

This isn't a clear issue description... lua-language-server does not have an :e command because it is not a text editor, it is a language server. You needed to extract the information I gave you when you filed the bug report against lspconfig instead of copy/pasting your issue to another issue tracker.

Here is a more appropriate issue description.

Servers by convention send diagnostics when text document's are opened. lua-language-server only sends diagnostics when the buffer is reopened (textDocument/didClose followed by textDocument/didOpen) and the first textDocument/didChange notification is sent. gopls has a similar issue I reported:

golang/go#50267

Here is the RPC log, note the diagnostics are only returned after sending the first didChange notification after the client sends textDocument/didClose followed by textDocument/didOpen:

[DEBUG][2022-02-08 14:21:52] .../vim/lsp/rpc.lua:347	"rpc.send"	{
  jsonrpc = "2.0",
  method = "textDocument/didClose",
  params = {
    textDocument = {
      uri = "file:///Users/michael/Repositories/nvim-lspconfig/test/minimal_init.lua"
    }
  }
}
[DEBUG][2022-02-08 14:21:52] .../vim/lsp/rpc.lua:347	"rpc.send"	{
  jsonrpc = "2.0",
  method = "textDocument/didOpen",
  params = {
    textDocument = {
      languageId = "lua",
      text = "local on_windows = vim.loop.os_uname().version:match 'Windows'\n\nlocal function join_paths(...)\n  local path_sep = on_windows and '\\\\' or '/'\n  local result = table.concat({ ... }, path_sep)\n  return result\nend\n\nvim.cmd [[set runtimepath=$VIMRUNTIME]]\n\nlocal temp_dir = vim.loop.os_getenv 'TEMP' or '/tmp'\n\nvim.cmd('set packpath=' .. join_paths(temp_dir, 'nvim', 'site'))\n\nlocal package_root = join_paths(temp_dir, 'nvim', 'site', 'pack')\nlocal install_path = join_paths(package_root, 'packer', 'start', 'packer.nvim')\nlocal compile_path = join_paths(install_path, 'plugin', 'packer_compiled.lua')\n\nlocal function load_plugins()\n  require('packer').startup {\n    {\n      'wbthomason/packer.nvim',\n      'neovim/nvim-lspconfig',\n    },\n    config = {\n      package_root = package_root,\n      compile_path = compile_path,\n    },\n  }\nend\n\n_G.load_config = function()\n  vim.lsp.set_log_level 'trace'\n  if vim.fn.has 'nvim-0.5.1' == 1 then\n    require('vim.lsp.log').set_format_func(vim.inspect)\n  end\n  local nvim_lsp = require 'lspconfig'\n  local on_attach = function(_, bufnr)\n    local function buf_set_keymap(...)\n      vim.api.nvim_buf_set_keymap(bufnr, ...)\n    end\n    local function buf_set_option(...)\n      vim.api.nvim_buf_set_option(bufnr, ...)\n    end\n\n    buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')\n\n    -- Mappings.\n    local opts = { noremap = true, silent = true }\n    buf_set_keymap('n', 'gD', '<Cmd>lua vim.lsp.buf.declaration()<CR>', opts)\n    buf_set_keymap('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts)\n    buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', opts)\n    buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)\n    buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)\n    buf_set_keymap('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)\n    buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)\n    buf_set_keymap('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)\n    buf_set_keymap('n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)\n    buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)\n    buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)\n    buf_set_keymap('n', '<space>e', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>', opts)\n    buf_set_keymap('n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts)\n    buf_set_keymap('n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', opts)\n    buf_set_keymap('n', '<space>q', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>', opts)\n  end\n\n  -- Add the server that troubles you here\n  local name = 'sumneko_lua'\n  if not name then\n    print 'You have not defined a server name, please edit minimal_init.lua'\n  end\n  if not nvim_lsp[name].document_config.default_config.cmd and not cmd then\n    print [[You have not defined a server default cmd for a server\n      that requires it please edit minimal_init.lua]]\n  end\n\n  nvim_lsp[name].setup {\n    cmd = cmd,\n    on_attach = on_attach,\n  }\n\n  print [[You can find your log at $HOME/.cache/nvim/lsp.log. Please paste in a github issue under a details tag as described in the issue template.]]\nend\n\nif vim.fn.isdirectory(install_path) == 0 then\n  vim.fn.system { 'git', 'clone', 'https://github.com/wbthomason/packer.nvim', install_path }\n  load_plugins()\n  require('packer').sync()\n  vim.cmd [[autocmd User PackerComplete ++once lua load_config()]]\nelse\n  load_plugins()\n  require('packer').sync()\n  _G.load_config()\nend\n",
      uri = "file:///Users/michael/Repositories/nvim-lspconfig/test/minimal_init.lua",
      version = 0
    }
  }
}
[DEBUG][2022-02-08 14:21:53] .../vim/lsp/rpc.lua:347	"rpc.send"	{
  jsonrpc = "2.0",
  method = "textDocument/didChange",
  params = {
    contentChanges = { {
        range = {
          end = {
            character = 0,
            line = 13
          },
          start = {
            character = 0,
            line = 13
          }
        },
        rangeLength = 0,
        text = "e"
      } },
    textDocument = {
      uri = "file:///Users/michael/Repositories/nvim-lspconfig/test/minimal_init.lua",
      version = 6
    }
  }
}
[DEBUG][2022-02-08 14:21:53] .../vim/lsp/rpc.lua:454	"rpc.receive"	{
  jsonrpc = "2.0",
  method = "$/status/report",
  params = {
    text = "😺Lua",
    tooltip = "Workspace   : /Users/michael/Repositories/nvim-lspconfig\nCached files: 147/146\nMemory usage: 15M"
  }
}
[TRACE][2022-02-08 14:21:53] .../lua/vim/lsp.lua:792	"notification"	"$/status/report"	{
  text = "😺Lua",
  tooltip = "Workspace   : /Users/michael/Repositories/nvim-lspconfig\nCached files: 147/146\nMemory usage: 15M"
}
[DEBUG][2022-02-08 14:21:53] .../vim/lsp/rpc.lua:454	"rpc.receive"	{
  jsonrpc = "2.0",
  method = "textDocument/publishDiagnostics",
  params = {
    diagnostics = { {
        code = "exp-in-action",
        data = "syntax",
        message = "Unexpected <exp> .",
        range = {
          end = {
            character = 1,
            line = 13
          },
          start = {
            character = 0,
            line = 13
          }
        },
        severity = 1,
        source = "Lua Syntax Check."
      }, {
        code = "undefined-global",
        message = "Undefined global `vim`.",
        range = {
          end = {
            character = 3,
            line = 8
          },
          start = {
            character = 0,
            line = 8
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `vim`.",
        range = {
          end = {
            character = 3,
            line = 12
          },
          start = {
            character = 0,
            line = 12
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `vim`.",
        range = {
          end = {
            character = 5,
            line = 88
          },
          start = {
            character = 2,
            line = 88
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `vim`.",
        range = {
          end = {
            character = 20,
            line = 10
          },
          start = {
            character = 17,
            line = 10
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `vim`.",
        range = {
          end = {
            character = 5,
            line = 32
          },
          start = {
            character = 2,
            line = 32
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `vim`.",
        range = {
          end = {
            character = 5,
            line = 85
          },
          start = {
            character = 2,
            line = 85
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `cmd`.",
        range = {
          end = {
            character = 70,
            line = 71
          },
          start = {
            character = 67,
            line = 71
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `cmd`.",
        range = {
          end = {
            character = 13,
            line = 77
          },
          start = {
            character = 10,
            line = 77
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `vim`.",
        range = {
          end = {
            character = 6,
            line = 84
          },
          start = {
            character = 3,
            line = 84
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `vim`.",
        range = {
          end = {
            character = 46,
            line = 34
          },
          start = {
            character = 43,
            line = 34
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `vim`.",
        range = {
          end = {
            character = 22,
            line = 0
          },
          start = {
            character = 19,
            line = 0
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `vim`.",
        range = {
          end = {
            character = 8,
            line = 33
          },
          start = {
            character = 5,
            line = 33
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `vim`.",
        range = {
          end = {
            character = 9,
            line = 39
          },
          start = {
            character = 6,
            line = 39
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      }, {
        code = "undefined-global",
        message = "Undefined global `vim`.",
        range = {
          end = {
            character = 9,
            line = 42
          },
          start = {
            character = 6,
            line = 42
          }
        },
        severity = 2,
        source = "Lua Diagnostics."
      } },
    uri = "file:///Users/michael/Repositories/nvim-lspconfig/test/minimal_init.lua",
    version = 6
  }
}

@sumneko
Copy link
Collaborator

sumneko commented Feb 9, 2022

The specification says:
if a language has a project system (for example C#) diagnostics are not cleared when a file closes.
If the computed set is empty it has to push the empty array to clear former diagnostics.

My understanding is that when you start the language server with a workspace, closing a file would not clear the diagnostics for that file.
When you reopen the file, the new diagnostics I calculated are exactly the same as before, so no diagnostics are re-sent to the client.

@mjlbach
Copy link
Contributor

mjlbach commented Feb 9, 2022

I believe contextually this is more referring to the server's responsibility for clearing rather than presenting diagnostics (embedding the full quote here for discussion).

Diagnostics are “owned” by the server so it is the server’s responsibility to clear them if necessary. The following rule is used for VS Code servers that generate diagnostics:

if a language is single file only (for example HTML) then diagnostics are cleared by the server when the file is closed. Please note that open / close events don’t necessarily reflect what the user sees in the user interface. These events are ownership events. So with the current version of the specification it is possible that problems are not cleared although the file is not visible in the user interface since the client has not closed the file yet.
if a language has a project system (for example C#) diagnostics are not cleared when a file closes. When a project is opened all diagnostics for all files are recomputed (or read from a cache).

I think that from a symmetry standpoint it is more elegant that when a client sends the didClose notification, from the server's point of view, there is no internal state/assumption that the client is caching previous diagnostics for that buffer. Instead didOpen/didClose/didOpen behaves the same was as the first didOpen.

I agree the spec is a little ambiguous, but my reading the above does not rule out sending diagnostics for a given URI on didOpen, more about not clearing diagnostics when didClose is sent.

@sumneko sumneko added the bug Something isn't working label Feb 9, 2022
@sumneko sumneko closed this as completed in cea9695 Feb 9, 2022
@nyngwang
Copy link
Author

@sumneko @mjlbach Many thanks for you!

sumneko added a commit that referenced this issue Apr 22, 2022
Clear cache when `didOpen`. This avoids clearing the cache according to the situation in the diagnostic business.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants