unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#58790: Eglot URI parsing bug when using clojure-lsp server
@ 2022-10-25 21:44 Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-10-26  6:22 ` Stefan Kangas
                   ` (2 more replies)
  0 siblings, 3 replies; 61+ messages in thread
From: Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-10-25 21:44 UTC (permalink / raw)
  To: 58790


[-- Attachment #1.1.1: Type: text/plain, Size: 2357 bytes --]

Hello,

I am submitting 2 patches that were discussed over on eglot's github
issue tracker: https://github.com/joaotavora/eglot/issues/661

Only one of the patches is needed to solve the issue, but I wanted to
present both options.

The problem occurs when using `xref-find-defintions` while eglot is
managing a clojure buffer with clojure-lsp. If the symbol that
xref-find-definitions is activated on is defined in a jar file,
clojure-lsp by default will provide a location with a response like the 
following

(:jsonrpc "2.0" :id 14 :result
   (:uri 
"zipfile:///Users/andreperictavares/.m2/repository/org/clojure/tools.namespace/0.3.1/tools.namespace-0.3.1.jar::clojure/tools/namespace/find.clj" 
... ))

However, there is a clojure-lsp setting that the maintainers of 
clojure-lsp would like to make default that changes the URI format to 
send a response like this

(:jsonrpc "2.0" :id 14 :result
   (:uri 
"jar:file:///Users/andreperictavares/.m2/repository/org/clojure/tools.namespace/0.3.1/tools.namespace-0.3.1.jar!clojure/tools/namespace/find.clj" 
... )).

This jar format URI is a common thing used in the JVM world apparently. 
It is a URI that contains a nested URI. If eglot is provided this kind 
of URI it needs to parse it TWICE before eglot dispatches the path. If 
it's only parsed once then emacs ends up trying to navigate using 
something roughly equivalent to `(find-file "file:///path/to/jar")` 
which does not work. That is what the patch titled 
`0001-Parse-jar-scheme-URIs-in-eglot-correctly.patch` fixes.

The other patch forces clojure-lsp to use the `zipfile` scheme and 
avoids the need to parse the URI twice. I prefer the double parsing 
patch though, as I believe if other language servers for JVM languages 
use this jar URI scheme, they would also be able to benefit from the patch.

I have a very simple repository here that can be used to test the 
problem here: 
https://git.sr.ht/~dannyfreeman/eglot-xref-to-jar-repo/tree/main/item/src/user.clj#L4

With clojure and clojure-lsp installed, eglot activated in a buffer 
visiting that `user.clj` file, the issue can be recreated.

This is my first time attempting to contribute to emacs. I am actively 
going through the process of copyright assignment right now in case it 
is necessary.

Thank you,
Danny Freeman

[-- Attachment #1.1.2: 0001-Initialize-clojure-lsp-with-the-zipfile-dependency-s.patch --]
[-- Type: text/x-patch, Size: 1945 bytes --]

From c20c0185929fbb3f5ca0101cab38721ddac412d6 Mon Sep 17 00:00:00 2001
From: dannyfreeman <danny@dfreeman.email>
Date: Tue, 25 Oct 2022 08:15:26 -0400
Subject: [PATCH] Initialize clojure-lsp with  the "zipfile" dependency-scheme
 option

When eglot is provided clojure dependencies that are located in external
jars, there are two formats they could be provided in.

The recommended format is "jar", which contains a jar URI like
"jar:file:///path/to/jar!/path/in/jar"
this URI contains a nested URI that eglot is not equipped to handle.

Setting this value to "zipfile" provides them in the following format
"zipfile:///path/to/jar::/path/in/jar"
which doesnt not contain any nested URIs.

This change ensures that we use zipfile so that the URI always correctly
has the scheme removed from the uri, and ends up being provided to xref
like "/path/to/jar::path/in/jar".
---
 lisp/progmodes/eglot.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index a28df6c2d5..c9d08de0d9 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -230,7 +230,7 @@ language-server/bin/php-language-server.php"))
                                 (html-mode . ,(eglot-alternatives '(("vscode-html-language-server" "--stdio") ("html-languageserver" "--stdio"))))
                                 (dockerfile-mode . ("docker-langserver" "--stdio"))
                                 ((clojure-mode clojurescript-mode clojurec-mode)
-                                 . ("clojure-lsp"))
+                                 . ("clojure-lsp" :initializationOptions (:dependency-scheme "zipfile")))
                                 (csharp-mode . ("omnisharp" "-lsp"))
                                 (purescript-mode . ("purescript-language-server" "--stdio"))
                                 (perl-mode . ("perl" "-MPerl::LanguageServer" "-e" "Perl::LanguageServer::run"))
-- 
2.37.3


[-- Attachment #1.1.3: 0001-Parse-jar-scheme-URIs-in-eglot-correctly.patch --]
[-- Type: text/x-patch, Size: 2158 bytes --]

From f6e0a6d1ff557719b11cdf357fc24246c9da6a86 Mon Sep 17 00:00:00 2001
From: dannyfreeman <danny@dfreeman.email>
Date: Tue, 25 Oct 2022 08:22:20 -0400
Subject: [PATCH] Parse "jar" scheme URIs in eglot correctly

jar schemes contain nested URIs, like-this'
"jar:file:///home/user/some.jar!/path/in/jar" such that when eglot was
parsing these URIs provided by lsp servers, it ended up trying to
navigate to "file:///home/user/some.jar!/path/in/jar".

This change parses the URIs twice if the scheme is "jar", so that the
final parsed URI looks like "/home/user/some.jar!/path/in/jar" instead.

When providing this path to a jar to tools like xref, emacs will still
not correctly open the file within the jar, but this change makes that
work easier.
---
 lisp/progmodes/eglot.el | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index a28df6c2d5..549de5cba4 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -1501,12 +1501,22 @@ If optional MARKER, return a marker instead"
              (directory-file-name (file-local-name truepath))
              eglot--uri-path-allowed-chars))))
 
+(defun eglot--parse-uri (uri)
+  (url-unhex-string
+   (url-filename
+    (let ((url (url-generic-parse-url uri)))
+      (if (string= "jar" (url-type url))
+          ;; jar: URIs can contain a nested URI, so we need to parse twice.
+          ;; For example, `jar:file:///home/user/some.jar!/path/in/jar'
+          (url-generic-parse-url (url-filename url))
+        url)))))
+
 (defun eglot--uri-to-path (uri)
   "Convert URI to file path, helped by `eglot--current-server'."
   (when (keywordp uri) (setq uri (substring (symbol-name uri) 1)))
   (let* ((server (eglot-current-server))
          (remote-prefix (and server (eglot--trampish-p server)))
-         (retval (url-unhex-string (url-filename (url-generic-parse-url uri))))
+         (retval (eglot--parse-uri uri))
          ;; Remove the leading "/" for local MS Windows-style paths.
          (normalized (if (and (not remote-prefix)
                               (eq system-type 'windows-nt)
-- 
2.37.3


[-- Attachment #1.1.4: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3185 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

^ permalink raw reply related	[flat|nested] 61+ messages in thread

end of thread, other threads:[~2022-12-10 17:45 UTC | newest]

Thread overview: 61+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-25 21:44 bug#58790: Eglot URI parsing bug when using clojure-lsp server Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-26  6:22 ` Stefan Kangas
2022-10-26 19:50   ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-27 15:09     ` João Távora
2022-10-29  1:22       ` Dmitry Gutov
2022-10-29  2:02         ` João Távora
2022-10-29 14:54           ` Dmitry Gutov
2022-10-29 19:35             ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-31 14:40               ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-02  8:09                 ` João Távora
2022-11-02 13:15                   ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-03 17:10                   ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-10  9:49                     ` Eli Zaretskii
2022-11-10 11:00                       ` João Távora
2022-11-10 13:47                         ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-10 15:38                         ` Eli Zaretskii
2022-11-10 21:45                           ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-10 21:59                             ` João Távora
2022-11-10 22:22                               ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-10 22:30                                 ` João Távora
2022-11-10 22:48                                   ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-10 22:48                                 ` João Távora
2022-11-10 22:57                                   ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-11  7:29                                   ` Eli Zaretskii
2022-11-12 17:03                                     ` Michael Albinus
2022-11-13 21:04                                       ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-15 19:04                                         ` Michael Albinus
2022-11-15 22:28                                           ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-16  7:53                                             ` Michael Albinus
2022-11-16 10:21                                               ` João Távora
2022-11-16 15:45                                                 ` Michael Albinus
2022-11-16 16:20                                                   ` João Távora
2022-11-16 22:59                                                     ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-12-02 16:14                                                       ` Michael Albinus
2022-12-07 18:56                                                         ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-12-08 13:46                                                           ` Michael Albinus
2022-12-08 19:07                                                             ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-12-09 16:04                                                               ` Michael Albinus
2022-12-10 17:21                                                                 ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-12-10 17:45                                                                   ` Michael Albinus
2022-11-22 14:30                                                     ` Michael Albinus
2022-11-23 11:55                                                       ` Richard Copley
2022-11-23 12:36                                                         ` João Távora
2022-11-23 12:42                                                           ` Arash Esbati
2022-11-23 12:49                                                             ` Richard Copley
2022-11-23 12:54                                                               ` João Távora
2022-11-23 13:33                                                           ` Eli Zaretskii
2022-11-23 13:44                                                             ` João Távora
2022-11-23 14:03                                                               ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-23 19:53                                                                 ` João Távora
2022-11-11  8:30                               ` Eli Zaretskii
2022-11-11  9:45                                 ` João Távora
2022-11-11 12:01                                   ` Eli Zaretskii
2022-11-11 14:02                                     ` João Távora
2022-11-11 14:45                                       ` Eli Zaretskii
2022-11-12  9:04                                         ` João Távora
2022-11-11  7:16                             ` Eli Zaretskii
2022-11-01 17:25         ` Juri Linkov
2022-10-29 15:36 ` Felician Nemeth
2022-10-29 17:09   ` João Távora
2022-11-09  0:59 ` bug#58790: Robert Brown

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).