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

nvim-lspconfig shortcircuits if on_attach contains vim.api.nvim_exec2 statements #2731

Open
jeanlucthumm opened this issue Jul 22, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@jeanlucthumm
Copy link

Description

If the user defined on_attach functions contains a vim.api.nvim_exec2 statement (see minimum repro example below), then nvim-lspconfig will mysteriously and without error stop execution on this line and not proceed:

client.config._on_attach(client, bufnr)

This is bad because the next couple of lines register the user passed commands:

if client.config.commands and not vim.tbl_isempty(client.config.commands) then
M.commands = vim.tbl_deep_extend('force', M.commands, client.config.commands)
end
if not M.commands_created and not vim.tbl_isempty(M.commands) then
util.create_module_commands(config_name, M.commands)
end

So this means that the commands field is completely ignored, and this is the reason that my rust-tools had none of its commands on start up (took way too long to root cause 😳)

Neovim version

NVIM v0.9.0-dev-1319+g10baf8971
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3

Nvim-lspconfig version

dd11ba7

Operating system and version

Linux main 6.4.3-zen1-2-zen #1 ZEN SMP PREEMPT_DYNAMIC Sat, 15 Jul 2023 20:18:15 +0000 x86_64 GNU/Linux

Affected language servers

all

Steps to reproduce

  1. nvim -u min.lua sample.rs (need a sample rust file to trigger rust_analyzer)
  2. Try :SampleCommand

Actual behavior

E492: Not an editor command: SampleCommand

Expected behavior

hello world

i.e. SampleCommand was defined

Minimal config

local opt = vim.opt
local api = vim.api
local fn = vim.fn

--- Lazy bootstrap
local lazypath = fn.stdpath('data') .. '/lazy/lazy.nvim'
if not vim.loop.fs_stat(lazypath) then
  vim.notify('Bootstraping lazy.nvim...')
  fn.system({
    'git',
    'clone',
    '--filter=blob:none',
    'https://github.com/folke/lazy.nvim.git',
    '--branch=stable', -- latest stable release
    lazypath,
  })
end
opt.rtp:prepend(lazypath)

-- Problematic function
local function bad_on_attach()
  -- This statement causes SampleCommand to not be registered.
  -- Comment it out and it will regsiter
  api.nvim_exec2[[
    augroup lsp_document_highlight
    autocmd! * <buffer>
    autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
    autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
    augroup END
  ]]
end

require'lazy'.setup {
  {
    'neovim/nvim-lspconfig',
    config = function()
      local lspconfig = require'lspconfig'
      lspconfig.rust_analyzer.setup {
        on_attach = bad_on_attach,
        commands = {
          SampleCommand = {
            function()
              print('hello world')
            end,
          },
        },
      }
    end,
  },
}

LSP log

see minimal lua

@sigmaSd
Copy link
Contributor

sigmaSd commented Jul 23, 2023

nvim_exec2 takes 2 arguments not one thats why its failing

@jeanlucthumm
Copy link
Author

Ah you're right, just tried it manually via :lua vim.api.nvim_exec2('echo hello'):

E5108: Error executing lua [string ":lua"]:1: Expected 2 arguments                                                                                                                                                   
stack traceback:                                                                                                                                                                                                     
        [C]: in function 'nvim_exec2'                                                                                                                                                                                
        [string ":lua"]:1: in main chunk  

But for this issue, I never got an error message. Execution just stopped silently.

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

2 participants