joaotavora / eglot

A client for Language Server Protocol servers
GNU General Public License v3.0
2.25k stars 200 forks source link

M-x eglot-format-buffer > [eglot] Server can't format! #1150

Closed pablos-here closed 1 year ago

pablos-here commented 1 year ago

Hi,

I'm learning about LSPs and eglot. Very wet behind the ears. :)

In the two languages I've tested (python and bash), I'm getting the titled error when trying to format the buffer.

The details below are for bash as I figured it has the least requirements.

Man, I hope I haven't made some boneheaded mistake ... hold my beer, I'm pressing Submit new issue

High-level

Recipe

  1. Prepare ~/.emacs.d, init.el and create the test script per below.
  2. Install the bash-language-server
  3. In a shell, run \emacs -Q -l init.el
  4. M-x eglot-format-buffer generates [eglot] Server can't format!

Details

~/.emacs.d and init.el Start with an empty` ~/.emacs.d` and create the following `init.el` file: ```lisp ;;; minimal-ess-eglot.el -*- lexical-binding: t; -*- (require 'package) (setq package-list '(eglot)) (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/")) (package-initialize) (unless package-archive-contents (package-refresh-contents)) (dolist (package package-list) (unless (package-installed-p package) (package-install package))) (add-hook 'sh-mode-hook #'eglot-ensure) ```
Test script Create ` /var/tmp/who_me` with: ```bash #!/bin/bash echo "hi mom" exit 0 ```
M-x eglot-events-buffer (trimmed) ``` .... trimmed .... [client-request] (id:6) Mon Jan 9 09:38:00 2023: (:jsonrpc "2.0" :id 6 :method "textDocument/hover" :params (:textDocument (:uri "file:///var/tmp/who_me") :position (:line 5 :character 0))) [client-request] (id:7) Mon Jan 9 09:38:00 2023: (:jsonrpc "2.0" :id 7 :method "textDocument/documentHighlight" :params (:textDocument (:uri "file:///var/tmp/who_me") :position (:line 5 :character 0))) [server-notification] Mon Jan 9 09:38:00 2023: (:jsonrpc "2.0" :method "window/logMessage" :params (:type 4 :message "onHover 5:0 word=null")) [server-reply] (id:6) Mon Jan 9 09:38:00 2023: (:jsonrpc "2.0" :id 6 :result nil) [server-notification] Mon Jan 9 09:38:00 2023: (:jsonrpc "2.0" :method "window/logMessage" :params (:type 4 :message "onDocumentHighlight 5:0 word=null")) [server-reply] (id:7) Mon Jan 9 09:38:00 2023: (:jsonrpc "2.0" :id 7 :result []) [client-request] (id:8) Mon Jan 9 09:38:38 2023: (:jsonrpc "2.0" :id 8 :method "textDocument/hover" :params (:textDocument (:uri "file:///var/tmp/who_me") :position (:line 5 :character 0))) [client-request] (id:9) Mon Jan 9 09:38:38 2023: (:jsonrpc "2.0" :id 9 :method "textDocument/documentHighlight" :params (:textDocument (:uri "file:///var/tmp/who_me") :position (:line 5 :character 0))) [server-notification] Mon Jan 9 09:38:38 2023: (:jsonrpc "2.0" :method "window/logMessage" :params (:type 4 :message "onHover 5:0 word=null")) [server-reply] (id:8) Mon Jan 9 09:38:38 2023: (:jsonrpc "2.0" :id 8 :result nil) [server-notification] Mon Jan 9 09:38:38 2023: (:jsonrpc "2.0" :method "window/logMessage" :params (:type 4 :message "onDocumentHighlight 5:0 word=null")) [server-reply] (id:9) Mon Jan 9 09:38:38 2023: (:jsonrpc "2.0" :id 9 :result []) ```
M-x toggle-debug-on-error ``` Debugger entered--Lisp error: (error "[eglot] Server can't format!") signal(error ("[eglot] Server can't format!")) error("[eglot] %s" "Server can't format!") eglot--error("Server can't format!") eglot-format(nil nil) eglot-format-buffer() funcall-interactively(eglot-format-buffer) call-interactively(eglot-format-buffer record nil) command-execute(eglot-format-buffer record) execute-extended-command(nil "eglot-format-buffer" "eglot-format-b") funcall-interactively(execute-extended-command nil "eglot-format-buffer" "eglot-format-b") call-interactively(execute-extended-command nil nil) command-execute(execute-extended-command) ```
M-x eglot-stderr-buffer No buffer was displayed but the following message was produced in the mini-buffer: ``` eglot--current-server-or-lose: jsonrpc-error: "No current JSON-RPC connection", (jsonrpc-error-code . 32603), (jsonrpc-error-message . "No current JSON-RPC connection") ```
6b6279 commented 1 year ago

I took a look at https://github.com/bash-lsp/bash-language-server/blob/main/server/src/server.ts and it seems like bash-language-server does not offer any facilities for auto-formatting source files. eglot should throw that error if (eglot--server-capable cap) returns false — so I am assuming that the language server simply doesn’t support auto-formatting.

pablos-here commented 1 year ago

Hi @6b6279 ,

Thank you for taking the time to do the analysis and provide an explanation.

Please let me know if you'd like me to close this issue or keep it open for the error-checking implementation.

6b6279 commented 1 year ago

eglot seems to work as intended: It recognizes that the current language server does not support auto-formatting of source files and informs the user that the called function has failed. I would say this is a wontfix. If you think eglot should handle this differently, I suggest creating a bug report on the official Emacs bug tracker.

There is already an issue over at the language server repository regarding the missing auto-format feature: bash-lsp/bash-language-server#320

pablos-here commented 1 year ago

Sounds good!