doomemacs icon indicating copy to clipboard operation
doomemacs copied to clipboard

`+format/region` crashes lsp server when working with js file

Open ladieman217 opened this issue 2 years ago • 10 comments

I confirm that...

  • [X] I have searched the issue tracker, documentation, FAQ, Discourse, and Google, in case this issue has already been reported/resolved.

  • [X] I have read "How to Debug Issues", and will use it to provide as much information about this issue as possible.

  • [X] The issue can be reproduced on the latest available commit of Doom.

  • [X] The issue can be reproduced on a stable release of Emacs, such as 27, 28, or 29. (Unstable versions end in .50, .60, or .9x)

Expected behavior

+format/region should behave consistently, either work or fail to work

Current behavior

+format/region failed first time then works after running +format/buffer , but crashes ts-ls lsp server

Steps to reproduce

  1. open a js file with react component in it.
  2. select a region and run +format/region
  3. it shows and: Symbol’s function definition is void: apheleia--get-formatters
  4. run +format/buffer
  5. it works
  6. run +format/region again
  7. it works, but ts-ls lsp server crashes
No Project.
Error: No Project.
    at Object.ThrowNoProject (/Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:170251:11)
    at ScriptInfo.getDefaultProject (/Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:170998:23)
    at _ProjectService.doEnsureDefaultProjectForFile (/Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:174123:36)
    at _ProjectService.ensureDefaultProjectForFile (/Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:174118:75)
    at IpcIOSession.getFileAndProjectWorker (/Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:179035:77)
    at IpcIOSession.getFileAndProject (/Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:179021:17)
    at IpcIOSession.getCodeFixes (/Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:179655:36)
    at getCodeFixes (/Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:177805:43)
    at /Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:179962:69
    at IpcIOSession.executeWithRequestId (/Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:179954:14)
    at IpcIOSession.executeCommand (/Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:179962:29)
    at IpcIOSession.onMessage (/Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:180004:51)
    at process.<anonymous> (/Users/richard/n/lib/node_modules/typescript/lib/tsserver.js:181571:14)
    at process.emit (node:events:513:28)
    at emit (node:internal/child_process:937:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:83:21) (Unknown error)
LSP :: Error from the Language Server: <semantic> TypeScript Server Error (5.0.2)

System Information

https://pastebin.com/jLKFntdQ

ladieman217 avatar Sep 17 '23 10:09 ladieman217

Can confirm that this is happening for python files as well.

sugat009 avatar Sep 19 '23 08:09 sugat009

This doesn’t appear to happen with eglot. pylsp remains alive regardless of whether black fails on a region or succeeds. Seems this bug is only present with lsp-mode.

hpfr avatar Sep 25 '23 06:09 hpfr

Yes, I'm using lsp-mode with ts-ls language server

ladieman217 avatar Sep 25 '23 15:09 ladieman217

I rely quite a lot on +format/region in my workflow.

I came up with a quick dirty solution, so I can continue to use it while a proper cleaner fix will hopefully be developed.

Needed to make sure apheleia--get-formatters gets properly loaded from the library.

;; HACK: since some upstream changes, formatting a specific region seems broken, and
;; calling `+format/region' raises: "with Symbol’s function definition is void:
;; apheleia--get-formatters", ensure to autoload the required function.
(use-package! apheleia
  :commands (apheleia--get-formatters))

This unblocked me, but I started noticing that it somehow broke my tree-sitter syntax highlighting when formatting some specific regions of code. So I added this wrapper around +format/region.

;; HACK: re the above hack, for some reason it also seems to break the tree-sitter
;; syntax highlighting, this function adds a wrapper to re-enable tree-sitter after
;; calling `+format/region', in case the highlighting was broken.
(defun my/format-region (beg end &optional arg)
  (interactive "rP")
  (+format/region beg end arg)
  (ignore-errors
    (tree-sitter--teardown)
    (turn-on-tree-sitter-mode)))

Which I call using a binding

(after! evil
  (map! :map evil-visual-state-map
        ";f" #'my/format-region))

This quick fix is probably dirty as it gets! But sharing here in case it's helpful to others!

Edit: I've only tried this to format Python files. For which I was also getting the Symbol’s function definition is void: apheleia--get-formatters error.

smallwat3r avatar Sep 26 '23 17:09 smallwat3r

I think this bug originates from this line here:

https://github.com/doomemacs/doomemacs/blob/844a82c4a0cacbb5a1aa558c88675ba1a9ee80a3/modules/editor/format/autoload/format.el#L26

where buffer local variables are copied into the new buffer. Probably, lsp related variables don't like this that much. Excluding them from the list appears to solve the issue as outlined in this rudimental patch:

iff --git a/modules/editor/format/autoload/format.el b/modules/editor/format/autoload/format.el
index 90929cb2..eb1368fa 100644
--- a/modules/editor/format/autoload/format.el
+++ b/modules/editor/format/autoload/format.el
@@ -23,7 +23,10 @@
       ;; Ensure this temp buffer seems as much like the origin buffer as
       ;; possible, in case the formatter is an elisp function, like `gofmt'.
       (cl-loop for (var . val)
-               in (cl-remove-if-not #'listp (buffer-local-variables cur-buffer))
+               in (cl-remove-if-not (lambda (variable)
+                                      (and (listp variable)
+                                           (not (string-match-p "^lsp" (symbol-name (car variable))))))
+                                    (buffer-local-variables cur-buffer))
                ;; Making enable-multibyte-characters buffer-local causes an
                ;; error.
                unless (eq var 'enable-multibyte-characters)

I didn't give much thought to the possible side effects of this patch though.

m-delfino avatar Sep 28 '23 15:09 m-delfino

seems like the apheleia not loaded? i just add this in my config.el, works well now:

(use-package! apheleia)

My doom version is: 4d072ce888577b023774460f6036abefcd0a1fa6

lroolle avatar Dec 04 '23 06:12 lroolle

I've tried your solution, it's not working on the latest doom version @lroolle

ladieman217 avatar Dec 04 '23 14:12 ladieman217

I've tried your solution, it's not working on the latest doom version @lroolle

try doom sync and doom build

lroolle avatar Dec 04 '23 14:12 lroolle

@lroolle js lsp server still crashes after running +format/region when i'm in latest doom version

ladieman217 avatar Dec 05 '23 06:12 ladieman217

I ran into this problem as well. It fails for me using feature mode. +format/region and +format-region fail when Apheleia tries to get the formatter interactively from the user. After setting that by using +format/buffer for e.g., the formatting starts to work.

raszi avatar Jun 10 '24 11:06 raszi