Subject: bug#58790: Eglot URI parsing bug when using clojure-lsp server

From: Danny Freeman <[email-masked]>
Date: Thu, 03 Nov 2022 13:10:32 -0400

João Távora <[email-masked]> writes:

> I think it's better to patch eglot--uri-to-path so that if X looks
> anything other than file://, Eglot leaves it untouched.  After all, it's
> the only safe translation Eglot can make.

I've attached patch
0001-Only-handle-file-type-URIs-in-eglot-explicitly.patch
to this email that does this pretty well

> And in eglot--path-to-uri, we do likewise.  If the PATH argument already
> looks vaguely URIish (say, it makes something like "^[[:alnum:]]+://")
> we leave it unchanged.

This is done by just trying to parse the path as a URL and checking the
url-type instead of checking a regex.

> Felicián also suggested that Eglot warns the user when it doesn't know
> an URI scheme.  I think that can make sense in some situations, for now
> let's assume it isn't as important as getting your new Jar
> file-name-handler to integrate with Eglot.  Maybe in some later patch
> Eglot can somehow predict if there is a file-name-handler entry for a
> given URI and only warn if there isn't.

This change is only a matter of consulting the `find-file-name-handler`
function. I added another patch for that as
0002-Warn-when-eglot-receives-a-non-file-type-URI-that-Em.patch

>From 936034d0e72621815584680a9e75f44a4448ba9d Mon Sep 17 00:00:00 2001
From: dannyfreeman <[email-masked]>
Date: Thu, 3 Nov 2022 09:39:16 -0400
Subject: [PATCH 1/2] Only handle file:// type URIs in eglot explicitly

(Bug#58790)

This issue originated with clojure-lsp sending clients "jar": type URIs
that emacs is unable to handle out of the box. Before this change, jar:
URIs were parsed once, but since jar: URIs contain a nested URI,
this resulted in a file being dispatched with a partially parsed path
that looked like `file://path/to.jar!/path/in/jar`.

Now eglot will not attempt to parse URIs that are not file:// type at
all, instead let file-name-handler-alist entries to deal with them.
Not parsing them at all allows the file-name-handler-alist regexps to
identify them more accurately.

By also checking if eglot received a URI in eglot--path-to-uri, the
file-name-handler-alist can provide the non-file type URI back to the
lsp server, which presumably will know how to handle them since it is
also giving them out to clients.
---
 lisp/progmodes/eglot.el | 42 ++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 204121045a..b272d370ab 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -1496,29 +1496,37 @@ eglot--uri-path-allowed-chars
 (defun eglot--path-to-uri (path)
   "URIfy PATH."
   (let ((truepath (file-truename path)))
-    (concat "file://"
-            ;; Add a leading "/" for local MS Windows-style paths.
-            (if (and (eq system-type 'windows-nt)
-                     (not (file-remote-p truepath)))
-                "/")
-            (url-hexify-string
-             ;; Again watch out for trampy paths.
-             (directory-file-name (file-local-name truepath))
-             eglot--uri-path-allowed-chars))))
+    (if (url-type (url-generic-parse-url truepath))
+        ;; Path is already a URI, forward it to the lsp server untouched
+        truepath
+      (concat "file://"
+              ;; Add a leading "/" for local MS Windows-style paths.
+              (if (and (eq system-type 'windows-nt)
+                       (not (file-remote-p truepath)))
+                  "/")
+              (url-hexify-string
+               ;; Again watch out for trampy paths.
+               (directory-file-name (file-local-name truepath))
+               eglot--uri-path-allowed-chars)))))
 
 (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))))
-         ;; Remove the leading "/" for local MS Windows-style paths.
-         (normalized (if (and (not remote-prefix)
-                              (eq system-type 'windows-nt)
-                              (cl-plusp (length retval)))
-                         (substring retval 1)
-                       retval)))
-    (concat remote-prefix normalized)))
+         (url (url-generic-parse-url uri)))
+    (if (string= "file" (url-type url))
+        (let* ((retval (url-unhex-string (url-filename url)))
+               ;; Remove the leading "/" for local MS Windows-style paths.
+               (normalized (if (and (not remote-prefix)
+                                    (eq system-type 'windows-nt)
+                                    (cl-plusp (length retval)))
+                               (substring retval 1)
+                             retval)))
+          (concat remote-prefix normalized))
+      ;; Leave non-file type URIs untouched, `file-name-handler-alist'
+      ;; handlers can be used to dispatch them properly.
+      uri)))
 
 (defun eglot--snippet-expansion-fn ()
   "Compute a function to expand snippets.
-- 
2.38.0

>From 4c5dbc458ca1b13a897b6f01f69cacd9490c1ee1 Mon Sep 17 00:00:00 2001
From: dannyfreeman <[email-masked]>
Date: Thu, 3 Nov 2022 09:57:45 -0400
Subject: [PATCH 2/2] Warn when eglot receives a non-file type URI that Emacs
 can't handle

(bug#58790)

The file-name-operation being checked, any-handler, has no significant
meaning, other than that it is not one that would be suppressed by
inhibit-file-name-operation. We just want to check that a handler exists
and has the potential to handle this URI, not actually dispatch a
file-name-operation right now.
---
 lisp/progmodes/eglot.el | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index b272d370ab..f0969290d0 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -1524,6 +1524,8 @@ eglot--uri-to-path
                                (substring retval 1)
                              retval)))
           (concat remote-prefix normalized))
+      (unless (find-file-name-handler uri 'any-handler)
+        (eglot--message "Received URI with unexpected scheme: %s" uri))
       ;; Leave non-file type URIs untouched, `file-name-handler-alist'
       ;; handlers can be used to dispatch them properly.
       uri)))
-- 
2.38.0

-- 
Danny Freeman