Skip to content

Integrate rust-analyzer and improve LSP Integration (required function, guards for empty replies, icons, deadlock fix)#9249

Open
matthiasblaesing wants to merge 7 commits intoapache:masterfrom
matthiasblaesing:rust-analyser
Open

Integrate rust-analyzer and improve LSP Integration (required function, guards for empty replies, icons, deadlock fix)#9249
matthiasblaesing wants to merge 7 commits intoapache:masterfrom
matthiasblaesing:rust-analyser

Conversation

@matthiasblaesing
Copy link
Contributor

General improvements (lsp.client)

To be able to sanely use rust-analyzer (the rust LSP implementation) the NetBeans lsp client implementation needs some adjustments:

  • lsp4j expects client implementation to provide at least a dummy implementation of refreshDiagnostics, failing to correctly work if that is not the case
  • it was observed that the rust lsp in many cases returns null as a result of LSP operations. The existing null guards were thus extended
  • icons in the lsp.client module were referenced but not actually present causing exceptions at runtime when they were to be used.
  • it was observed, that requests to apply document formatting might block if the language servers issues a publishDiagnostics call as part of the formatting.
  • The CSL task was executed independently of the presence of its prerequisites, overwriting the results of other tasks if necessary.

Rust specific

The cargo TOML support is updated to the current version of TOML to be able to parse project files using extended syntax.

While NetBeans can provide parts of the IDE services based on custom implementations, the rust-analyzer LSP server provides extended support of the box. Compared with the built-in solution rust-analyzer has the drawback, that it is huge and platform dependent, so it can't be bundled with NetBeans.

To provide both minimal rust support in all cases and improved editing support by using rust-analyzer the solution provided here will switch from internal support to rust-analyzer only if rust-analyzer is installed and configured.

When a user now opens a rust file and rust-analyzer is not configured a warning is attached to the file (as is done for cpplite):

Bildschirmfoto vom 2026-03-06 21-14-19

But structure is still present:

Bildschirmfoto vom 2026-03-06 21-15-20

The icons used in the rust module were aligned with the other NetBeans icons from lsp.client and PHP support.

The warning allows to directly jump into options to configure rust-analyzer:

Bildschirmfoto vom 2026-03-06 21-14-52 Bildschirmfoto vom 2026-03-06 21-15-20 Bildschirmfoto vom 2026-03-06 21-15-30

With that done extended structure support is activated:

Bildschirmfoto vom 2026-03-06 21-17-23

Code completion is provided:

Bildschirmfoto vom 2026-03-06 21-18-09

and Find usages works:

Bildschirmfoto vom 2026-03-06 21-20-43 Bildschirmfoto vom 2026-03-06 21-20-34

…hDiagnostic

Test with rust-analyzer yielded the following exception:

java.lang.UnsupportedOperationException
	at org.eclipse.lsp4j.services.LanguageClient.refreshDiagnostics(LanguageClient.java:251)
Caused: java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint.lambda$null$0(GenericEndpoint.java:65)
Caused: java.lang.RuntimeException
	at org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint.lambda$null$0(GenericEndpoint.java:67)
	at org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint.request(GenericEndpoint.java:120)
[catch] at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.handleRequest(RemoteEndpoint.java:261)
	at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.consume(RemoteEndpoint.java:190)
	at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.handleMessage(StreamMessageProducer.java:194)
	at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.listen(StreamMessageProducer.java:94)
	at org.eclipse.lsp4j.jsonrpc.json.ConcurrentMessageProcessor.run(ConcurrentMessageProcessor.java:113)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:840)
…cument

It was observed, that with the rust-analyzer language server inserting a
completion led to a dead-lock. Checking the stacks of running threads shows,
that the LS tries to publish diagostics, while in parallel a documentFormat is
called.

The assumption is, that the rust side block execution of the `documentFormat`
request until the `publishDiagnostics` call from the LS finishes. The latter
though needs access to the document lock and that is already taken by the
`documentFormat` task.

"AWT-EventQueue-0" apache#35 prio=6 os_prio=0 cpu=8224,39ms elapsed=119,39s tid=0x00007f5384156f60 nid=0x2b0da7 waiting on condition  [0x00007f537e7f9000]
   java.lang.Thread.State: WAITING (parking)
	at jdk.internal.misc.Unsafe.park(java.base@17.0.9/Native Method)
	- parking to wait for  <0x00000006448493e8> (a java.util.concurrent.CompletableFuture$Signaller)
	at java.util.concurrent.locks.LockSupport.park(java.base@17.0.9/LockSupport.java:211)
	at java.util.concurrent.CompletableFuture$Signaller.block(java.base@17.0.9/CompletableFuture.java:1864)
	at java.util.concurrent.ForkJoinPool.unmanagedBlock(java.base@17.0.9/ForkJoinPool.java:3465)
	at java.util.concurrent.ForkJoinPool.managedBlock(java.base@17.0.9/ForkJoinPool.java:3436)
	at java.util.concurrent.CompletableFuture.waitingGet(java.base@17.0.9/CompletableFuture.java:1898)
	at java.util.concurrent.CompletableFuture.get(java.base@17.0.9/CompletableFuture.java:2072)
	at org.netbeans.modules.lsp.client.bindings.Formatter.documentFormat(Formatter.java:122)
	at org.netbeans.modules.lsp.client.bindings.Formatter.reformat(Formatter.java:87)
	at org.netbeans.modules.editor.indent.TaskHandler$MimeItem.runTask(TaskHandler.java:550)
	at org.netbeans.modules.editor.indent.TaskHandler.runTasks(TaskHandler.java:309)
	at org.netbeans.modules.editor.indent.IndentImpl.reformat(IndentImpl.java:349)
	at org.netbeans.modules.editor.indent.api.Reformat.reformat(Reformat.java:129)
	at org.netbeans.lib.editor.codetemplates.CodeTemplateInsertHandler.run(CodeTemplateInsertHandler.java:352)
	at org.netbeans.editor.GuardedDocument.runAtomicAsUser(GuardedDocument.java:333)
	at org.netbeans.lib.editor.codetemplates.CodeTemplateInsertHandler.insertTemplate(CodeTemplateInsertHandler.java:258)
	at org.netbeans.lib.editor.codetemplates.CodeTemplateInsertHandler.processTemplate(CodeTemplateInsertHandler.java:229)
	at org.netbeans.lib.editor.codetemplates.CodeTemplateManagerOperation.insert(CodeTemplateManagerOperation.java:273)
	at org.netbeans.lib.editor.codetemplates.api.CodeTemplate.insert(CodeTemplate.java:82)
	at org.netbeans.modules.lsp.client.bindings.CompletionProviderImpl$3.lambda$commit$0(CompletionProviderImpl.java:307)
	at org.netbeans.modules.lsp.client.bindings.CompletionProviderImpl$3$$Lambda$825/0x00007f53e4ae8498.run(Unknown Source)
	at org.netbeans.editor.GuardedDocument.runAtomic(GuardedDocument.java:296)
	at org.openide.text.NbDocument.runAtomic(NbDocument.java:411)
	at org.netbeans.modules.lsp.client.bindings.CompletionProviderImpl$3.commit(CompletionProviderImpl.java:254)
	at org.netbeans.modules.lsp.client.bindings.CompletionProviderImpl$3.defaultAction(CompletionProviderImpl.java:231)
	at org.netbeans.modules.editor.completion.CompletionImpl.dispatchKeyEvent(CompletionImpl.java:785)
	at org.netbeans.modules.editor.completion.CompletionImpl.keyPressed(CompletionImpl.java:386)
	at java.awt.AWTEventMulticaster.keyPressed(java.desktop@17.0.9/AWTEventMulticaster.java:258)
        [...]

"pool-4-thread-1" apache#75 prio=5 os_prio=0 cpu=136,37ms elapsed=76,96s tid=0x00007f5380ae1080 nid=0x2b0e84 in Object.wait()  [0x00007f52e99fc000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(java.base@17.0.9/Native Method)
	- waiting on <no object reference available>
	at java.lang.Object.wait(java.base@17.0.9/Object.java:338)
	at javax.swing.text.AbstractDocument.readLock(java.desktop@17.0.9/AbstractDocument.java:1421)
	- locked <0x000000063c6af680> (a org.netbeans.modules.csl.core.GsfDocument)
	at org.netbeans.editor.BaseDocument.render(BaseDocument.java:1405)
	at org.openide.text.PositionRef$Manager$DocumentRenderer.render(PositionRef.java:927)
	at org.openide.text.PositionRef$Manager$DocumentRenderer.renderToObject(PositionRef.java:948)
	at org.openide.text.PositionRef$Manager.addPosition(PositionRef.java:389)
	at org.openide.text.PositionRef.init(PositionRef.java:105)
	at org.openide.text.PositionRef.<init>(PositionRef.java:90)
	at org.openide.text.PositionRef.<init>(PositionRef.java:68)
	at org.openide.text.CloneableEditorSupport.createPositionRef(CloneableEditorSupport.java:1264)
	at org.netbeans.modules.editor.hints.HintsControllerImpl.linePart(HintsControllerImpl.java:231)
	at org.netbeans.spi.editor.hints.ErrorDescriptionFactory.createErrorDescription(ErrorDescriptionFactory.java:245)
	at org.netbeans.spi.editor.hints.ErrorDescriptionFactory.createErrorDescription(ErrorDescriptionFactory.java:218)
	at org.netbeans.modules.lsp.client.bindings.LanguageClientImpl.lambda$publishDiagnostics$0(LanguageClientImpl.java:176)
	at org.netbeans.modules.lsp.client.bindings.LanguageClientImpl$$Lambda$705/0x00007f53e4ac73a8.apply(Unknown Source)
	at java.util.stream.ReferencePipeline$3$1.accept(java.base@17.0.9/ReferencePipeline.java:197)
	at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(java.base@17.0.9/ArrayList.java:1625)
	at java.util.stream.AbstractPipeline.copyInto(java.base@17.0.9/AbstractPipeline.java:509)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(java.base@17.0.9/AbstractPipeline.java:499)
	at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(java.base@17.0.9/ReduceOps.java:921)
	at java.util.stream.AbstractPipeline.evaluate(java.base@17.0.9/AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.collect(java.base@17.0.9/ReferencePipeline.java:682)
	at org.netbeans.modules.lsp.client.bindings.LanguageClientImpl.publishDiagnostics(LanguageClientImpl.java:177)
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(java.base@17.0.9/Native Method)
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(java.base@17.0.9/NativeMethodAccessorImpl.java:77)
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(java.base@17.0.9/DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(java.base@17.0.9/Method.java:568)
	at org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint.lambda$recursiveFindRpcMethods$0(GenericEndpoint.java:65)
	at org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint$$Lambda$648/0x00007f53e4a4dd50.apply(Unknown Source)
	at org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint.notify(GenericEndpoint.java:160)
	at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.handleNotification(RemoteEndpoint.java:231)
        [...]
- add support for rust LSP implementation rust-analyzer
- align rust icons more with existing NB icons
- show hint if rust analyser is not configured
@matthiasblaesing matthiasblaesing added this to the NB30 milestone Mar 6, 2026
@matthiasblaesing matthiasblaesing added LSP [ci] enable Language Server Protocol tests Rust [ci] enable Rust tests labels Mar 6, 2026
@matthiasblaesing
Copy link
Contributor Author

@vieiro could you please have a general look?

@lahodaj could you please have look at the general LSP and CSL changes and see if they make sense to you?

@vieiro
Copy link
Contributor

vieiro commented Mar 8, 2026

Hi @matthiasblaesing ,

Thanks for this effort!

I wasn't able to get LSP server running properly with:

$ which rust-analyzer 
~/.cargo/bin/rust-analyzer
$ rust-analyzer --version
rust-analyzer 1.90.0 (1159e78 2025-09-14)

The NetBeans UI froze, with the EDT trying to update nodes [1].

I haven't found time to look at it in detail, but from [2] I wonder if we should be using SwingUtilities.invokeLater in the setKeys near org.netbeans.modules.lsp.client.bindings.NavigatorPanelImpl.run(NavigatorPanelImpl.java:77)

I think it's premature to add this to the IDE right now. Maybe we want to iterate a little bit more.

Kind regards,
Antonio

[1]

   java.lang.Thread.State: RUNNABLE
	at org.openide.util.Mutex.isReadAccess(Mutex.java:313)
	at org.openide.nodes.EntrySupportDefault.getArray(EntrySupportDefault.java:673)
	at org.openide.nodes.EntrySupportDefault$Info.nodes(EntrySupportDefault.java:781)
	at org.openide.nodes.EntrySupportDefault.justComputeNodes(EntrySupportDefault.java:172)
	at org.openide.nodes.ChildrenArray.nodes(ChildrenArray.java:63)
	at org.openide.nodes.EntrySupportDefault.getNodes(EntrySupportDefault.java:107)
	at org.openide.nodes.EntrySupportDefault.getNodes(EntrySupportDefault.java:149)
	at org.openide.nodes.Children.getNodes(Children.java:444)
	at org.openide.nodes.Node.assignTo(Node.java:318)
	- locked <0x0000000421bd4f88> (a java.lang.Object)
	at org.openide.nodes.EntrySupportDefault.justComputeNodes(EntrySupportDefault.java:189)
	at org.openide.nodes.ChildrenArray.nodes(ChildrenArray.java:63)
	at org.openide.nodes.EntrySupportDefault.getNodes(EntrySupportDefault.java:107)
	at org.openide.nodes.EntrySupportDefault.getNodes(EntrySupportDefault.java:149)
	at org.openide.nodes.Children.getNodes(Children.java:444)
	at org.openide.nodes.Node.assignTo(Node.java:318)
	- locked <0x0000000421bd4f88> (a java.lang.Object)
	at org.openide.nodes.EntrySupportDefault.justComputeNodes(EntrySupportDefault.java:189)
	at org.openide.nodes.ChildrenArray.nodes(ChildrenArray.java:63)
	at org.openide.nodes.EntrySupportDefault.getNodes(EntrySupportDefault.java:107)
	at org.openide.nodes.EntrySupportDefault.getNodes(EntrySupportDefault.java:149)
	at org.openide.nodes.Children.getNodes(Children.java:444)
	at org.openide.nodes.Node.assignTo(Node.java:318)
	- locked <0x0000000421bd4f88> (a java.lang.Object)
	at org.openide.nodes.EntrySupportDefault.justComputeNodes(EntrySupportDefault.java:189)
	at org.openide.nodes.ChildrenArray.nodes(ChildrenArray.java:63)
	at org.openide.nodes.EntrySupportDefault.getNodes(EntrySupportDefault.java:107)
	at org.openide.nodes.EntrySupportDefault.getNodes(EntrySupportDefault.java:149)
	at org.openide.nodes.Children.getNodes(Children.java:444)
	at org.openide.nodes.Node.assignTo(Node.java:318)

[2]

	at org.openide.nodes.Children$Keys.applyKeys(Children.java:1556)
	at org.openide.nodes.Children$Keys.setKeys(Children.java:1500)
	at org.netbeans.modules.lsp.client.bindings.NavigatorPanelImpl.run(NavigatorPanelImpl.java:77)
	at org.netbeans.modules.lsp.client.LSPBindings.lambda$addBackgroundTask$19(LSPBindings.java:563)
	at org.netbeans.modules.lsp.client.LSPBindings$$Lambda/0x00000000689ee8f0.run(Unknown Source)

@vieiro
Copy link
Contributor

vieiro commented Mar 8, 2026

The thread dump.txt I was able to extract when the IDE froze.

.start();
return LanguageServerDescription.create(p.getInputStream(), p.getOutputStream(), p);
} catch (IOException ex) {
LOG.log(Level.FINE, null, ex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use Level.INFO for this, so one can see what may be failing when launching rust-analyzer as a background process. It's also worth investigating when the LSP server is restarted.

@matthiasblaesing
Copy link
Contributor Author

I think it's premature to add this to the IDE right now. Maybe we want to iterate a little bit more.

I'll look into the points you brought up. BUT I have to ask: for what do we want to wait? Rust Support in NetBeans has not moved an inch and while this is rough, not doing it will not yield improvements.

@matthiasblaesing
Copy link
Contributor Author

Where does that version of rust-analyzer come from? I looked through

https://github.com/rust-lang/rust-analyzer/releases

and

https://github.com/rust-lang/rust-analyzer/tags

there is neither a tag, nor a release entry in september of last year. I fail to find the reference 1159e78 in the rust-analyzer repository (it does not resolve to a commit). The latest version of rust-analyzer is 0.3.2811, so that also makes no sense.

@matthiasblaesing
Copy link
Contributor Author

@vieiro could you please describe how you reached the deadlock and whether that is reproducible?

@vieiro
Copy link
Contributor

vieiro commented Mar 8, 2026

I think it's premature to add this to the IDE right now. Maybe we want to iterate a little bit more.

I'll look into the points you brought up. BUT I have to ask: for what do we want to wait? Rust Support in NetBeans has not moved an inch and while this is rough, not doing it will not yield improvements.

Very true! If this is on master we'll have more chances to get contributions!

@vieiro
Copy link
Contributor

vieiro commented Mar 8, 2026

@vieiro could you please describe how you reached the deadlock and whether that is reproducible?

I had a project opened (and a Rust file opened in the editor). Then the IDE got restarted (after changing the rust-analyzer path) and then it happened. I think there's a race condition between the LSP server being started and the opened files being sent to the LSP server, or something like that.

IIRC there were two rust-analyzer servers running at the same time. I'll try to get a reproducer, though.

updateFocused();
}
});
updateFocused();
Copy link
Member

@mbien mbien Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a guess regarding the reported freeze:

  1. wrap this line into SwingUtilities.invokeLater(line) or replace @OnStart with @OnShowing
  2. remove synchronized from updateFocused() (since no longer needed)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be.
Maybe we should redesign the LSPClient in NetBeans.
This should be a state machine, that queues requests in order and does not execute them until the LSP server has been properly initialized.
I have a small draft that initializes servers somewhere (but all the rest is still missing). May want to recover it from backup...

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

Labels

LSP [ci] enable Language Server Protocol tests Rust [ci] enable Rust tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants