You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(dap): create an expression code fragment as the file of its language, as its parser definition does - #2001
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 logpoint) 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.lang.javascript.psi.util.JSStubBasedPsiTreeUtil.processDeclarationsInScope(JSStubBasedPsiTreeUtil.java:232)
at com.intellij.lang.javascript.psi.impl.JSTextReferenceResolver.doProcessLocalVariants(JSTextReferenceResolver.java:48)
...
at com.intellij.lang.javascript.psi.impl.JSReferenceSetElement.resolve(JSReferenceSetElement.java:54)
at com.intellij.codeInsight.TargetElementUtilBase.getReferencedElement(TargetElementUtilBase.java:180)
...
at com.intellij.codeInsight.daemon.impl.IdentifierHighlightingComputer.highlightReferencesAndDeclarations(IdentifierHighlightingComputer.kt:213)
IntelliJ blames the JavaScript plugin, but the cause is in LSP4IJ. The same exception comes from identifier highlighting, code completion (JSReferenceCompletionProvider) and JSResolvePrefetch. One IDE log (IntelliJ IDEA 2026.3, IU-263.7307.200) holds it 115 times over two sessions.
With this change, the fragment is created as the file of its language, the way the language's parser definition creates it: a JSFile for JavaScript.
Why the fragment is no JSFile
DAPDebuggerEditorsProvider.createExpressionCodeFragment() gives the fragment the file type and language of the file being debugged (a dialect such as ECMAScript 6 is degraded to the file type's language, JavaScript). It then created new DAPExpressionCodeFragment(...), a PsiFileBase in that language.
A PsiFileBase takes only the parser from the language: its tree is JavaScript PSI under a root node of the type JSFileElementType. The file object itself is LSP4IJ's own class. It implements neither JSFile nor JSElement. The language's own file class (JSFileImpl) is only created by JSParserDefinition.createFile(), which LSP4IJ never called.
The JavaScript plugin relies on the file class matching the root node. Claude Code decompiled the JavaScript plugin of 2026.3:
JSStubBasedScopeHandler.getScope() looks for the enclosing scope with JSStubBasedPsiTreeUtil.getContextOfType(context, SCOPE_TYPES, false) and casts the result to JSElement.
JSTreeUtil.ScopeTypesHolder.SCOPE_TYPES includes every JSFileElementType.
getContextOfType() returns a PsiFileImpl as the scope when types.contains(file.getFileElementType()).
For a reference at file level, the scope is the fragment, and the cast fails. The same code (and the same cast) is in the JavaScript plugin of 2024.2. An instanceof in the JavaScript plugin would avoid the exception, but the assumption itself is a reasonable one that most language plugins make: a file whose root node is of their file element type is their file class.
Debugging JavaScript in GoLand: PsiFileBase asserts "Language: ECMAScript 6 doesn't participate in view provider". The fragment for a JavaScript file could not be created at all, which hid the cast.
A dialect is degraded to the file type's language. The JavaScript fragment is now created, and its PSI is reachable for the JavaScript plugin: the cast becomes possible.
2026-10-22, not released yet
6a31cee0 "fix: make a DAP expression code fragment physical when IntelliJ asks"
The fragment of an expression editor is physical, as IntelliJ asks. IntelliJ now runs all of its highlighting passes and inspections on it, and the exception comes on nearly every key stroke.
So the bug has been in the code since DAP support started. It could be hit from 0.19.3 on, and it is frequent since 6a31cee. Few users run into it: it needs a DAP debug adapter for JavaScript (for example the vscode-js-debug template) in an IDE that has the JavaScript plugin. IntelliJ IDEA Community has no JavaScript plugin, and in IDEA Ultimate or WebStorm most people use the built-in JavaScript debugger.
Other languages
The same design breaks other languages, too. Claude Code ran a temporary probe in the test sandbox (IntelliJ IDEA Ultimate 2024.2 with its bundled plugins). For every registered language with a parser definition, it created a fragment from several expressions and resolved all references in it.
With new DAPExpressionCodeFragment(...) (before this change), 20 of 89 languages failed:
EL, SpEL, MicronautEL (ELResolveUtil.processEmptyContext), PointcutExpression (AopPointcutUtil.getHolder), Markdown (flavour not set)
Not related to the fragment (also fails for a file that IntelliJ creates itself)
Declarative (Android classes missing in the sandbox), SQLDateTime (injection-only, its lexer throws)
With this change, only the last two still fail. Both fail the same way with IntelliJ's own PsiFileFactory.createFileFromText(), so they are not caused by the fragment.
The probe covers reference resolution only. Completion, annotators and inspections on the old fragment may fail in more languages.
If the language has a parser definition, it creates a LightVirtualFile with the file type and language and sets up its PSI with PsiFileFactoryImpl.trySetupPsiForFile(file, language, physical, false). That is what PsiFileFactory.createFileFromText() does: the language's own file view provider (a template language such as JSP or FreeMarker has one) creates the file of the language through its parser definition.
The DAP debug process is put on the file as user data (Key), not into a field of a subclass.
Only for a language without a parser definition does it still create a DAPExpressionCodeFragment.
physical is passed to the view provider as before ("event system enabled"), so a physical fragment stays physical: the fix of 6a31cee is kept, and the existing tests for it pass.
DAPCompletionProcessor found the expression editor by instanceof DAPExpressionCodeFragment and got the current stack frame from it. It now calls DAPExpressionCodeFragment.getCurrentDapStackFrame(psiFile), which reads the user data and so works for any file class.
Other solutions considered
Always plain text. Creating every fragment as a plain text file would avoid all language plugin code, so no language could fail in it. But the expression editor would lose everything the language gives it: syntax highlighting, brace matching, commenting, the language's own completion. DAP completion would still work (DAPCompletionProcessor is registered for language="any"). That is a big loss for a rare bug, and it would undo what redhat-developer/lsp4ij#1466 set out to keep.
Plain text file type with the language's syntax highlighter only. Giving the fragment the plain text language but the debugged file type would keep the colors, but the file view provider takes its base language from the file type, so file type and language would disagree, the same kind of mismatch that #1118 and #1465 hit.
An own language for the fragment. A DAP expression language that delegates its lexer and highlighter to the debugged language would keep the colors and keep language plugins out. It would need its own parser definition, file type and highlighter factory, and still lose brace matching, commenting and the language's completion. Much more code for less function.
Fix it in the JavaScript plugin. An instanceof JSElement in JSStubBasedScopeHandler.getScope() would avoid this one cast, but not the casts in JSP, the language mismatches, or the next language plugin that relies on its own file class.
Possible hardening on top (not in this PR):
If the language's file cannot be created (trySetupPsiForFile() returns null or throws), fall back to a plain text fragment instead of a DAPExpressionCodeFragment in the foreign language. That would catch errors when the file is created. Parsing is lazy, so it would not catch a lexer that throws later (SQLDateTime).
If the language's inspections turn out to be noisy in a fragment (for example "unresolved variable" for a variable of the debuggee), a ProblemHighlightFilter could switch off problem highlighting for files with the DAP user data. This has not been seen yet.
Tests
DAPDebuggerEditorsProviderTest (runs with IntelliJ IDEA Community, as on CI):
new testWithALanguageWhoseFileIsCreatedByAnotherLanguage: an XHTML fragment is created, is an XmlFile and is physical. Before this change it fails with AssertionError: Language: XML != Language: XHTML.
testWithDialectLanguage now also asserts that the JSON fragment is a JsonFile, the file of its language. Before this change it is a DAPExpressionCodeFragment.
New DAPExpressionCodeFragmentJavaScriptTest: creates fragments of a .js file from several expressions and resolves all references in them. Before this change, o["x"] fails with the ClassCastException above, with the same stack (JSLiteralTextReference → JSTextReferenceResolver → JSStubBasedScopeHandler.getScope). It needs the JavaScript plugin and returns at once without it. A plain identifier such as x + y resolves another way and does not fail. See the open point below: as the build is now, this test runs nowhere.
Open point: the JavaScript test runs nowhere
Since redhat-developer/lsp4ij#1931 (4ffed454), prepareTestSandbox in build.gradle.kts disables the bundled JavaScript and com.intellij.css plugins, so that they do not take the .js, .ts and .css files of the other tests away from LSP4IJ. That is right for those tests, but it also means DAPExpressionCodeFragmentJavaScriptTest returns at once everywhere, including the CI job on IntelliJ IDEA 2026.3 (IU). The JavaScript plugin depends on the CSS plugin, so both have to be enabled for the test to run.
To run it locally, as done for this PR:
In gradle.properties: platformType=IU and platformBundledPlugins=…,JavaScript,JavaScriptDebugger.
In build.gradle.kts: comment out disabledPlugins.addAll("JavaScript", "com.intellij.css") in prepareTestSandbox.
./gradlew test --tests 'com.redhat.devtools.lsp4ij.dap.*'
A possible way to make it run on CI (not in this PR, happy to add it here or in a follow-up if you like it):
A Gradle property, for example -PtestWithJavaScriptPlugin, that leaves the two plugins enabled in prepareTestSandbox.
One more step in the IU job of ci.yml that runs only the tests which need the JavaScript plugin with that property, for example ./gradlew test --tests '*JavaScriptTest' -PplatformType=IU -PtestWithJavaScriptPlugin. The other tests keep running with both plugins disabled.
Removing the line from #1931 is no option: the other tests would lose their .js, .ts and .css files to the IDE's plugins again.
How this was checked
On upstream main21a3f42a, with IntelliJ IDEA 2024.2 (the platformVersion of gradle.properties):
With platformType=IC (as on CI): all 328 tests in com.redhat.devtools.lsp4ij.dap.* pass. DAPDebuggerEditorsProviderTest fails without this change and passes with it; DAPExpressionCodeFragmentJavaScriptTest returns at once (no JavaScript plugin).
With platformType=IU, the bundled JavaScript plugin, and JavaScript and com.intellij.css enabled in the test sandbox (see the open point above): DAPExpressionCodeFragmentJavaScriptTest fails without this change with the ClassCastException (the fragment is a DAPExpressionCodeFragment) and passes with it (the fragment is a JSFileImpl). All 328 tests in com.redhat.devtools.lsp4ij.dap.* pass.
The probe over all languages, described above (not part of this PR).
IDE re-test
IntelliJ IDEA 2026.3 (IU-263.7307.200) with its bundled JavaScript plugin, and a DAP debug adapter for JavaScriptCore, run as a DAP run configuration. The debuggee stops at a debugger; statement in a .js file. Then, in the same steps before and after this change, with idea.log emptied first:
Evaluate: type item["count"], evaluate, delete it; the same with item.count.
Watches: add item["count"], then edit it (type and delete).
A breakpoint condition: item["count"] > 0.
Before: 0.30.0-SNAPSHOT from main, without this change
idea.log has the ClassCastException 59 times (message and stack lines). That is 17 SEVERE reports, all with "Plugin to blame: JavaScript and TypeScript":
Reported by
SEVERE reports
InspectionRunner
10
Unhandled exception in the kernel (ExceptionsKt)
4
CompletionProgressIndicator
1
JSResolvePrefetch
1
Alarm
1
18 of the stacks also go through identifier highlighting (IdentifierHighlightingComputer). Each stack is the one above: JSLiteralTextReference → JSTextReferenceResolver → JSStubBasedScopeHandler.getScope().
After: this branch
The plugin built from this branch, installed in the same IDE, and the IDE restarted. The same steps.
Count in idea.log
ClassCastException
0
DAPExpressionCodeFragment
0
SEVERE reports
0
WARN entries
3, none related: 2 from PerformanceWatcherImpl ("responsive for long enough"), 1 from Alarm ("Do not create alarm without coroutineScope" in IntelliJ's DetailController)
DAPExpressionCodeFragment extends PsiFileBase: it took only the parser of
the debugged file's language, so its tree was that language's PSI under a
root node of the language's file element type, but the file was LSP4IJ's
own class, not the file class of the language. Language plugins rely on
that class. While debugging JavaScript with the JavaScript plugin
installed, JSStubBasedScopeHandler.getScope() found the fragment as the
scope by its JSFileElementType and cast it to JSElement, so typing in an
expression editor reported a ClassCastException from highlighting,
completion and resolve. In other languages the fragment could not be
created at all (XHTML, the ignore languages, FreeMarker), or was cast to
the language's file class (JSP).
DAPExpressionCodeFragment.create() now sets up the PSI of the language
for the fragment as PsiFileFactory.createFileFromText() does, with the
language's own view provider, which creates the language's file through
its parser definition. A physical fragment stays physical. The debug
process is kept as user data on the file, and DAPCompletionProcessor
reads it from there instead of by instanceof. A DAPExpressionCodeFragment
is created only for a language without a parser definition.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
No actionable comments were generated in the recent review.
Summary
Summary by CodeRabbit
Bug Fixes
Debugger expression fragments now better preserve the source language and file type, improving expression handling for languages such as JavaScript and XHTML.
Completion continues to use the active debug stack frame when available, including for parser-created expression files.
DAP expression fragments now use language-specific PSI files when parser definitions are available. The debug process is stored as PSI user data, and completion obtains the current stack frame through a static accessor.
A factory creates a language-specific PSI file when possible and falls back to DAPExpressionCodeFragment. Both creation paths store the debug process as user data.
Use the factory and retrieve the stack frame src/main/java/com/redhat/devtools/lsp4ij/dap/DAPDebuggerEditorsProvider.java, src/main/java/com/redhat/devtools/lsp4ij/dap/completion/DAPCompletionProcessor.java, src/test/java/com/redhat/devtools/lsp4ij/dap/DAPDebuggerEditorsProviderTest.java, src/test/java/com/redhat/devtools/lsp4ij/dap/DAPExpressionCodeFragmentJavaScriptTest.java
The editor provider uses the factory, and completion reads the stack frame from the original PSI file. Tests cover XHTML PSI creation and reference resolution in JavaScript expression fragments.
Expression fragments in DAP debugging now use the language's own PSI file, which should remove the reported ClassCastException. No merge-blocking risk was identified.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title states the main change: DAP expression code fragments are created as the file of their language, matching the language's parser definition. The raw summary confirms this: create() uses the p…
Linked Issues check
Objective from #1998: a JavaScript DAP expression fragment must not be a DAPExpressionCodeFragment, so that the JavaScript plugin's JSElement cast does not fail. At reviewed head 17d8bdc, DAPExpressi…
Out of Scope Changes check
The changed files are DAPDebuggerEditorsProvider (factory call), DAPCompletionProcessor (uses the static stack-frame accessor instead of instanceof), DAPExpressionCodeFragment (new factory, user-data …
Description check
The description is directly related to the changeset. It names the ClassCastException in JavaScript DAP expression fragments, explains the cause (DAPExpressionCodeFragment is a PsiFileBase, not the la…
Full details: Title check
Explanation
The title states the main change: DAP expression code fragments are created as the file of their language, matching the language's parser definition. The raw summary confirms this: create() uses the parser definition, and the fragment falls back to DAPExpressionCodeFragment only without one. The title is specific and accurate, though long (103 characters).
Full details: Linked Issues check
Explanation
Objective from #1998: a JavaScript DAP expression fragment must not be a DAPExpressionCodeFragment, so that the JavaScript plugin's JSElement cast does not fail. At reviewed head 17d8bdc, DAPExpressionCodeFragment.create(...) calls PsiFileFactoryImpl.trySetupPsiForFile(file, language, physical, false) when the language has a parser definition, so the fragment is the language's own file class (JSFile for JavaScript). The DAP debug process is stored as file user data (DEBUG_PROCESS), and getCurrentDapStackFrame(PsiFile) reads it for any file class. Physical behavior is preserved by passing physical to the view provider. The fallback to DAPExpressionCodeFragment applies only when no parser definition exists. DAPDebuggerEditorsProvider uses the factory, and DAPCompletionProcessor uses the static accessor. Automated test: DAPExpressionCodeFragmentJavaScriptTest resolves references in JavaScript fragments and checks the expected behavior. The test returns at once when the JavaScript plugin is absent, which is the default CI sandbox, so it verifies the fix only when that plugin is enabled. The author reports a local run with the plugin enabled: it fails on main and passes with the change. DAPDebuggerEditorsProviderTest also covers the file-class behavior (XHTML, JSON). #1465 is closed and serves as context only.
Full details: Out of Scope Changes check
Explanation
The changed files are DAPDebuggerEditorsProvider (factory call), DAPCompletionProcessor (uses the static stack-frame accessor instead of instanceof), DAPExpressionCodeFragment (new factory, user-data accessor, constructor kept for languages without a parser definition), and the DAP tests. Each change supports the #1998 objective: the factory creates the language file, and the completion processor must read the debug process from a file of any class. No change was found that is unrelated to DAP expression fragments.
Full details: Description check
Explanation
The description is directly related to the changeset. It names the ClassCastException in JavaScript DAP expression fragments, explains the cause (DAPExpressionCodeFragment is a PsiFileBase, not the language's file class), and describes the fix: creating the fragment via the language's parser definition, storing the debug process as user data, and updating DAPCompletionProcessor. It also covers tests and verification.
Recent review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Organization UI
Review profile: CHILL
Plan: Advanced
Run ID: e4ff243a-a67c-427d-9a33-b74dbcb40edf
Commits
Reviewing files that changed from the base of the PR and between 21a3f42 and 17d8bdc.
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
❌ Patch coverage is 61.11111% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 49.10%. Comparing base (21a3f42) to head (17d8bdc).
@muescha Thanks! Since CI can't show the JavaScript case, I checked this one locally before merging it for 0.30.0.
DAPExpressionCodeFragmentJavaScriptTest returns at its first line in every run. CI's default legs run IntelliJ IDEA Community, which has no JavaScript plugin, and on Ultimate and the unified IntelliJ IDEA, build.gradle.kts disables the JavaScript plugin in the test sandbox (prepareTestSandbox, since #1931). So its comment, that it runs with platformType=IU and the bundled JavaScript plugin, doesn't hold. #2007 is about a test task in which it can run.
I ran it, with a broader check, on IntelliJ IDEA Ultimate 2024.2 with the bundled JavaScript plugin and that sandbox line commented out, for 15 Evaluate-style expressions (x + y, o["x"], "x" in o, o.hasOwnProperty("x"), Reflect.get(o, "x"), document.getElementById("x"), require("x"), undefinedName.foo, …):
With this PR, nothing throws, and each fragment is a JSFileImpl. DAPExpressionCodeFragmentJavaScriptTest passes.
With all 140 default-on JavaScript inspections enabled, no fragment gets a warning or an error, before or after. The same inspections report unresolved variables and unused code in an ordinary .js file, so IntelliJ simply doesn't inspect these fragments. The change adds no marks in Evaluate, Watches or breakpoint conditions.
All DAP tests pass on Ultimate with this PR.
DAP completion still finds its stack frame: DAPCompletionProcessor reads the debug process from parameters.getOriginalFile(), which keeps the user data, not from completion's copy of the file.
Not covered: Python with debugpy, since Ultimate doesn't bundle the Python plugin, and a live debug session.
… their own (#2008)
The tests' sandbox disables the JavaScript and CSS plugins (#1931), and CI's default legs run IntelliJ
IDEA Community, which has neither. So DAPExpressionCodeFragmentJavaScriptTest (#2001) returned at its
first line in every run and passed without testing anything.
testJavaScript, registered with intellijPlatformTesting.testIde, runs the tests named
*JavaScriptPluginTest in a sandbox of its own, which keeps the JavaScript plugin; test leaves them out.
The IntelliJ Platform Gradle Plugin makes check run it. On IntelliJ IDEA Ultimate (-PplatformType=IU),
which bundles the plugin, it runs, as on CI's 2026.3 leg; elsewhere it is skipped. The test is renamed
to DAPExpressionCodeFragmentJavaScriptPluginTest, and fails instead of returning when the plugin isn't
loaded.
Validated:
- testJavaScript on IntelliJ IDEA Ultimate 2024.2 and 2026.3: 1 test, 0 failures.
- With the production change of #2001 reverted, it fails with the ClassCastException of #1998.
- On Community (the default), testJavaScript is skipped, and test doesn't run the test.
- The configuration cache is stored and reused.
Fixes#2007
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1998
Summary
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 logpoint) reports:
IntelliJ blames the JavaScript plugin, but the cause is in LSP4IJ. The same exception comes from identifier highlighting, code completion (
JSReferenceCompletionProvider) andJSResolvePrefetch. One IDE log (IntelliJ IDEA 2026.3, IU-263.7307.200) holds it 115 times over two sessions.With this change, the fragment is created as the file of its language, the way the language's parser definition creates it: a
JSFilefor JavaScript.Why the fragment is no
JSFileDAPDebuggerEditorsProvider.createExpressionCodeFragment()gives the fragment the file type and language of the file being debugged (a dialect such as ECMAScript 6 is degraded to the file type's language, JavaScript). It then creatednew DAPExpressionCodeFragment(...), aPsiFileBasein that language.A
PsiFileBasetakes only the parser from the language: its tree is JavaScript PSI under a root node of the typeJSFileElementType. The file object itself is LSP4IJ's own class. It implements neitherJSFilenorJSElement. The language's own file class (JSFileImpl) is only created byJSParserDefinition.createFile(), which LSP4IJ never called.The JavaScript plugin relies on the file class matching the root node. Claude Code decompiled the JavaScript plugin of 2026.3:
JSStubBasedScopeHandler.getScope()looks for the enclosing scope withJSStubBasedPsiTreeUtil.getContextOfType(context, SCOPE_TYPES, false)and casts the result toJSElement.JSTreeUtil.ScopeTypesHolder.SCOPE_TYPESincludes everyJSFileElementType.getContextOfType()returns aPsiFileImplas the scope whentypes.contains(file.getFileElementType()).For a reference at file level, the scope is the fragment, and the cast fails. The same code (and the same cast) is in the JavaScript plugin of 2024.2. An
instanceofin the JavaScript plugin would avoid the exception, but the assumption itself is a reasonable one that most language plugins make: a file whose root node is of their file element type is their file class.History
DAPExpressionCodeFragment extends PsiFileBase, created with the language of the debugged file. The cause of this bug is in this design.PsiFileBaseasserts "Language: ECMAScript 6 doesn't participate in view provider". The fragment for a JavaScript file could not be created at all, which hid the cast.So the bug has been in the code since DAP support started. It could be hit from 0.19.3 on, and it is frequent since 6a31cee. Few users run into it: it needs a DAP debug adapter for JavaScript (for example the
vscode-js-debugtemplate) in an IDE that has the JavaScript plugin. IntelliJ IDEA Community has no JavaScript plugin, and in IDEA Ultimate or WebStorm most people use the built-in JavaScript debugger.Other languages
The same design breaks other languages, too. Claude Code ran a temporary probe in the test sandbox (IntelliJ IDEA Ultimate 2024.2 with its bundled plugins). For every registered language with a parser definition, it created a fragment from several expressions and resolved all references in it.
With
new DAPExpressionCodeFragment(...)(before this change), 20 of 89 languages failed:JSElement); JSP (BaseJspFile)XML != XHTML), GitIgnore, HgIgnore, DockerIgnore, GitExclude (IgnoreLang != …), FTL (FTL> != FTL)ELResolveUtil.processEmptyContext), PointcutExpression (AopPointcutUtil.getHolder), Markdown (flavour not set)With this change, only the last two still fail. Both fail the same way with IntelliJ's own
PsiFileFactory.createFileFromText(), so they are not caused by the fragment.The probe covers reference resolution only. Completion, annotators and inspections on the old fragment may fail in more languages.
Change
DAPExpressionCodeFragment.create(text, fileType, language, debugProcess, project, physical):LightVirtualFilewith the file type and language and sets up its PSI withPsiFileFactoryImpl.trySetupPsiForFile(file, language, physical, false). That is whatPsiFileFactory.createFileFromText()does: the language's own file view provider (a template language such as JSP or FreeMarker has one) creates the file of the language through its parser definition.Key), not into a field of a subclass.DAPExpressionCodeFragment.physicalis passed to the view provider as before ("event system enabled"), so a physical fragment stays physical: the fix of 6a31cee is kept, and the existing tests for it pass.DAPDebuggerEditorsProvider.createExpressionCodeFragment()callsDAPExpressionCodeFragment.create(...).DAPCompletionProcessorfound the expression editor byinstanceof DAPExpressionCodeFragmentand got the current stack frame from it. It now callsDAPExpressionCodeFragment.getCurrentDapStackFrame(psiFile), which reads the user data and so works for any file class.Other solutions considered
Always plain text. Creating every fragment as a plain text file would avoid all language plugin code, so no language could fail in it. But the expression editor would lose everything the language gives it: syntax highlighting, brace matching, commenting, the language's own completion. DAP completion would still work (
DAPCompletionProcessoris registered forlanguage="any"). That is a big loss for a rare bug, and it would undo what redhat-developer/lsp4ij#1466 set out to keep.Plain text file type with the language's syntax highlighter only. Giving the fragment the plain text language but the debugged file type would keep the colors, but the file view provider takes its base language from the file type, so file type and language would disagree, the same kind of mismatch that #1118 and #1465 hit.
An own language for the fragment. A DAP expression language that delegates its lexer and highlighter to the debugged language would keep the colors and keep language plugins out. It would need its own parser definition, file type and highlighter factory, and still lose brace matching, commenting and the language's completion. Much more code for less function.
Fix it in the JavaScript plugin. An
instanceof JSElementinJSStubBasedScopeHandler.getScope()would avoid this one cast, but not the casts in JSP, the language mismatches, or the next language plugin that relies on its own file class.Possible hardening on top (not in this PR):
trySetupPsiForFile()returns null or throws), fall back to a plain text fragment instead of aDAPExpressionCodeFragmentin the foreign language. That would catch errors when the file is created. Parsing is lazy, so it would not catch a lexer that throws later (SQLDateTime).ProblemHighlightFiltercould switch off problem highlighting for files with the DAP user data. This has not been seen yet.Tests
DAPDebuggerEditorsProviderTest(runs with IntelliJ IDEA Community, as on CI):testWithALanguageWhoseFileIsCreatedByAnotherLanguage: an XHTML fragment is created, is anXmlFileand is physical. Before this change it fails withAssertionError: Language: XML != Language: XHTML.testWithDialectLanguagenow also asserts that the JSON fragment is aJsonFile, the file of its language. Before this change it is aDAPExpressionCodeFragment.DAPExpressionCodeFragmentJavaScriptTest: creates fragments of a.jsfile from several expressions and resolves all references in them. Before this change,o["x"]fails with theClassCastExceptionabove, with the same stack (JSLiteralTextReference→JSTextReferenceResolver→JSStubBasedScopeHandler.getScope). It needs the JavaScript plugin and returns at once without it. A plain identifier such asx + yresolves another way and does not fail. See the open point below: as the build is now, this test runs nowhere.Open point: the JavaScript test runs nowhere
Since redhat-developer/lsp4ij#1931 (4ffed454),
prepareTestSandboxinbuild.gradle.ktsdisables the bundledJavaScriptandcom.intellij.cssplugins, so that they do not take the.js,.tsand.cssfiles of the other tests away from LSP4IJ. That is right for those tests, but it also meansDAPExpressionCodeFragmentJavaScriptTestreturns at once everywhere, including the CI job on IntelliJ IDEA 2026.3 (IU). The JavaScript plugin depends on the CSS plugin, so both have to be enabled for the test to run.To run it locally, as done for this PR:
gradle.properties:platformType=IUandplatformBundledPlugins=…,JavaScript,JavaScriptDebugger.build.gradle.kts: comment outdisabledPlugins.addAll("JavaScript", "com.intellij.css")inprepareTestSandbox../gradlew test --tests 'com.redhat.devtools.lsp4ij.dap.*'A possible way to make it run on CI (not in this PR, happy to add it here or in a follow-up if you like it):
-PtestWithJavaScriptPlugin, that leaves the two plugins enabled inprepareTestSandbox.ci.ymlthat runs only the tests which need the JavaScript plugin with that property, for example./gradlew test --tests '*JavaScriptTest' -PplatformType=IU -PtestWithJavaScriptPlugin. The other tests keep running with both plugins disabled.Removing the line from #1931 is no option: the other tests would lose their
.js,.tsand.cssfiles to the IDE's plugins again.How this was checked
On upstream
main21a3f42a, with IntelliJ IDEA 2024.2 (theplatformVersionofgradle.properties):platformType=IC(as on CI): all 328 tests incom.redhat.devtools.lsp4ij.dap.*pass.DAPDebuggerEditorsProviderTestfails without this change and passes with it;DAPExpressionCodeFragmentJavaScriptTestreturns at once (no JavaScript plugin).platformType=IU, the bundled JavaScript plugin, andJavaScriptandcom.intellij.cssenabled in the test sandbox (see the open point above):DAPExpressionCodeFragmentJavaScriptTestfails without this change with theClassCastException(the fragment is aDAPExpressionCodeFragment) and passes with it (the fragment is aJSFileImpl). All 328 tests incom.redhat.devtools.lsp4ij.dap.*pass.IDE re-test
IntelliJ IDEA 2026.3 (IU-263.7307.200) with its bundled JavaScript plugin, and a DAP debug adapter for JavaScriptCore, run as a DAP run configuration. The debuggee stops at a
debugger;statement in a.jsfile. Then, in the same steps before and after this change, withidea.logemptied first:item["count"], evaluate, delete it; the same withitem.count.item["count"], then edit it (type and delete).item["count"] > 0.Before: 0.30.0-SNAPSHOT from
main, without this changeidea.loghas theClassCastException59 times (message and stack lines). That is 17SEVEREreports, all with "Plugin to blame: JavaScript and TypeScript":SEVEREreportsInspectionRunnerExceptionsKt)CompletionProgressIndicatorJSResolvePrefetchAlarm18 of the stacks also go through identifier highlighting (
IdentifierHighlightingComputer). Each stack is the one above:JSLiteralTextReference→JSTextReferenceResolver→JSStubBasedScopeHandler.getScope().After: this branch
The plugin built from this branch, installed in the same IDE, and the IDE restarted. The same steps.
idea.logClassCastExceptionDAPExpressionCodeFragmentSEVEREreportsWARNentriesPerformanceWatcherImpl("responsive for long enough"), 1 fromAlarm("Do not create alarm without coroutineScope" in IntelliJ'sDetailController)🤖 Generated with Claude Code