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: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 49 (14 by maintainers)

Most upvoted comments

@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

@Princesseuh @johnsoncodehk

Seems to be fully resolved for me!

Thanks guys.

@johnsoncodehk it is case sensitive but it seems like we’re only lowercasing the encoding. Everywhere I can find the uri’s keep the filename case but lower the encoding case.

I can’t find where we are lowering the encoding case.

in vscode-uri I just manually changed the encoding to the lower version and have full functionality.

@aaronjconway Is your file system case sensitive? You can add the following log to the code to check this.

getScriptSnapshot(fileName) {
+	console.warn(ts.sys.useCaseSensitiveFileNames);
	askedFiles.pathSet(fileName, true);
	const doc = server.documents.get(fileNameToUri(fileName));
	if (doc) {
		return doc.getSnapshot();
	}
},

I know this was not directed at me, but all Linux distributions are case sensitive. Even if it’s in WSL it still runs Linux.

@stauersbol Can you reproduce the issue with minimal operations and share the LSP logs?

Hello @johnsoncodehk, I think it has to do with the conversion or normalization of case based on what system is detected. This is why it works in VS code but not other systems, at least mine, neovim on wsl.

in URI.file from vscode-uri the encoding is upper case while in our uriMap we lowercase.

in typescriptProject.js we fail to get the uri from the weakmap which is what I believe causes the issue.

 getScriptSnapshot(fileName) {
            askedFiles.pathSet(fileName, true);
            //uri_1.fileNameToUri(fileName) outputs an upper cased encoding 
            //while server.documents will all be lowercased at some point. 
            //Only the encoding is lower cased. 
            const doc = server.documents.get((0, uri_1.fileNameToUri)(fileName));
            if (doc) {
                return doc.getSnapshot();
            }
        },

This is within the getScriptSnapshot() image

https://gist.github.com/stauersbol/6db13542237c08563107b179f1f17497

Had to paste it to a gist as the logs hit the character support that github supports for comments. There was some more above this, but it was primarily start logs for tailwind and the astro lsp. This above is a simple operation of adding a p element and saving right after.

Sorry for the constant spam on this issue, but this looks like a state problem. The duplication stops as soon as I turned off the formatter. So it looks like the file state that is stored never gets updated, or whenever it sends the updated file state it refuses to update it internally(?).

Sadly I have no idea how volar works under the hood with this other than from the brief time I looked at it.

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!