sveltejs / language-tools

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

Seemingly unable to exclude node_modules from svelte-check pass #2514

Open webJose opened 1 month ago

webJose commented 1 month ago

Describe the bug

Maybe this is not a bug, and maybe it is just me unable to configure things properly. Since I have struggled with this for a couple of hours already, I'll venture myself and post it as a bug. If you know better, please, by all means correct me.

I want to set skipLibCheck: false in my tsconfig.json file because I am declaring some modules in .d.ts files and I want them checked. Doing this certainly makes VS Code correct my mistakes. Good. However, svelte-check, when run, checks the d.ts files inside node_modules. I have tried many variations for excluding the node_modules folder without success.

  1. skipLibCheck: false, exclude: [ "node_modules" ]
  2. skipLibCheck: false, exclude: [ "node_modules/" ]
  3. skipLibCheck: false, exclude: [ "./node_modules" ]
  4. skipLibCheck: false, exclude: [ "./node_modules/" ]
  5. skipLibCheck: false, exclude: [ "./node_modules/*" ]
  6. skipLibCheck: false, exclude: [ "./node_modules/**" ]
  7. skipLibCheck: false, exclude: [ "node_modules/**" ]
  8. skipLibCheck: false, exclude: [ "node_modules/**/*.d.ts" ]

And the list goes on and on.

Reproduction

  1. npm create vite@latest. Select Svelte, then Typescript.
  2. npm install svelte@next svelte-check@4
  3. Modify tsconfig.json with skipLibCheck: false and your favorite version of exclude.

Let me know how it goes. For me, it complains about node_modules/svelte/types/index.d.ts and node_modules/svelte-check/dist/src/svelte-shims-v4.d.ts.

Expected behaviour

No definition files are checked inside the node_modules folder.

System Info

Which package is the issue about?

svelte-check

Additional Information, eg. Screenshots

No response

jasonlyu123 commented 1 month ago

exclude doesn't prevent files from being checked it only prevents them from being loaded as entry files. All files loaded either as entry files or because of an import will be checked. This is the behaviour of tsc as well so we are unlikely to change this. There is a proposal in the TypeScript repo to add a flag only to disable .d.ts check in node_modules but it was a couple of years ago and doesn't seem to be on track to be implemented.

Can you share what kind of .d.ts file you want to check? If it's just some types you can just use the ts file instead and it'll be checked. That also work if you plan on using JSDoc in case that is why you use .d.ts instead of a regular ts file.

webJose commented 1 month ago

I need to create ambient modules:

declare module "@scope/utility-module" {
    ...
}

These are modules exported by micro-frontends that are then consumed by other micro-frontends. All these are externalized, and then defined in an import map.

jasonlyu123 commented 1 month ago

That should work in a ts file as well. But if the module has an import map why do you have to declare it manually? Is there any error that can't be fixed in the @scope/utility-module module? Or maybe your module resolution isn't bundler or nodenext?

webJose commented 1 month ago

The d.ts file is for Intellisense support. If not included, VS Code will report "could not find module definition or its declaration", or something like that.

jasonlyu123 commented 1 month ago

Oh right, it's import map, not export map. You do need the "declare module" or define it in the "paths" config. But yeah you can also use "define module" in a ts file so you don't disable skipLibCheck.

webJose commented 1 month ago

Ok, thanks. So long story short: The exclude property in tsconfig.json is only for a subset of the TypeScript functionality and it doesn't cover type checking. Svelte tooling has no saying about this (currently or in the future?) and we are at the mercy of Microsoft.

Accurate?

jasonlyu123 commented 1 month ago

It is a common misunderstanding. It's just not what the config is meant for. https://www.typescriptlang.org/tsconfig/#exclude