sveltejs / language-tools

The Svelte Language Server, and official extensions which use it
MIT License
1.24k stars 198 forks source link

LSP Diagnostics error "... is an illegal variable name" on Neovim #2326

Closed pierregoutheraud closed 7 months ago

pierregoutheraud commented 7 months ago

Describe the bug

My code works fine but I am getting a diagnostics error on Neovim using svelte-language-server@0.16.6 and svelte@5.0.0-next.1:

$myStore is an illegal variable name. To reference a global variable called $myStore, use globalThis.$myStore(illegal-global) [svelte/valid-compile]

This only happens when using svelte 5 and in neovim and not vscode. I am getting this error displayed whenever I am using a store inside a .svelte layout or page.

Example:

// stores/test.ts
import { writable } from 'svelte/store';
export const myStore = writable(0);
// +layout.svelte
<script lang="ts">
  import { onMount } from 'svelte';
  import { myStore } from '../stores/test';
  onMount(() => {
    $myStore = 1; <--- ERROR here
  });
</script>

Reproduction

Reproduction minimal repo: https://github.com/pierregoutheraud/test-svelte5

Versions used:

        "@sveltejs/adapter-auto": "^3.0.0",
        "@sveltejs/kit": "^2.0.0",
        "@sveltejs/vite-plugin-svelte": "^3.0.0",
        "@types/eslint": "^8.56.0",
        "@typescript-eslint/eslint-plugin": "^7.0.0",
        "@typescript-eslint/parser": "^7.0.0",
        "eslint": "^8.56.0",
        "eslint-config-prettier": "^9.1.0",
        "eslint-plugin-svelte": "^2.36.0-next.4",
        "prettier": "^3.1.1",
        "prettier-plugin-svelte": "^3.1.2",
        "svelte": "^5.0.0-next.1",
        "svelte-check": "^3.6.0",
        "tslib": "^2.4.1",
        "typescript": "^5.0.0",
        "vite": "^5.0.3",
        "vitest": "^1.2.0"

Expected behaviour

No diagnostics error.

System Info

Which package is the issue about?

svelte-language-server

Additional Information, eg. Screenshots

No response

jasonlyu123 commented 7 months ago

I can't reproduce this. Can you remove your language server installation and its node_modules and try again?

pierregoutheraud commented 7 months ago

@jasonlyu123 I use Mason to install my LSPs, so I already tried re-installing svelte-language-server without success. I am not sure what you mean by "its node_modules", you mean the svelte project node_modules? If yes, I tried that also yes.

I am getting the same error with yarn eslint . so I guess the issue is coming from eslint-plugin-svelte and not the LSP. This is related to https://github.com/sveltejs/eslint-plugin-svelte/issues/652.

jasonlyu123 commented 7 months ago

The language server is a node package with dependencies. What I meant is the node_modules for these dependencies. But when using Svelte 5, the Svelte language server loads the Svelte compiler user installed in their project. So if there is a dependency problem, it is more likely from the node_modules in your project.

I am closing this since it does seem like it's from eslint. I can reproduce the error with lint but not with editor or svelte-check. Feel free to add more info if it turns out to be the svelte language server.