language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

bug: (neovim) crash when opening "virtual files"

Open igorlfs opened this issue 5 months ago • 4 comments

Describe the bug

When opening a "virtual file" (i.e., files that do not exist on disk) in neovim, the LSP crashes with the following error:

/usr/lib/node_modules/svelte-language-server/dist/src/plugins/typescript/LSAndTSDocResolver.js:201
            throw new Error('Cannot call getTSService without filePath and without tsconfigPath');
                  ^

Error: Cannot call getTSService without filePath and without tsconfigPath
    at LSAndTSDocResolver.getTSService (/usr/lib/node_modules/svelte-language-server/dist/src/plugins/typescript/LSAndTSDocResolver.js:201:19)
    at LSAndTSDocResolver.getOrCreateSnapshot (/usr/lib/node_modules/svelte-language-server/dist/src/plugins/typescript/LSAndTSDocResolver.js:118:38)
    at EventEmitter.<anonymous> (/usr/lib/node_modules/svelte-language-server/dist/src/plugins/typescript/LSAndTSDocResolver.js:42:22)
    at EventEmitter.emit (node:events:518:28)
    at DocumentManager.notify (/usr/lib/node_modules/svelte-language-server/dist/src/lib/documents/DocumentManager.js:118:22)
    at DocumentManager.openDocument (/usr/lib/node_modules/svelte-language-server/dist/src/lib/documents/DocumentManager.js:43:18)
    at DocumentManager.openClientDocument (/usr/lib/node_modules/svelte-language-server/dist/src/lib/documents/DocumentManager.js:25:21)
    at /usr/lib/node_modules/svelte-language-server/dist/src/server.js:296:37
    at handleNotification (/usr/lib/node_modules/svelte-language-server/node_modules/vscode-jsonrpc/lib/common/connection.js:640:25)
    at handleMessage (/usr/lib/node_modules/svelte-language-server/node_modules/vscode-jsonrpc/lib/common/connection.js:342:13)

Node.js v22.16.0

Reproduction

(0) Configure the server in neovim. E.g., use the default config for svelte from lspconfig.

  1. Launch neovim in a folder matching the configuration's root_markers (e.g., package.json or .git)
  2. Create and open a new "virtual file" with :edit foo://bar.svelte

Expected behaviour

The server shouldn't crash.

System Info

  • OS: Linux
  • IDE: Neovim

Which package is the issue about?

svelte-language-server

Additional Information, eg. Screenshots

N/A

igorlfs avatar Jun 08 '25 17:06 igorlfs

Currently, only file:// protocol is supported. Can you give an example of the use case?

jasonlyu123 avatar Jun 09 '25 00:06 jasonlyu123

Can you give an example of the use case?

It's a very common practice in the neovim ecosystem. For instance, multiple git-related plugins use their own schemes for diffing files. To go a bit more into depth, my current workflow requires me to disable the LSP when reviewing a PR to avoid a plethora of crashes (and then re-enabling once I'm done with the review). Usually these virtual files are related to an actual file in some way, but the mapping may be unclear.

I forgot to mention it, but these crashes started happening after neovim (0.11) introduced a new way to configure LSPs. Previously, neovim would "filter out" these "virtual files", but the new method changed the conditions to include them by default (some servers have no issues with them). It's a recent-ish change, but it's a fairly common use case, I'm surprised it wasn't reported before.

igorlfs avatar Jun 09 '25 01:06 igorlfs

There is a PR https://github.com/sveltejs/language-tools/pull/2611 to add support for non-file schema, but it is inactive. If you're interested, PR is welcome. Otherwise, you might need to find a way to disable virtual files in your Neovim config; lsp doesn't seem to have a way for the server to announce this capability.

jasonlyu123 avatar Jun 09 '25 02:06 jasonlyu123

Otherwise, you might need to find a way to disable virtual files in your Neovim config

Indeed, that's what I ended up doing. It's fairly simple to introduce this restriction on the client side. My goal with this issue was to resolve the problem on the server side, which I assumed to be a lot easier. Since that's not the case, feel free to close the issue if you want to.

igorlfs avatar Jun 10 '25 23:06 igorlfs