language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

Typescript Language Server Error on "Move to File" in Svelte Project

Open ian-viri opened this issue 1 year ago • 8 comments

Describe the bug

The new VSCode Move to file refactor throws an error in a Svelte project that has *.svelte component files.

Reproduction

  1. Create a new SvelteKit project
  2. Add a new component file, e.g. src/lib/Component.svelte
  3. Use that component in a +page.svelte file
  4. Open any .ts file, find a function, and attempt to Refactor > Move to File
  5. 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

ian-viri avatar Dec 04 '23 17:12 ian-viri

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.

jasonlyu123 avatar Dec 05 '23 01:12 jasonlyu123

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

ian-viri avatar Dec 05 '23 14:12 ian-viri

Typescript team https://github.com/microsoft/TypeScript

jasonlyu123 avatar Dec 05 '23 15:12 jasonlyu123

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

ian-viri avatar Dec 05 '23 15:12 ian-viri

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.

jasonlyu123 avatar Dec 08 '23 10:12 jasonlyu123

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.

fregante avatar Jan 19 '24 06:01 fregante

@jasonlyu123 did you ever open an issue at the TypeScript repo asking for this assertion to be removed?

dummdidumm avatar Jul 31 '24 12:07 dummdidumm

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.

jasonlyu123 avatar Jul 31 '24 13:07 jasonlyu123