DEV Community

abdellah ht
abdellah ht

Posted on

Solve nvim lsp denols vs tsserver clash

Recently I started using Deno for some personal projects, and when I installed the Deno lsp client on neovim, it clashed with tsserver as it has no idea what Deno types are and some other annoying diagnostics.

The official documentation instructs you to specify the root_dir param in the server:setup(options), but I thought that was a lot of hassle as it would not work for when you don't have a Deno project, but have no package.json either.

My solution was to use a programmatic way to have fine grained control over when to start which. Basically when tsserver wants to start, it checks if denols is in the active clients (which you can get using vim.lsp.get_active_clients()) and not continue setup, and denols does the same check to see if tsserver is attached to a file that belongs to it and kills it.

here's the full snippet:

      local active_clients = vim.lsp.get_active_clients()
      if client.name == 'denols' then
        for _, client_ in pairs(active_clients) do
          -- stop tsserver if denols is already active
          if client_.name == 'tsserver' then
            client_.stop()
          end
        end
      elseif client.name == 'tsserver' then
        for _, client_ in pairs(active_clients) do
          -- prevent tsserver from starting if denols is already active
          if client_.name == 'denols' then
            client.stop()
          end
        end
      end
Enter fullscreen mode Exit fullscreen mode

I hope it saves you some time.

Top comments (6)

Collapse
 
bigusbeckus profile image
Beck

Made an account just to say thanks!

I dumped this into my on_attach and so far it's working beautifully!

Very new to nvim so I'm not sure if there's a better way to handle tsserver and denols conflicts by now, but I'd love to hear it if there is :)

Collapse
 
_hariti profile image
abdellah ht

I just hacked it after a long google search with no satisfying results

There could be a better solution for this

Collapse
 
acro5piano profile image
Kay Gosho

Thank you for posting this. It helped me much!

Combining @beck 's solution, my code is like this:

lspconfig.denols.setup({
  root_dir = lspconfig.util.root_pattern("deno.json"),
  init_options = {
    lint = true,
    unstable = true,
    suggest = {
      imports = {
        hosts = {
          ["https://deno.land"] = true,
          ["https://cdn.nest.land"] = true,
          ["https://crux.land"] = true,
        },
      },
    },
  },
  on_attach = function()
    local active_clients = vim.lsp.get_active_clients()
    for _, client in pairs(active_clients) do
      -- stop tsserver if denols is already active
      if client.name == "tsserver" then
        client.stop()
      end
    end
  end,
})
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mebble profile image
Neil Syiemlieh

This is why I love the internet

Collapse
 
davidecavaliere profile image
Davide Cavaliere • Edited

I think I found a better approach πŸŽ‰
dev.to/davidecavaliere/avoid-confl...

Collapse
 
azillion profile image
alexander zillion

Thanks for this!