Require("lspconfig.configs").some_conf returns nill for all configs, eg `cssls`. Need troubleshoot advice!

Hi!

I am trying to troubleshoot the latest unreleased version of doom-nvim. The issue
is described below under the problem heading.

The branch I am using is this one for should anyone be interested: https://github.com/molleweide/doom-nvim/tree/moll_modular

Doom Nvim crash report

Report date: 2022-03-10 01:20:08

System and Doom Nvim information

  • OS: Darwin
  • Neovim version: 0.6.1
  • Doom Nvim information:
  • version: 4.0.0-alpha1
  • doom_root variable: /Users/hjalmarjakobsson/.config/doom-nvim
  • doom_configs_root variable: /Users/hjalmarjakobsson/.config/doom-nvim

problem: require("lspconfig.configs").some_server_name returns nil for all servers

I am trying to problem solve why overriding the pre-configured servers generate an error.
I am fairly new to lsp stuff but it seems to me that everything in the function below should be correct.

I recall commenting (**) out, reloading, and then toggle it back in again once, and then all of the server
configs became accessible but I haven’t been able to reproduce this, which I guess makes sense because otherwise
I wouldn’t be writing this :smiley:

If I run:

:lua print(vim.insperct(require("lspconfig.configs)))

this returns

{
  <metatable> = {
    __newindex = <function 1>
  }
}

but if I do:

:lua print(vim.insperct(require("lspconfig.configs).cssls))

it just returns nil.

my question:

is there anything in particular that could be recommended for me to problem solve this?
As I mentionned above I am an intermediate person when it comes to nvim configs so I don’t know atm
how I should troubleshoot this.

packer info

  ["nvim-lspconfig"] = {
    "neovim/nvim-lspconfig",
    commit = "e7df7ecae0b0d2f997ea65e951ddbe98ca3e154b",
    opt = true,
    cmd = {
      "LspStart",
      "LspRestart",
      "LspStop",
    },
    module = "lspconfig",
  },

this is the helper function used to setup lsp servers


module.use_lsp = function(lsp_name, opts)
  local lsp = require('lspconfig')
  local lsp_configs = require("lspconfig.configs")


  -- Apply or merge lsp configs -- (**) THIS IS WHERE THE ERR HAPPENS.
  local config_name = opts.name and opts.name or lsp_name
  -- log.info("lsp_name:", lsp_name)
  -- log.info("opts.name:", opts.name)
  -- log.info("config_name:", config_name)
  -- log.info("lsp_configs["..config_name.."] -> ", lsp_configs[config_name]) -- <<<<< returns nil for all configs here?
  if opts.config then
    if lsp_configs[config_name] then
      lsp_configs[config_name] = vim.tbl_deep_extend('force', lsp_configs[config_name], opts.config)
    else
      lsp_configs[config_name] = opts.config -- <<<<<<<<< enters here for all servers, even though they come predefined in lspconfig.configs??
    end
  end


  -- Start server and bind to buffers
  local start_lsp = function()
    log.info("start_lsp > " .. config_name)
    lsp[config_name].setup({})
    local server = lsp[config_name]
    log.info(vim.inspect(server))
    local buffer_handler = server.filetypes and server.manager.try_add_wrapper or server.manager.try_add
    for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
      buffer_handler(bufnr)
    end
  end



  -- Auto install if possible
  if not utils.is_plugin_disabled('auto_install') and not opts.no_installer then
    log.info("lsp >> if auto_install")
    local lsp_installer = require("nvim-lsp-installer.servers")
    local server_available, server = lsp_installer.get_server(lsp_name)
    -- log.info(vim.inspect(server))
    if server_available then
      lsp_configs[config_name].cmd_env = server:get_default_options().cmd_env
      if not server:is_installed() then
        vim.defer_fn(function()
          server:install()
        end, 50)
      end

      server:on_ready(function()
        start_lsp()
      end)
    end
  else
    log.info("lsp >> else start_lsp")
    start_lsp()
  end
end

output of log statements

[doom] [INFO  01:24:39] langs_utils.lua:34: lsp_name: cssls
[doom] [INFO  01:24:39] langs_utils.lua:35: opts.name: nil
[doom] [INFO  01:24:39] langs_utils.lua:36: config_name: cssls
[doom] [INFO  01:24:39] langs_utils.lua:37: lsp_configs[cssls] ->  nil

error message that is displayed


Error detected while processing BufWritePost Autocommands for "*/doom-nvim/config.lua":
E5108: Error executing lua ...pack/packer/opt/nvim-lspconfig/lua/lspconfig/configs.lua:8: default_config: expected table, got nil
stack traceback:
        [C]: in function 'error'
        vim/shared.lua:608: in function 'validate'
        ...pack/packer/opt/nvim-lspconfig/lua/lspconfig/configs.lua:8: in function '__newindex'
        ...k/cheovim/start/cheovim/lua/doom/modules/langs_utils.lua:44: in function 'use_lsp'
        ...ck/cheovim/start/cheovim/lua/doom/modules/css/config.lua:18: in main chunk
        [C]: in function 'require'
        ...e/pack/cheovim/start/cheovim/lua/doom/utils/reloader.lua:134: in function 'reload_lua_module'
        ...e/pack/cheovim/start/cheovim/lua/doom/utils/reloader.lua:159: in function 'reload_lua_modules'
        ...e/pack/cheovim/start/cheovim/lua/doom/utils/reloader.lua:208: in function 'reload_configs'
        ...e/pack/cheovim/start/cheovim/lua/doom/utils/reloader.lua:225: in function 'full_reload'
        ...cheovim/start/cheovim/lua/doom/modules/core/autocmds.lua:8: in function <...cheovim/start/cheovim/lua/doom/modules/core/autocmds.lua:8>
        [string ":lua"]:1: in main chunk


hmm, i just learnt that this syntax works instead local lsp_configs = require'lspconfig.configs'. But why only this syntax?

Edit. no now its back to were it started. it worked once but I dont know why.

1 Like

Configs is the list of loaded configs, you need to have called setup on a given server for it to be populated in the table.

Thanks for replying to me with this! I am going to re-read the code and figure it out now.

does this mean that I need to setup with the default config before I can override a servers configs?

That’s the point of setup {}, pass the keys you want to override into the table that setup is called with (re-read the lspconfig documentation)

1 Like

does this make sense to your eyes? (yes/no)

  ...
  ...
  lsp[config_name].setup({}) 

  if opts.config then
    if lsp_configs[config_name] then
    lsp_configs[config_name] = vim.tbl_deep_extend('force', lsp_configs[config_name], override_config)
  else
    lsp_configs[config_name] = opts.config -- if new custom server
    end
  end
  ...

taken from this larger func

module.use_lsp = function(lsp_name, opts)
  local lsp = require('lspconfig')
  local lsp_configs = require("lspconfig.configs")

  -- Apply or merge lsp configs
  local config_name = opts.name and opts.name or lsp_name

  lsp[config_name].setup({})

  if opts.config then
    if lsp_configs[config_name] then
    lsp_configs[config_name] = vim.tbl_deep_extend('force', lsp_configs[config_name], opts.config)
  else
    lsp_configs[config_name] = opts.config -- if new custom server
    end
  end


  -- Start server and bind to buffers
  local start_lsp = function()
    -- lsp[config_name].setup({})
    local server = lsp[config_name]
    local buffer_handler = server.filetypes and server.manager.try_add_wrapper or server.manager.try_add
    for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
      buffer_handler(bufnr)
    end
  end

  -- Auto install if possible
  if not utils.is_plugin_disabled('auto_install') and not opts.no_installer then
    local lsp_installer = require("nvim-lsp-installer.servers")
    local server_available, server = lsp_installer.get_server(lsp_name)
    if server_available then
      lsp_configs[config_name].cmd_env = server:get_default_options().cmd_env
      if not server:is_installed() then
        vim.defer_fn(function()
          server:install()
        end, 50)
      end

      server:on_ready(function()
        start_lsp()
      end)
    end
  else
    start_lsp()
  end
end

no, you need to pass those options directly into setup, as I stated, overriding them post-hoc will not achieve what you are trying to achieve (initialization options and workspace configuration settings will have already be sent to the server)

1 Like

Thank you very much for adding this extra comment. Much appreciated!