language-tools: svelte-language-server watcher doesn't work in neovim lspconfig

I’m in the same problem with my sveltekit project. I’m using typescript-svelte-plugin. Although typescript files (like +page.server.ts) works realtime, diagnostics of .svelte file does not updated.

I’m configuring root_pattern( {"package.json", "tsconfig.json", "tsconfig.jsonc", "node_modules"}) for svelte language server

https://user-images.githubusercontent.com/1560508/237040552-0a1be152-0c2b-45ab-b98c-3e927ffeae34.mov

_Originally posted by @ryoppippi in https://github.com/sveltejs/language-tools/issues/1415#issuecomment-1539681042_

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 55 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Actually, you can just manually change the client capability in 0.9.x to have dynamicRegistration = true

local lsp_capabilities = vim.lsp.protocol.make_client_capabilities() --or whatever your setup requires
lsp_capabilities.workspace.didChangeWatchedFiles.dynamicRegistration = true

-- whatever your other plugin requires, for example:
-- lsp_capabilities.textDocument.completion = require('cmp_nvim_lsp').default_capabilities().textDocument.completion

lspconfig.svelte.setup {
    capabilities = lsp_capabilities,
}

If this still doesn’t work, please prepare a reproduction and report to neovim. Also, to reiterate, $/onDidChangeTsOrJsFile is not a substitution for file watcher, They both exist for a reason.

So It seems like neovim intentionally disabled didChangeWatchedFiles.dynamicRegistration in Linux because the watcher capability is limited. Currently, only Mac and Windows have dynamicRegistration=true.

https://github.com/neovim/neovim/blob/f69937fdbd162630c35e119e67bbbf052558c0e0/runtime/doc/news-0.10.txt#L185

Setting it to true does trigger the watcher registration but the file watch itself never triggers. For Linux users with 0.10 stable, You can set lsp_capabilities.workspace.didChangeWatchedFiles to false. This will enable a fallback watcher with chokidar in the language server.

local lsp_capabilities = vim.lsp.protocol.make_client_capabilities() --or whatever your setup requires
lsp_capabilities.workspace.didChangeWatchedFiles = false

-- whatever your other plugin requires, for example:
-- lsp_capabilities.textDocument.completion = require('cmp_nvim_lsp').default_capabilities().textDocument.completion

lspconfig.svelte.setup {
    capabilities = lsp_capabilities,
}

Also, I probably have to repeat a thousand times in this kind of big thread. $/onDidChangeTsOrJsFile is for unsaved changes. And we don’t add new files from $/onDidChangeTsOrJsFile. If you only use it, the new files won’t be loaded.

For people for whom the hack is still not working it might be because you are using ctx.file which when using telescope for example will be a relative path and not an absolute path. You can use ctx.match instead, and it fixed it for me.

on_attach = function(client, bufnr)
  vim.api.nvim_create_autocmd("BufWritePost", {
    pattern = { "*.js", "*.ts" },
    callback = function(ctx)
      -- Here use ctx.match instead of ctx.file
      client.notify("$/onDidChangeTsOrJsFile", { uri = ctx.match })
    end,
  })
end,

Okay, this is a truly hacky things, but I solved this problem with this setting

function on_attach(on_attach)
  vim.api.nvim_create_autocmd("LspAttach", {
    callback = function(args)
      local buffer = args.buf
      local client = vim.lsp.get_client_by_id(args.data.client_id)
      on_attach(client, buffer)
    end,
  })
end

on_attach(function(client, bufnr)
      vim.api.nvim_create_autocmd("BufWritePost", {
        pattern = { "*.js", "*.ts" },
        callback = function(ctx)
          if client.name == "svelte" then
            client.notify("$/onDidChangeTsOrJsFile", { uri = ctx.file })
          end
        end,
      })
    end)
end)

We can update @ryoppippi’s snippet to work without requiring the file to be saved to disk, replicating the behavior in the svelte-vscode extension. It would enable us to do the following:

  1. Assume foo.ts imports MyType from bar.svelte.
  2. Update MyType in foo.ts.
  3. Do not save foo.ts.
  4. bar.svelte is now aware of the latest changes made in step 2.
on_attach(function(client, bufnr)
-     vim.api.nvim_create_autocmd("BufWritePost", {
+     vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI", "TextChangedP" }, {
        pattern = { "*.js", "*.ts" },
        callback = function(ctx)
          if client.name == "svelte" then
            client.notify("$/onDidChangeTsOrJsFile", {
              uri = ctx.file,
+             changes = {
+               { text = table.concat(vim.api.nvim_buf_get_lines(ctx.buf, 0, -1, false), "\n") },
+             },
            })
          end
        end,
+       group = vim.api.nvim_create_augroup("svelte_ondidchangetsorjsfile", { clear = true }),
      })
    end)
end)

Some things I’m not sure of:

  1. In the original svelte-vscode implementation, the changes.range property is defined. However, I’m unsure as to how to define it in nvim. Nevertheless, this still works (maybe at the cost of some performance?) because “if only a text is provided it is considered to be the full content of the document.” (source).

  2. What is the best autocmd event(s) to use to balance performance and convenience. TBH, I’m not sure if we need to worry ourselves with performance at this point. I’m only mentioning this point due to the note in TextChanged’s description.


svelte-language-server: v0.16.1

$ nvim -v
NVIM v0.10.0-dev-7ca2d64
Build type: Release
LuaJIT 2.1.1693350652

This worked for me, nvim 0.10.0 on macOs, also working good for astro + svelte.

local on_attach = function(client, bufnr)
  if client.name == "svelte" then
    vim.api.nvim_create_autocmd("BufWritePost", {
      pattern = { "*.js", "*.ts" },
      group = vim.api.nvim_create_augroup("svelte_ondidchangetsorjsfile", { clear = true }),
      callback = function(ctx)
        -- Here use ctx.match instead of ctx.file
        client.notify("$/onDidChangeTsOrJsFile", { uri = ctx.match })
      end,
    })
  end

  -- attach keymaps if needed
end

Make sure you include:

group = vim.api.nvim_create_augroup("svelte_ondidchangetsorjsfile", { clear = true }),

inside the 2nd param for nvim_create_autocmd function. Otherwise, new autocmd is created on every new buffer svelte lsp get attached to.

Please repoen, this entire extension literally doesn’t work, you install it copying/pasting the exact commands of the readme and nothing.

Thank you @amitgupta6 ! I was not able to really test this in Astro because I cannot easily restart it in the container test setup that they provide.

But it gave me an idea for my LazyVim setup that finally does the trick.

I added a .config/nvim/lua/plugins/lspconfig.lua with

return {
  "neovim/nvim-lspconfig",
  opts = {
    setup = {
      svelte = function(_, opts)
        LazyVim.lsp.on_attach(function(client, buffer)
          if client.name == "svelte" then
            vim.api.nvim_create_autocmd("BufWritePost", {
              pattern = { "*.js", "*.ts" },
              callback = function(ctx)
                client.notify("$/onDidChangeTsOrJsFile", { uri = ctx.match })
              end,
            })
          end
        end)
      end,
    },
  },
 }

Thanks everyone for all the information. I got mine working by adding this into my on_attach() function:

                if client.name == "svelte" then
                    vim.api.nvim_create_autocmd("BufWritePost", {
                        pattern = { "*.js", "*.ts" },
                        group = svelte_augroup,
                        callback = function(ctx)
                            client.notify("$/onDidChangeTsOrJsFile", { uri = ctx.match })
                        end,
                    })
                end

I’m running

NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1702233742
NVIM v0.10.0-dev-735+g8fe9f41f7-Homebrew
Build type: Release
LuaJIT 2.1.0-beta3

and

svelte-language-server 0.15.16

on latest macOS and problem persists.

It is, actually. The file watcher is not triggered for “changes that have not yet been saved to the file system”.

Ah, I see, my bad.

Hey folks,

I’m porting the $/onDidChangeTsOrJsFile notification to lspconfig, so other users will have a better OOTB experience.

At this point, I am ok with PR to add a workaround specific to neovim to enable the fallback chokidar watcher we have. If anyone wanted to contribute, you can find a way to detect if the client is neovim and if evt.capabilities.workspace?.didChangeWatchedFiles.dynamicRegistration is false in here https://github.com/sveltejs/language-tools/blob/ef92ff314e6f647c10c36b04c151da911453e19e/packages/language-server/src/server.ts#L126

neovim 0.12.0-dev, svelte-language-server 0.17.15, this works for me:

local on_attach = function(client, bufnr)        
    if client.name == "svelte" then
        vim.api.nvim_create_autocmd({ "BufWritePre" }, {
            pattern = { "*.js", "*.ts" },
            callback = function(ctx)
                client:notify("$/onDidChangeTsOrJsFile", {
                    uri = ctx.match,
                })
            end
        })
    end
end

lsp.svelte.setup {
    on_attach = on_attach
}

Note the replacement client:notify for client.notify since client.notify is deprecated in nvim 0.12

Hello, I’ve been experiencing similar issues under the following circumstances:

macOS 15.1.1
NVIM v0.10.3
svelte language server 0.17.10

Based on my understanding, the root cause of the problem appears to be Neovim’s watch behavior, not the Svelte language server itself. I found a related issue on Neovim’s GitHub and wanted to share it here: https://github.com/neovim/neovim/pull/30595 It looks like this fix will be included in version 0.11.

For anyone still struggling, I used following configuration in Astrovim and it worked

    config = {
      svelte = {
        capabilities = {
          workspace = {
            didChangeWatchedFiles = {
              dynamicRegistration = true,
            },
          },
        },
      },
    },

@jasonlyu123 i install the last version but not working and fixed using example of @ryoppippi