ballerina-platform / lsp4intellij

This language client library provides language server protocol support for IntelliJ IDEA and other Jetbrains IDEs.
Apache License 2.0
438 stars 104 forks source link

NullpointerException, URISyntaxException in FileUtils.java #328

Closed akashnagesh closed 1 year ago

akashnagesh commented 1 year ago

Description: URISyntaxException in VFSToURI(VirtualFile file) method in FileUtils.java causes NullPointerException.

https://github.com/ballerina-platform/lsp4intellij/blob/6e892fa9bd17be481cf46abeb512bf540dee69fe/src/main/java/org/wso2/lsp4intellij/utils/FileUtils.java#L202

VFSToURI

> public static String VFSToURI(VirtualFile file) { > try { > return sanitizeURI(new URL(file.getUrl().replace(" ", SPACE_ENCODED)).toURI().toString()); > } catch (MalformedURLException | URISyntaxException e) { > LOG.warn(e); > return null; > } > }

Stack Trace

> java.lang.NullPointerException: Property must not be null: uri > > at org.eclipse.lsp4j.util.Preconditions.checkNotNull(Preconditions.java:29) > > at org.eclipse.lsp4j.TextDocumentIdentifier.(TextDocumentIdentifier.java:34) > > at org.wso2.lsp4intellij.editor.EditorEventManager.(EditorEventManager.java:156) > > at org.wso2.lsp4intellij.client.languageserver.wrapper.LanguageServerWrapper.lambda$connect$3(LanguageServerWrapper.java:374)

Root Cause Analysis: File paths can have special characters like [, ] etc. The URL returned by calling VirtualFile.getPath() is not RFC-compliant.

Quoting the VirtualFile.getPath() documentation:

Please note these URLs are intended for use withing VFS - meaning they are not necessarily RFC-compliant.

Calling toURI() on a URL that does not conform to RFC2396 will result in URISyntaxException and a NullPointerException later on.

FileUtils.VFSToURI() should account for special characters in file path and sanitize the path correctly.

akashnagesh commented 1 year ago

Fixed in https://github.com/ballerina-platform/lsp4intellij/pull/329