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
Templates can come from a stale VFS cache after the plugin JAR is rebuilt #1949
After rebuilding the plugin, Choose template... still showed the TypeScript template without its transports, also after restarting the IDE, while Import template... of the same folder showed it right.
ServerTemplateManager.getTemplateRoot reads the templates through IntelliJ's JarFileSystem, whose content lives in the IDE's VFS cache. As far as I can tell, that cache is refreshed when something refreshes the JAR, not when the IDE starts, so a JAR rebuilt at the same path keeps its old content: the IDE's content.dat held the TypeScript template, but no "transports" anywhere. A plugin update normally gets a JAR with a new version in its name, so users are rarely affected; a rebuilt snapshot or runIde is.
Reading the templates with the plugin's class loader (getResourceAsStream), or refreshing the JAR root before reading it (VfsUtil.markDirtyAndRefresh), would avoid it.
How this was checked
Seen in runIde: the VFS cache held a template.json without "transports", while Import template... showed it. Not checked again with the caches cleared.
@muescha#2080 is no longer on GitHub. It now answers "Not Found", and so does its author's account, so its review is gone too. I'll pick this up with your proposal from there:
refresh the plugin's JAR once per IDE start, in a startup activity, where no read lock is held;
getTemplateRoot() goes back to findFileByPath, so creating the template services never refreshes;
as I suggested there, only in a development build (IntelliJPlatformUtils.isDevMode()), since a released LSP4IJ has its version in its JAR's name.
I'll check the dynamic reload in runIde, as you planned.
It replaces findFileByPath with refreshAndFindFileByPath in ServerTemplateManager.getTemplateRoot().
The two comments below are the replies from the review of fix: refresh the plugin JAR before reading the templates #2080. The second one explains why this diff can't go in as it is (synchronous refresh under a read lock) and proposes a refresh at startup instead.
diff --git a/src/main/java/com/redhat/devtools/lsp4ij/templates/ServerTemplateManager.java b/src/main/java/com/redhat/devtools/lsp4ij/templates/ServerTemplateManager.java
index 9cc3c7b23..89c784cab 100644
--- a/src/main/java/com/redhat/devtools/lsp4ij/templates/ServerTemplateManager.java+++ b/src/main/java/com/redhat/devtools/lsp4ij/templates/ServerTemplateManager.java@@ -87,7 +87,10 @@ public VirtualFile getTemplateRoot() {
String resourcePath = new URI(filePart).getSchemeSpecificPart();// get decoded part (i.e. converts %20 to spaces ...)
// resourcePath looks like /Users/username/Library/Application Support/JetBrains/IDEVersion/plugins/LSP4IJ/lib/instrumented-lsp4ij-version.jar!/templates/
LOGGER.debug("Templates resources path from uri : {}", resourcePath);
- return JarFileSystem.getInstance().findFileByPath(resourcePath);+ // Refresh the JAR content before reading it: after the plugin JAR is rebuilt+ // at the same path (snapshot or runIde), the VFS cache can still hold the+ // previous content, so templates would come from a stale cache (see #1949).+ return JarFileSystem.getInstance().refreshAndFindFileByPath(resourcePath);
} catch (URISyntaxException e) {
LOGGER.warn(e.getMessage());
}
Reply posted in #2080 on 2026-11-22 at 06:38 CET (edited at 06:39), copied here because #2080 is no longer reachable.
It answers CodeRabbit's review of 9370f348: the templates list is filled once in the constructor, so after a JAR rebuild while the IDE runs, getTemplates() would still return the old templates.
In short: the list is rebuilt on every IDE start and plugin reload; the stale content of this issue came from the VFS cache on disk.
@coderabbitai Thanks for the hint. Claude Code looked at this, and we think the list is not a problem in any state the IDE supports.
Three layers can hold the templates:
The JAR on disk. It has the new content after a rebuild.
IntelliJ's VFS cache, also on disk (content.dat and related files in the IDE's system folder). JarFileSystem reads from this cache, and the cache survives an IDE restart. When a JAR is rebuilt at the same path, nothing reads it again until something refreshes it.
The templates list in memory. It is filled once in the constructor.
What #1949 was about: after a rebuild and a restart (runIde), the service was created again and its constructor ran again. It still got the old template.json, because findFileByPath answered from the stale VFS cache (layer 2), which IntelliJ keeps on disk and which had kept the old content across the restart. So the stale data came from the cache on disk (layer 2), not from the list in memory (layer 3). Import template... showed the right content because it reads a folder on disk, not the JAR through the VFS cache. With refreshAndFindFileByPath, the constructor now checks the JAR on disk against the cache and reads it again if it changed.
When the constructor runs:LanguageServerTemplateManager and DAPTemplateManager are application services (plugin.xml), so:
yes (the old class loader and all of the plugin's services are thrown away)
current
JAR replaced while the IDE keeps running, with no restart and no reload
no
stale
About the last row: in that state, the cached list is the smallest problem. The plugin's class loader still holds the old JAR and has the old classes loaded, and classes that are loaded later would come from the new JAR, so old and new code would be mixed. The IntelliJ Platform doesn't support this, and neither runIde nor a plugin update gets you there. A way to reload only the template list would give new templates to old code, so it wouldn't make that case work.
So we'd keep the list as it is and leave this PR at the refresh.
Reply posted in #2080 on 2026-11-23 at 01:58 CET, copied here because #2080 is no longer reachable.
It answers lagergren's review of fix: refresh the plugin JAR before reading the templates #2080, which made two points: refreshAndFindFileByPath refreshes synchronously and may run under a read lock (the service can be created inside a read action), and only a JAR rebuilt at the same path goes stale, so the fix should stay with development builds and away from the read lock.
In short: both points are right; the proposal is to refresh the plugin JAR once at startup in a ProjectActivity, outside the read lock, and to go back to findFileByPath in getTemplateRoot().
Thanks, both points are right. In RefreshQueueImpl.execute, a synchronous refresh under a read lock only logs "Do not perform a synchronous refresh under read lock" and then doesn't refresh at all. So when the service is created inside a read action, this PR reports an error and the templates stay stale.
Why the VFS doesn't notice the rebuilt JAR: this doesn't look like a platform bug. VfsImplUtil.checkSubscription already throws away the cached content of a JAR and refreshes it asynchronously when LocalFileSystem reports a VFileContentChangeEvent for the .jar file. But that event only comes when something refreshes the .jar file itself, and nothing does that for a plugin JAR: the plugin's class loader reads the JAR directly, not through the VFS.
Proposal: refresh the JAR once at startup, outside the read lock
Whoever replaces the plugin JAR restarts the IDE or reloads the plugin, and both create the template services again. So one refresh per IDE start is enough, as long as it runs where no read lock is held:
A ProjectActivity (postStartupActivity) runs in a background coroutine without a read lock, so a synchronous refresh is allowed there (RefreshQueueImpl queues it and waits for it).
An AtomicBoolean makes it run once per IDE start, not once per opened project.
It refreshes the local .jar file and then the JAR root recursively, both synchronously. Refreshing a single unchanged file is just a timestamp and length check, so for a released LSP4IJ this costs one stat per start, and nothing else happens.
getTemplateRoot() goes back to findFileByPath, so the constructor never refreshes.
This needs no isDevMode() check, no refresh under a read lock, and no way to reload the template list. The templates are only needed in New Language Server and the DAP dialog, after a user action, so the refresh has finished long before. If the service were created earlier, it would be the same as today.
Not checked yet: whether the ProjectActivity of a dynamically loaded plugin runs for projects that are already open. A plugin update has a new JAR name anyway, and VfsImplUtil clears its archive handlers when a plugin is unloaded.
Earlier idea: detect the stale JAR and refresh asynchronously
The stale case can be detected cheaply and without a refresh, so also under a read lock, by comparing what the VFS has stored with the file on disk:
If it is stale: VfsUtil.markDirtyAndRefresh(true, true, true, localJar, jarRoot), and when it has finished, build the template list again and swap it in at once. This works under a read lock, but it needs a way to reload the list (CodeRabbit's point above). Also, the event for the local JAR makes VfsImplUtil start its own asynchronous refresh of the JAR content, so it would have to be checked that the content is already new when the callback runs. The startup refresh above avoids both.
Alternative: read the templates without the VFS
Read the templates of the plugin's own JAR directly, for example with FileSystems.newFileSystem(jarPath). That avoids the cache and the lock completely, but importServerTemplate and createLsTemplate/createDapTemplate work on VirtualFile, and Import template... uses them too, so it would be a bigger change.
Would the startup refresh work for you? If so, I'd update the PR with it and check it in runIde.
#!/bin/bashset -u
echo'--- referenced files ---'
fd -a '^(plugin\.xml|ServerTemplateManager\.java|LanguageServerTemplateManager\.java|DAPTemplateManager\.java)$'.echo'--- manager outlines ---'
fd -a '^(ServerTemplateManager\.java|LanguageServerTemplateManager\.java|DAPTemplateManager\.java)$'. -x ast-grep outline {} --items all
echo'--- plugin.xml service declarations ---'
fd -a '^plugin\.xml$'. -x rg -n -C 5 'LanguageServerTemplateManager|DAPTemplateManager' {}
echo'--- relevant manager source ---'fornamein ServerTemplateManager.java LanguageServerTemplateManager.java DAPTemplateManager.java;dowhile IFS= read -r file;doecho"--- $file ---"
cat -n "$file"| sed -n '1,220p'done<<(fd -a "^${name//./\\.}$" .)done
Length of output: 35090
Script executed:
#!/bin/bashset -u
echo'--- referenced files ---'
fd -a '^(plugin\.xml|ServerTemplateManager\.java|LanguageServerTemplateManager\.java|DAPTemplateManager\.java)$'.echo'--- manager outlines ---'
fd -a '^(ServerTemplateManager\.java|LanguageServerTemplateManager\.java|DAPTemplateManager\.java)$'. -x ast-grep outline {} --items all
echo'--- plugin.xml ---'
fd -a '^plugin\.xml$'. -x sh -c 'echo "--- $1 ---"; cat -n "$1"' sh {}
echo'--- relevant manager source ---'fornamein ServerTemplateManager.java LanguageServerTemplateManager.java DAPTemplateManager.java;dowhile IFS= read -r file;doecho"--- $file ---"
cat -n "$file"| sed -n '1,220p'done<<(fd -a "^${name//./\\.}$" .)done
Length of output: 120857
@muescha Thanks for the explanation. I agree and withdraw my concern about reloading the in-memory list.
plugin.xml registers both template managers as application services, and ServerTemplateManager builds the list in its constructor. For the restart case in #1949, refreshing the VFS cache before service construction addresses the issue without reloading the list.
For the later startup proposal, the refresh must finish before either service is created. Otherwise, the constructor can still cache stale templates. Dynamic plugin reload also remains to be checked, as you noted.
Activity
lagergren commentedon Nov 24, 2026
@muescha #2080 is no longer on GitHub. It now answers "Not Found", and so does its author's account, so its review is gone too. I'll pick this up with your proposal from there:
getTemplateRoot()goes back tofindFileByPath, so creating the template services never refreshes;IntelliJPlatformUtils.isDevMode()), since a released LSP4IJ has its version in its JAR's name.I'll check the dynamic reload in
runIde, as you planned.muescha commentedon Nov 24, 2026
The fix from #2080, kept here because #2080 is no longer reachable (it returns 404).
fix/template-root-refresh-jar-1949, commit9370f348"Refresh the plugin JAR before reading the templates (Templates can come from a stale VFS cache after the plugin JAR is rebuilt #1949)".findFileByPathwithrefreshAndFindFileByPathinServerTemplateManager.getTemplateRoot().muescha commentedon Nov 24, 2026
Reply posted in #2080 on 2026-11-22 at 06:38 CET (edited at 06:39), copied here because #2080 is no longer reachable.
9370f348: thetemplateslist is filled once in the constructor, so after a JAR rebuild while the IDE runs,getTemplates()would still return the old templates.@coderabbitaiThanks for the hint. Claude Code looked at this, and we think the list is not a problem in any state the IDE supports.Three layers can hold the templates:
content.datand related files in the IDE's system folder).JarFileSystemreads from this cache, and the cache survives an IDE restart. When a JAR is rebuilt at the same path, nothing reads it again until something refreshes it.templateslist in memory. It is filled once in the constructor.What #1949 was about: after a rebuild and a restart (
runIde), the service was created again and its constructor ran again. It still got the oldtemplate.json, becausefindFileByPathanswered from the stale VFS cache (layer 2), which IntelliJ keeps on disk and which had kept the old content across the restart. So the stale data came from the cache on disk (layer 2), not from the list in memory (layer 3). Import template... showed the right content because it reads a folder on disk, not the JAR through the VFS cache. WithrefreshAndFindFileByPath, the constructor now checks the JAR on disk against the cache and reads it again if it changed.When the constructor runs:
LanguageServerTemplateManagerandDAPTemplateManagerare application services (plugin.xml), so:runIde(the case of #1949)About the last row: in that state, the cached list is the smallest problem. The plugin's class loader still holds the old JAR and has the old classes loaded, and classes that are loaded later would come from the new JAR, so old and new code would be mixed. The IntelliJ Platform doesn't support this, and neither
runIdenor a plugin update gets you there. A way to reload only the template list would give new templates to old code, so it wouldn't make that case work.So we'd keep the list as it is and leave this PR at the refresh.
muescha commentedon Nov 24, 2026
Reply posted in #2080 on 2026-11-23 at 01:58 CET, copied here because #2080 is no longer reachable.
refreshAndFindFileByPathrefreshes synchronously and may run under a read lock (the service can be created inside a read action), and only a JAR rebuilt at the same path goes stale, so the fix should stay with development builds and away from the read lock.ProjectActivity, outside the read lock, and to go back tofindFileByPathingetTemplateRoot().Thanks, both points are right. In
RefreshQueueImpl.execute, a synchronous refresh under a read lock only logs "Do not perform a synchronous refresh under read lock" and then doesn't refresh at all. So when the service is created inside a read action, this PR reports an error and the templates stay stale.Why the VFS doesn't notice the rebuilt JAR: this doesn't look like a platform bug.
VfsImplUtil.checkSubscriptionalready throws away the cached content of a JAR and refreshes it asynchronously whenLocalFileSystemreports aVFileContentChangeEventfor the.jarfile. But that event only comes when something refreshes the.jarfile itself, and nothing does that for a plugin JAR: the plugin's class loader reads the JAR directly, not through the VFS.Proposal: refresh the JAR once at startup, outside the read lock
Whoever replaces the plugin JAR restarts the IDE or reloads the plugin, and both create the template services again. So one refresh per IDE start is enough, as long as it runs where no read lock is held:
ProjectActivity(postStartupActivity) runs in a background coroutine without a read lock, so a synchronous refresh is allowed there (RefreshQueueImplqueues it and waits for it).AtomicBooleanmakes it run once per IDE start, not once per opened project..jarfile and then the JAR root recursively, both synchronously. Refreshing a single unchanged file is just a timestamp and length check, so for a released LSP4IJ this costs onestatper start, and nothing else happens.getTemplateRoot()goes back tofindFileByPath, so the constructor never refreshes.This needs no
isDevMode()check, no refresh under a read lock, and no way to reload the template list. The templates are only needed in New Language Server and the DAP dialog, after a user action, so the refresh has finished long before. If the service were created earlier, it would be the same as today.Not checked yet: whether the
ProjectActivityof a dynamically loaded plugin runs for projects that are already open. A plugin update has a new JAR name anyway, andVfsImplUtilclears its archive handlers when a plugin is unloaded.Earlier idea: detect the stale JAR and refresh asynchronously
The stale case can be detected cheaply and without a refresh, so also under a read lock, by comparing what the VFS has stored with the file on disk:
If it is stale:
VfsUtil.markDirtyAndRefresh(true, true, true, localJar, jarRoot), and when it has finished, build the template list again and swap it in at once. This works under a read lock, but it needs a way to reload the list (CodeRabbit's point above). Also, the event for the local JAR makesVfsImplUtilstart its own asynchronous refresh of the JAR content, so it would have to be checked that the content is already new when the callback runs. The startup refresh above avoids both.Alternative: read the templates without the VFS
Read the templates of the plugin's own JAR directly, for example with
FileSystems.newFileSystem(jarPath). That avoids the cache and the lock completely, butimportServerTemplateandcreateLsTemplate/createDapTemplatework onVirtualFile, and Import template... uses them too, so it would be a bigger change.Would the startup refresh work for you? If so, I'd update the PR with it and check it in
runIde.coderabbitai commentedon Nov 24, 2026
Analysis chain
Script executed:
Length of output: 35090
Script executed:
Length of output: 120857
@mueschaThanks for the explanation. I agree and withdraw my concern about reloading the in-memory list.plugin.xmlregisters both template managers as application services, andServerTemplateManagerbuilds the list in its constructor. For the restart case in#1949, refreshing the VFS cache before service construction addresses the issue without reloading the list.For the later startup proposal, the refresh must finish before either service is created. Otherwise, the constructor can still cache stale templates. Dynamic plugin reload also remains to be checked, as you noted.
You are interacting with an AI system.
muescha commentedon Nov 24, 2026
The three comments above are what I still found in my temporary files. Maybe they help to follow the ideas of #2080.
fix: read the templates of a rebuilt LSP4IJ JAR at the next start of …