From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Danny Freeman via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#58790: Eglot URI parsing bug when using clojure-lsp server Date: Thu, 03 Nov 2022 13:10:32 -0400 Message-ID: <87cza40xgs.fsf@dfreeman.email> References: <8cf8ba5d-c604-b2dc-274a-7597b19fb73f@dfreeman.email> <87ilk5xq01.fsf@gmail.com> <87r0yrwfn3.fsf@gmail.com> <37716e41-5955-99f6-5204-e760a716fbf6@yandex.ru> <9bb290c8-f000-31d8-265d-b5441c33eb38@dfreeman.email> <4d50b820-7053-75eb-5b11-d3d36a02b013@dfreeman.email> <87v8nxsrq6.fsf@gmail.com> Reply-To: Danny Freeman Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="28833"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 58790@debbugs.gnu.org, felician.nemeth@gmail.com, Stefan Kangas , Dmitry Gutov To: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Nov 03 18:18:32 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oqdrL-0007FQ-FU for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 03 Nov 2022 18:18:31 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oqdrD-0008NN-S9; Thu, 03 Nov 2022 13:18:24 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oqdqw-0008Ao-Ce for bug-gnu-emacs@gnu.org; Thu, 03 Nov 2022 13:18:08 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oqdqs-0001X0-E9 for bug-gnu-emacs@gnu.org; Thu, 03 Nov 2022 13:18:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oqdqs-0000nc-8j for bug-gnu-emacs@gnu.org; Thu, 03 Nov 2022 13:18:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Danny Freeman Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 03 Nov 2022 17:18:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 58790 X-GNU-PR-Package: emacs Original-Received: via spool by 58790-submit@debbugs.gnu.org id=B58790.16674958353007 (code B ref 58790); Thu, 03 Nov 2022 17:18:02 +0000 Original-Received: (at 58790) by debbugs.gnu.org; 3 Nov 2022 17:17:15 +0000 Original-Received: from localhost ([127.0.0.1]:50315 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oqdq6-0000mP-Vc for submit@debbugs.gnu.org; Thu, 03 Nov 2022 13:17:15 -0400 Original-Received: from out0.migadu.com ([94.23.1.103]:15162) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oqdq4-0000mC-4J for 58790@debbugs.gnu.org; Thu, 03 Nov 2022 13:17:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dfreeman.email; s=key1; t=1667495830; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=6Va+BsLVL9dkF+aaeO9n53YksKLFIoe1+3pmEDOIJYU=; b=sKBNAHSCyFa6vVO882arDvFd2MeZzN/2ab+3/YLOjGV4Ij7kDjqz4rltx9MfkuF6YRBUSp nPoUggkaWkp1Kr1TQFDeGti0IndoHX1oRNcd584UavSE7Q/FkIAhWXnRZVY0o9BxRr/AUY 1EZIkwuKhc6POcl4WXQm56iUbS16Z6o= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. In-reply-to: <87v8nxsrq6.fsf@gmail.com> X-Migadu-Flow: FLOW_OUT X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: "bug-gnu-emacs" Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:246991 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Jo=C3=A3o T=C3=A1vora 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=C3=A1n also suggested that Eglot warns the user when it doesn't kn= ow > 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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Only-handle-file-type-URIs-in-eglot-explicitly.patch Content-Description: 0001-Only-handle-file-type-URIs-in-eglot-explicitly.patch >From 936034d0e72621815584680a9e75f44a4448ba9d Mon Sep 17 00:00:00 2001 From: dannyfreeman 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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0002-Warn-when-eglot-receives-a-non-file-type-URI-that-Em.patch Content-Description: 0002-Warn-when-eglot-receives-a-non-file-type-URI-that-Em.patch >From 4c5dbc458ca1b13a897b6f01f69cacd9490c1ee1 Mon Sep 17 00:00:00 2001 From: dannyfreeman 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 --=-=-= Content-Type: text/plain -- Danny Freeman --=-=-=--