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

From: Danny Freeman <[email-masked]>
Date: Thu, 10 Nov 2022 16:45:27 -0500

Eli Zaretskii <[email-masked]> writes:

>
> OK, then I have a few minor comments, before this can be installed:
>
> This lacks ChangeLog-style parts which specify the file(s) and
> function(s) which were changed.

Thanks for your patience, I believe I've got that corrected now. See the
attachments.

> Also, in the text above, please leave two spaces between sentences,
> per our conventions, and refill the text to be at most 63 columns.

I cut it down to 63. I don't mind either way, but thought I would
mention that the CONTRIBUTE file mentions 79 columns, which is what I
normally default to in commit messages.

>> 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.
>
> Same here.  And in this case, the text should definitely be in
> comments.

Done!!!

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

This fixes Bug#58790

* lisp/progmodes/eglot.el (eglot--path-to-uri, eglot--uri-to-path):
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 them at all.
---
 lisp/progmodes/eglot.el | 46 ++++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 17 deletions(-)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index b707a6b059..11bf0884ac 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -1500,29 +1500,41 @@ 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.
+        ;; This assumes that the server can handle the same non-file
+        ;; scheme URIs that it provides to clients.
+        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 scheme URIs untouched.
+      ;; Handlers registered with `file-name-handler-alist' can
+      ;; identify these URIs and decide how to handle them.
+      uri)))
 
 (defun eglot--snippet-expansion-fn ()
   "Compute a function to expand snippets.
-- 
2.38.1

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

Addresses bug#58790

* 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.
---
 lisp/progmodes/eglot.el | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 11bf0884ac..dfa18b885f 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -1531,6 +1531,13 @@ eglot--uri-to-path
                                (substring retval 1)
                              retval)))
           (concat remote-prefix normalized))
+      ;; `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.
+      (unless (find-file-name-handler uri 'any-handler)
+        (eglot--message "Received URI with unexpected scheme: %s" uri))
       ;; Leave non-file scheme URIs untouched.
       ;; Handlers registered with `file-name-handler-alist' can
       ;; identify these URIs and decide how to handle them.
-- 
2.38.1

Thank you,
-- 
Danny Freeman