language-tools: 🐛 BUG: Neovim broken lsp on dynamic routes

Describe the Bug

Currently when using dynamic [anything].astro routes I am having issues in neovim. No issues on other .astro files.

I get minimal lsp support but html, css and most ts not working.

Some but not all diagnostics work.

Formatting is either non existent or sometimes breaks on save. If I delete a line and save, it adds the line back.

vscode has no issues.

Thank you!!

Steps to Reproduce

I have a minimal neovim config at https://github.com/aaronjconway/astro_config

Steps to reproduce

  1. Init basic Astro with typescript
npm create astro@latest

Empty: yes
Typescript: yes - strict

  1. Add prettier and prettier-plugin-astro
npm i -D prettier prettier-plugin-astro

  1. Add the recommend .prettierrc.mjs found here: https://github.com/withastro/prettier-plugin-astro

// .prettierrc.mjs
/** @type {import("prettier").Config} */
export default {
  plugins: ['prettier-plugin-astro'],
  overrides: [
    {
      files: '*.astro',
      options: {
        parser: 'astro',
      },
    },
  ],
}

  1. make a [page].astro file test lsp in new astro file and no lsp

About this issue

  • Original URL
  • State: open
  • Created 4 months ago
  • Reactions: 1
  • Comments: 29 (7 by maintainers)

Most upvoted comments

@aaronjconway I have the same problem, but not really sure if it’s a problem with astro or with neovim. Please post an update if you find the culprit or a solution to this.

Update: Downgrading astro lsp to version 2.6.3 resolved the issue. Still looking for a proper solution to this.

@Princesseuh again, I’ll mention that I really don’t know what I’m doing but I believe I have a solution.

The issue seems to be in Volar. Is the next step bringing this up in volar?

There are some places in volar where we do a .get() on a key where the case was changed. Here you can see the encoding case has changed.

image

in @volar/language-server/lib/uri.js the fileNameToURI function I added a tolowercase in the return and we’re now working in neovim. This was because in project/typescriptProject.js there is a context.documents.get() that was checking on a key that hadn’t been lower cased yet for whatever reason.

function fileNameToUri(fileName) {
    if (fileName.startsWith('/') && fileName.includes('@@')) {
        const parts = fileName.slice(1).split('@@');
        if (parts.length !== 3) {
            throw new Error('Invalid file name');
        }
        return vscode_uri_1.URI.from({
            scheme: parts[0],
            authority: parts[1],
            path: parts[2],
        }).toString();
    }
    return vscode_uri_1.URI.file(fileName).toString().toLowerCase()

Here is where the key is checked. Because somewhere the encoding is lowercased this key is not found and no snapshot is returned. The no snapshot causes all sort of problems such as inability to save updated state, no completion etc.

So either we should just add the tolowercase in the filenametouri, or maybe figuring out why we are changing the case of the encoding. image

Awesome work investigating this! I have brought it up internally in Volar and we’ll attempt to get a fix out. I wonder why it works in VS Code then.

Any update on this @stauersbol?

Not anymore than what I gave, so you’d have to wait for @Princesseuh to have an update for you

Thank you, the logs are helpful. I assume that perhaps VS Code decodes the paths and Neovim doesn’t, not sure who’s right. We’ll investigate!