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

From: Danny Freeman <[email-masked]>
Date: Thu, 10 Nov 2022 17:22:15 -0500

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

> I agree on the two comments about the ChangeLog style entries and the
> spacing.  
>
> But when dealing with eglot.el, which I am the prime maintainer of, 
> I really prefer to see design described in the commit message where the 
> design was introduced. It plays very well with vc-region-history.
>
> So Danny, if you don't mind, please revert that part.  
>
> João

Yall all really taking me for a ride on this lol.

In reviewing the comments over and over I've made a modification to the
second patch involving the warning message. Instead of using an
arbitrary symbol I realized that the function `find-file-name-handler'
works when a nil value for the second argument (operation).

Anyways, here they are again.

>From e56e987cbf601f6caffac7edfdd580a0b7da921f Mon Sep 17 00:00:00 2001
From: dannyfreeman <[email-masked]>
Date: Thu, 3 Nov 2022 09:39:16 -0400
Subject: [PATCH 1/2] Eglot: Only handle URIs with the file:// scheme

This fixes Bug#58790

* lisp/progmodes/eglot.el (eglot--path-to-uri,
eglot--uri-to-path):

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.

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".
---
 lisp/progmodes/eglot.el | 43 +++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 17 deletions(-)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 1b1302d689..2463f68f97 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -1501,29 +1501,38 @@ 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, so 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)))
+    ;; Only attempt to parse URIs with the file scheme.
+    (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.1

>From a2b92859d0a327c843bcd677eede72c0c100821f Mon Sep 17 00:00:00 2001
From: dannyfreeman <[email-masked]>
Date: Thu, 10 Nov 2022 13:05:49 -0500
Subject: [PATCH 2/2] Eglot: warn when receiving a non-file scheme URI

* lisp/progmodes/eglot.el (eglot--uri-to-path): Write a message
when eglot encounters a non-file scheme URI that Emacs is not
capable of handling.

Note that the file-name-handler operation checked is `nil'
because we don't want to use a real one that may 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.

Addresses bug#58790
---
 lisp/progmodes/eglot.el | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 2463f68f97..1850334b84 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -1530,6 +1530,8 @@ eglot--uri-to-path
                                (substring retval 1)
                              retval)))
           (concat remote-prefix normalized))
+      (unless (find-file-name-handler uri nil)
+        (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.1

-- 
Danny Freeman