Skip to content

typescript-vue-plugin-forward causing: TS Server fatal error: Type assertions should never be parsed outside of TS; they should either be comparisons or JSX. #2434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mjbvz opened this issue Feb 21, 2023 · 10 comments
Labels

Comments

@mjbvz
Copy link

mjbvz commented Feb 21, 2023

Hi, it looks like typescript-vue-plugin-forward may be causing VS Code's Typescript language server to throw the following exception:

Error: Debug Failure. False expression: Type assertions should never be parsed outside of TS; they should either be comparisons or JSX.
    at parseTypeAssertion (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:33949:11)
    at parseSimpleUnaryExpression (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:33582:16)
    at parseUnaryExpressionOrHigher (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:33550:35)
    at parseBinaryExpressionOrHigher (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:33468:25)
    at parseAssignmentExpressionOrHigher (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:33141:18)
    at parseExpression (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:33107:16)
    at doOutsideOfContext (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:30804:12)
    at allowInAnd (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:30825:12)
    at parseExpressionOrLabeledStatement (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:34619:24)
    at parseStatement (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:34851:12)
    at parseListElement (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:31575:12)
    at parseList (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:31560:19)
    at parseSourceFileWorker (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:30620:24)
    at Object.parseSourceFile (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:30453:20)
    at createSourceFile (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:30314:21)
    at createLanguageServiceSourceFile (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:135115:22)
    at acquireOrUpdateDocument (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:132037:26)
    at Object.acquireDocumentWithKey (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:131962:12)
    at getOrCreateSourceFileByPath (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:135440:31)
    at Object.getOrCreateSourceFile (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:135420:14)
    at c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:135352:42
    at Object.getSourceFileWithCache [as getSourceFile] (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:117119:24)
    at findSourceFileWorker (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:119451:23)
    at findSourceFile (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:119371:20)
    at c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:119320:22
    at getSourceFileFromReferenceWorker (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:119289:26)
    at processSourceFile (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:119318:5)
    at processRootFile (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:119114:5)
    at c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:117899:41
    at forEach (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:2309:22)
    at createProgram (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:117899:5)
    at synchronizeHostData (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:135376:15)
    at Object.getProgram (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:135448:5)
    at ConfiguredProject2.updateGraphWorker (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:172060:41)
    at ConfiguredProject2.updateGraph (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:171997:32)
    at ConfiguredProject2.updateGraph (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:173179:24)
    at _ProjectService.createLoadAndUpdateConfiguredProject (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:174786:13)
    at _ProjectService.assignProjectToOpenedScriptInfo (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:175877:26)
    at c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:176157:64
    at flatMap (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:2557:17)
    at _ProjectService.applyChangesInOpenFiles (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:176157:24)
    at updateOpen (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:177302:29)
    at c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:179869:69
    at IpcIOSession.executeWithRequestId (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:179861:14)
    at IpcIOSession.executeCommand (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:179869:29)
    at IpcIOSession.onMessage (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:179911:51)
    at process.<anonymous> (c:\Users\LC\.vscode\extensions\ms-vscode.vscode-typescript-next-5.0.202302200\node_modules\typescript\lib\tsserver.js:181479:14)
    at process.emit (node:events:526:28)
    at emit (node:internal/child_process:938:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:84:21)

We're seeing multiple reports of this, all from users with this plugin enabled. Best I can tell, this plugin comes from this repository but let me know if there's somewhere else this belongs

Please take a look and follow up with any of the effected users in the linked issues below to figure out what may be going on.

This was referenced Feb 21, 2023
@johnsoncodehk
Copy link
Member

typescript-vue-plugin-forward generates .js virtual files containing TS code, which may be the cause of the crash, but I think this should be considered a legitimate error rather than causing a crash, so the problem may belong to TypeScript.

I didn't find a repo case from the related links, but I will try to guess the virtual code that caused the problem from the call stack.

@mjbvz
Copy link
Author

mjbvz commented Feb 22, 2023

@johnsoncodehk I agree crashing seems wrong and please do file an issue against TypeScript for this, but more immediately this plugin appears to be causing this crash by getting TS Server into a bad state. That's causing a lot of user pain and needs to be patched around on the plugin side first

If you're not able to reproduce this, try pinging some of the effected users to see if they can share some steps. Here's all the issue reports we've had so far: https://github.com/microsoft/vscode/issues?q=is%3Aissue+Type+assertions+should+never+be+parsed+outside+of+TS

@jakebailey
Copy link

I added this assert in microsoft/TypeScript#52667; if this is firing, then there's still a bug in the TS parser so I definitely want to know what JS code is causing this.

@jakebailey
Copy link

To be clear, this isn't a user error; the old-style type assertions cannot appear in JSX/TSX at all because we're supposed to parse them as JSX elements.

@jakebailey
Copy link

Hm, I guess this is because the plugin uses a custom script kind / extension and so on. I'll tweak the assert in TS to account for this.

@jakebailey
Copy link

PR is merged, so the next nightly should make this go away; no change is required here.

@johnsoncodehk
Copy link
Member

johnsoncodehk commented Feb 22, 2023

@jakebailey Great. 👍

I found that the problem is not due to virtual .js code, but Vue TS Plugin provides .vue files via getExternalFiles() with Deferred ScriptKind, but tsserver expects ScriptKind to be TS.

Simply debug with:

  function parseTypeAssertion() {
    console.log('!@#$', typeof sourceText, scriptKind);
    console.log(JSON.stringify(sourceText));
    Debug.assert(scriptKind === 3 /* TS */, "Type assertions should never be parsed outside of TS; they should either be comparisons or JSX.");
    // ...
  }
Info 41   [03:20:34.068] !@#$, string, 7
Info 42   [03:20:34.068] "<template>\n  <img alt=\"Vue logo\" src=\"./assets/logo.png\" />\n  <HelloWorld msg=\"Hello Vue 3 + TypeScript + Vite\" />\n  {{ $t /* $t(key: \"hello\"): void */ }}\n</template>\n\n<script lang=\"ts\" setup>\nimport HelloWorld from './components/HelloWorld.vue'\n</script>\n\n<style>\n#app {\n  font-family: Avenir, Helvetica, Arial, sans-serif;\n  -webkit-font-smoothing: antialiased;\n  -moz-osx-font-smoothing: grayscale;\n  text-align: center;\n  color: #2c3e50;\n  margin-top: 60px;\n}\n</style>\n\n<i18n lang=\"json\">\n{\n  \"en\": {\n    \"hello\": \"hello world!\"\n  },\n  \"ja\": {\n    \"hello\": \"こんにちは、世界!\"\n  }\n}\n</i18n>\n"

As you said at #2434 (comment).

@jakebailey
Copy link

Hm, given that's not any language that TypeScript currently supports, I do wonder if there's a bunch of time being wasted parsing that whole thing...

@johnsoncodehk
Copy link
Member

This is possible considering that tsserver is actually trying to parse .vue files. 😅

It is worth noting that this is only the case for TS Plugin, for Vue Language Server we only parse .vue to the .vue.ts and provide to TS LanguageServiceHost, the mapping of .vue.ts to .vue is done outside of TS.

For Vue users who are concerned about performance, they are using takeover mode and avoiding the duplication cost of TS Plugin / tsserver, so I think not a big problem at the moment.

@jakebailey
Copy link

Well, in any case, thanks for fuzz testing our parser 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants