Skip to content

DAP: typing in an expression editor throws a ClassCastException when the JavaScript plugin is installed #1998

Closed

Description

@muescha

Problem

While a DAP session debugs a JavaScript file in an IDE with the JavaScript plugin (IntelliJ IDEA Ultimate, WebStorm, GoLand, …), typing in an expression editor (Evaluate, Watches, a breakpoint condition or a log expression) reports:

java.lang.ClassCastException: class com.redhat.devtools.lsp4ij.dap.evaluation.DAPExpressionCodeFragment
  cannot be cast to class com.intellij.lang.javascript.psi.JSElement
  at com.intellij.lang.javascript.psi.util.JSStubBasedScopeHandler.getScope(JSStubBasedScopeHandler.java:51)
  at com.intellij.lang.javascript.psi.util.JSStubBasedScopeHandler.processDeclarationsInScope(JSStubBasedScopeHandler.java:37)
  ...
  at com.intellij.codeInsight.daemon.impl.IdentifierHighlightingComputer.highlightReferencesAndDeclarations(IdentifierHighlightingComputer.kt:213)

The same exception comes from code completion (JSReferenceCompletionProvider) and JSResolvePrefetch. One IDE log (IntelliJ IDEA 2026.3) held it 115 times over two debug sessions. IntelliJ reports it as an error of the JavaScript plugin, but the cause is in LSP4IJ.

Steps to reproduce

  1. Install IntelliJ IDEA Ultimate (or WebStorm) with the bundled JavaScript plugin, and LSP4IJ built from main.
  2. Debug a JavaScript file with a DAP debug adapter, for example the vscode-js-debug template, and stop at a breakpoint.
  3. In Evaluate or in a watch, type an expression with a string-literal reference, such as o["x"].
  4. The IDE reports the ClassCastException above, on nearly every key stroke.

Cause

DAPDebuggerEditorsProvider.createExpressionCodeFragment() creates the fragment as a DAPExpressionCodeFragment, a PsiFileBase in the language of the debugged file. Its tree is JavaScript PSI under a JSFileElementType root, but the file itself is no JSFile. Claude Code found that the JavaScript plugin takes a file with that root as the scope and casts it to JSElement. Most language plugins assume the same: a file whose root node is of their file element type is their own file class.

Since when

The design is as old as DAP support. It could be hit from 0.19.3 on (#1466), and it is frequent since 6a31cee0 ("make a DAP expression code fragment physical when IntelliJ asks", not released yet): IntelliJ now runs all of its highlighting passes on the fragment. So 0.30.0 would be the first release where users see it often.

It needs a DAP debug adapter for JavaScript in an IDE with the JavaScript plugin. Since the 0.30.0 announcement draft (#1940) mentions vscode-js-debug, I'd fix it before the release.

Other languages

Claude Code checked every language with a parser definition in IntelliJ IDEA Ultimate 2024.2. With the current fragment, references failed to resolve in 20 of 89 languages. Besides the JavaScript family (JavaScript, TypeScript, Flow, …) they include JSP, XHTML, the ignore-file languages and FTL. With the fix below, only 2 still fail, and they fail the same way in a file that IntelliJ creates itself.

Fix

A PR is in progress: create the fragment as the file of its language, the way the language's parser definition creates it (PsiFileFactoryImpl.trySetupPsiForFile), and keep the DAP debug process on the file as user data. It keeps the fragment physical, as 6a31cee0 needs it. A test that resolves references in a JavaScript fragment fails with this ClassCastException on main and passes with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions