Subject: bug#78392: 31.0.50; eglot breaks on xref-find-definitions

From: Eli Zaretskii <[email-masked]>
Date: Mon, 12 May 2025 18:16:44 +0300

> From: "[email-masked]" <[email-masked]>
> Date: Mon, 12 May 2025 06:29:15 +0000
> 
> Currently, when building from latest Emacs ToT, activating `eglot-mode`
> in any C++ file using `clangd` and attempting to call
> `xref-find-definitions` on any definition results in the following error
> being triggered:
> 
> ```
> Opening input file: Is a directory, c:/Users/sonictk/tmp
> ```
> 
> Where the `.cpp` file is in the given directory. Tried building Emacs
> using a build from a month-ish ago to find where the regression first
> occurred and it still had the issue present, even when doing `make
> distclean` and then `make bootstrap` after re-running `./configure`.
> 
> `toggle-debug-on-error` gives me:
> 
> ```
> Debugger entered--Lisp error: (file-error "Opening input file" "Is a directory" "c:/Users/sonictk/tmp")
>   eglot--xref-make-match(#("Baz" 0 3 (face font-lock-function-name-face fontified t))
> "file:///c:/Users/sonictk/tmp/test2.cpp" (:end (:character 11 :line 23) :start (:character 8 :line 23)))
>   #f(compiled-function (loc-or-loc-link) #<bytecode 0x413eb090fd3d661>)((:range (:end (:character 11 :line
> 23) :start (:character 8 :line 23)) :uri "file:///c:/Users/sonictk/tmp/test2.cpp"))
>   eglot--lsp-xrefs-for-method(:textDocument/definition)
>   #f(compiled-function (backend id) #<bytecode -0x10624bec505aa107>)(eglot "LSP identifier at point")
>   apply(#f(compiled-function (backend id) #<bytecode -0x10624bec505aa107>) eglot "LSP identifier at
> point")
>   xref-backend-definitions(eglot "LSP identifier at point")
>   #f(compiled-function () #<bytecode 0x558a9dc3551d156>)()
>   xref-show-definitions-buffer(#f(compiled-function () #<bytecode 0x558a9dc3551d156>) ((window .
> #<window 9 on test2.cpp>) (display-action) (auto-jump)))
>   xref--show-defs(#f(compiled-function () #<bytecode 0x558a9dc3551d156>) nil)
>   xref--find-definitions("LSP identifier at point" nil)
>   xref-find-definitions("LSP identifier at point")
>   funcall-interactively(xref-find-definitions "LSP identifier at point")
>   command-execute(xref-find-definitions)
> ```

Thanks.  Does the patch below give good results?

João, I think the assumption that 'retval' there necessarily starts
with a slash is because url-generic-parse-url would not necessarily
DTRT with Windows file:// URIs; it was fixed lately.  So now we need
to test for that before applying the fixup.

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index eae8e1b..6f6b05a 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -1165,7 +1165,8 @@ eglot-uri-to-path
                ;; Remove the leading "/" for local MS Windows-style paths.
                (normalized (if (and (not remote-prefix)
                                     (eq system-type 'windows-nt)
-                                    (cl-plusp (length retval)))
+                                    (cl-plusp (length retval))
+                                    (eq (aref retval 0) ?/))
                                (w32-long-file-name (substring retval 1))
                              retval)))
           (concat remote-prefix normalized))