DEV Community

Davide Cavaliere
Davide Cavaliere

Posted on • Edited on

Avoid conflicts between denols and tsserver in Neovim

Recently I've been working on a project in a monorepo were I've got nodejs/typescript and deno projects. This is notoriously annoying because any ts file will start both tsserver and denols language services.

If you've got here you know what I'm talking about 😓

I'm setting up my lsp with lspconfig.

This is how denols is configured

lspconfig.denols.setup({
  root_dir = lspconfig.util.root_pattern("deno.json", "deno.jsonc"),
  init_options = {
    lint = true,
    unstable = true,
    suggest = {
      imports = {
        hosts = {
          ["https://deno.land"] = true,
          ["https://cdn.nest.land"] = true,
          ["https://crux.land"] = true,
        },
      },
    },
  },

  on_attach = on_attach,
})

Enter fullscreen mode Exit fullscreen mode

And this is the hack to avoid tsserver to start on a file that belongs to a deno project.

spconfig.tsserver.setup({
  on_attach = function (client, bufnr)
    on_attach(client, bufnr);
    vim.keymap.set('n', '<leader>ro', function()
      vim.lsp.buf.execute_command({
        command = "_typescript.organizeImports",
        arguments = { vim.fn.expand("%:p") }
      })
    end, { buffer = bufnr,  remap = false });
  end,
  root_dir = function (filename, bufnr)
    local denoRootDir = lspconfig.util.root_pattern("deno.json", "deno.json")(filename);
    if denoRootDir then
      -- print('this seems to be a deno project; returning nil so that tsserver does not attach');
      return nil;
    -- else
      -- print('this seems to be a ts project; return root dir based on package.json')
    end

    return lspconfig.util.root_pattern("package.json")(filename);
  end,
  single_file_support = false,
})
Enter fullscreen mode Exit fullscreen mode

This leverage the fact that if root_dir function returns nil then lspconfig wont's start a language service for that file. 🎉

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more