Subject: bug#62198: [PATCH] Eglot: Send clientInfo during the initialize request

From: Felician Nemeth <[email-masked]>
Date: Wed, 15 Mar 2023 12:53:58 +0100

I originally posted this patch on github:
https://github.com/joaotavora/eglot/pull/818

João requested to resubmit it here.

To recap: clientInfo arrived in LSP 3.15.0.  LSP clients can use
clientInfo to identify themselves in the initialize request.  This is
generally useful for various debugging tasks.

>From 5fe9d054845382ce74c29bb338ffc737b0770542 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felici=C3=A1n=20N=C3=A9meth?= <[email-masked]>
Date: Wed, 15 Mar 2023 12:34:06 +0100
Subject: [PATCH] Eglot: Send clientInfo during the initialize request

clientInfo arrived in LSP 3.15.0.  LSP clients can use clientInfo to
identify themselves in the initialize request.  This is generally
useful for various debugging tasks.

* lisp/progmodes/eglot.el (eglot--version): New defconst.
(eglot--connect): Send clientInfo using eglot--version.
* test/lisp/progmodes/eglot-tests.el
(eglot-test-client-info): New test.
---
 lisp/progmodes/eglot.el            | 12 ++++++++++++
 test/lisp/progmodes/eglot-tests.el | 18 ++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 037cc87148..f89c47ac4f 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -410,6 +410,14 @@ eglot-withhold-process-id
 
 ;;; Constants
 ;;;
+(defconst eglot--version
+  (eval-when-compile
+    (when byte-compile-current-file
+      (require 'lisp-mnt)
+      (lm-version byte-compile-current-file)))
+  "The version as a string of this version of Eglot.
+It is nil if Eglot is not byte-complied.")
+
 (defconst eglot--symbol-kind-names
   `((1 . "File") (2 . "Module")
     (3 . "Namespace") (4 . "Package") (5 . "Class")
@@ -1310,6 +1318,10 @@ eglot--connect
                                         (eq (jsonrpc-process-type server)
                                             'network))
                               (emacs-pid))
+                            :clientInfo (if eglot--version
+                                            `(:name "Eglot"
+                                              :version ,eglot--version)
+                                          '(:name "Eglot"))
                             ;; Maybe turn trampy `/ssh:foo@bar:/path/to/baz.py'
                             ;; into `/path/to/baz.py', so LSP groks it.
                             :rootPath (file-local-name
diff --git a/test/lisp/progmodes/eglot-tests.el b/test/lisp/progmodes/eglot-tests.el
index b95e527c51..f17b96848f 100644
--- a/test/lisp/progmodes/eglot-tests.el
+++ b/test/lisp/progmodes/eglot-tests.el
@@ -399,6 +399,24 @@ eglot-test-auto-reconnect
           (while (process-live-p proc) (accept-process-output nil 0.5)))
         (should (not (eglot-current-server)))))))
 
+(ert-deftest eglot-test-client-info ()
+  "Check clientInfo in Eglot's initialize request."
+  (skip-unless (executable-find "pylsp"))
+  (eglot--with-fixture
+   `(("project" . (("coiso.py" . "def coiso: pass"))))
+   (with-current-buffer
+       (eglot--find-file-noselect "project/coiso.py")
+     (eglot--sniffing (:client-requests c-reqs)
+       (should (eglot--tests-connect 10))
+       (eglot--wait-for (c-reqs 10)
+           (&key _id method params &allow-other-keys)
+         (when (string= method "initialize")
+           (let* ((clientInfo (plist-get params :clientInfo))
+                  (name (plist-get clientInfo :name))
+                  (version (plist-get clientInfo :version)))
+             (should (equal name "Eglot"))
+             (should (version<= "1.8" (or version "0"))))))))))
+
 (ert-deftest eglot-test-rust-analyzer-watches-files ()
   "Start rust-analyzer.  Notify it when a critical file changes."
   (skip-unless (executable-find "rust-analyzer"))
-- 
2.30.2