language-tools
language-tools copied to clipboard
Typescript Language Server Error on "Move to File" in Svelte Project
Describe the bug
The new VSCode Move to file refactor throws an error in a Svelte project that has *.svelte component files.
Reproduction
- Create a new SvelteKit project
- Add a new component file, e.g.
src/lib/Component.svelte - Use that component in a
+page.svelte file - Open any
.tsfile, find a function, and attempt toRefactor > Move to File - See the following error from the TS lanugage server
<semantic> TypeScript Server Error (5.2.2) Debug Failure. File /Users/iangillis/code/blog/prisma-sveltekit-seeding/src/lib/Component.svelte has unknown extension. Error: Debug Failure. File /Users/iangillis/code/blog/prisma-sveltekit-seeding/src/lib/Component.svelte has unknown extension. at extensionFromPath (/Users/iangillis/code/blog/prisma-sveltekit-seeding/node_modules/typescript/lib/tsserver.js:21064:39) at /Users/iangillis/code/blog/prisma-sveltekit-seeding/node_modules/typescript/lib/tsserver.js:144823:221 at mapDefined (/Users/iangillis/code/blog/prisma-sveltekit-seeding/node_modules/typescript/lib/tsserver.js:2671:22) at Proxy.getMoveToRefactoringFileSuggestions (/Users/iangillis/code/blog/prisma-sveltekit-seeding/node_modules/typescript/lib/tsserver.js:144823:19) at IpcIOSession.getMoveToRefactoringFileSuggestions (/Users/iangillis/code/blog/prisma-sveltekit-seeding/node_modules/typescript/lib/tsserver.js:184487:41) at getMoveToRefactoringFileSuggesti...
Expected behaviour
Ideally, be able to use the Move to File refactor to move files between any combination of .ts and .svelte files. At the very least, be able to us it to move files between two .ts files without error.
System Info
- OS: Mac
- IDE: VSCode
Which package is the issue about?
Svelte for VS Code extension
Additional Information, eg. Screenshots
Moved from https://github.com/microsoft/vscode/issues/199785
This is a TypeScript debug error with an unknown file extension when auto-import tries to convert the file extension to something runtime supports. For example, write to import something from './index.js' for index.ts. So I am not sure they'll remove this Debug check. It's also pretty deep into the call stack that there is pretty much no way we can patch it to avoid this. So I am afraid this is not something that can be fixed on our side.
Related to #1187
This is also technically not "caused" by the Svelte extension. The root reason is that there are imports for svelte files, and TypeScirpt doesn't know how to rewrite it.
This is also technically not "caused" by the Svelte extension. The root reason is that there are imports for svelte files, and TypeScirpt doesn't know how to rewrite it.
That was my thought as well. Who owns the upstream TypeScript language server? Is it VSCode maintainers or someone else
Typescript team https://github.com/microsoft/TypeScript
I'd like to be able to use this built-in refactor -- what would the fix look like? Some wild guesses:
- We somehow tell VSCode to use the Svelte language server for this refactor in Svelte projects?
- The TS language server natively supports .svelte extensions
- The TS language exposes some way to let the svelte extension plug in and tell is how to handle .svelte files
Sorry, I jumped to the conclusion too fast. I didn't realise it only happens in "move to file" and not "move to new files", so I thought I reproduced with an import that ends with .js.
This is not related to the js ending thing I mentioned. It is a new issue. It just used the same utility function that caused the previous problem. Looking at the typescript source code, we might be able to work around It, but I'll have to see if the workaround is too hacky and might cause other problems.
I had this issue when using a more common .module.css import. In my case it would be easier (or at least it would be a start) to just ignore unrecognized imports. A partial "Move to file" action is better than a complete failure.
@jasonlyu123 did you ever open an issue at the TypeScript repo asking for this assertion to be removed?
Yes. https://github.com/microsoft/TypeScript/issues/56749 It was assigned to a team member so I didn't create a PR to fix it. But it has been over half a year now I'll probably create one.
The refactor source of the feature has also been limited from an arbitrary amount of code to a named block of code. So it probably doesn't make much sense for html-liked files or CSS modules to be a "move to" target anyway. Making TS filter out unknown files should be fine.