Skip to content

How to make this work with lsp-java? #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
SreenivasVRao opened this issue Mar 25, 2024 · 4 comments
Open

How to make this work with lsp-java? #19

SreenivasVRao opened this issue Mar 25, 2024 · 4 comments

Comments

@SreenivasVRao
Copy link

Many thanks for this package! Was wondering what happened to the JSON RPC improvements that the LSP folks demoed!

I'm trying to use this with lsp-java and I'm not seeing any logs indicating lsp-booster is being triggered.

@blahgeek
Copy link
Owner

I'm not seeing any logs indicating lsp-booster is being triggered.

Firstly you may check that if emacs-lsp-booster process is launched (e.g. using ps in linux). If not, then there may be some problem with the lsp-booster--advice-final-command advise and you may further debug the issue by adding some logs in lsp-stdio-connection function; If the emacs-lsp-booster process is already launched, you should be able to find some logs in the *xxx:stderr* buffer

@huangfeiyu
Copy link

For me, moving the executable emacs-lsp-booster to /usr/bin/ solved my problem.

@ldeck
Copy link

ldeck commented Mar 16, 2025

@huangfeiyu @blahgeek when using usePackage to setup various lsp-mode, lsp-java, lsp-metals, lsp-python, etc -- what would a recommended config look like?

When attempting to add it instead via a mode hook like "java-mode . my-booster"

(defun my-booster ()
            (setq lsp-use-plists t)
             (defun lsp-booster--advice-json-parse (old-fn &rest args)
            "Try to parse bytecode instead of json."
            (or
             (when (equal (following-char) ?#)
               (let ((bytecode (read (current-buffer))))
                 (when (byte-code-function-p bytecode)
                   (funcall bytecode))))
             (apply old-fn args)))
          (advice-add (if (progn (require 'json)
                                 (fboundp 'json-parse-buffer))
                          'json-parse-buffer
                        'json-read)
                      :around
                      #'lsp-booster--advice-json-parse)

          (defun lsp-booster--advice-final-command (old-fn cmd &optional test?)
            "Prepend emacs-lsp-booster command to lsp CMD."
            (let ((orig-result (funcall old-fn cmd test?)))
              (if (and (not test?)                             ;; for check lsp-server-present?
                       (not (file-remote-p default-directory)) ;; see lsp-resolve-final-command, it would add extra shell wrapper
                       lsp-use-plists
                       (not (functionp 'json-rpc-connection))  ;; native json-rpc
                       (executable-find "emacs-lsp-booster"))
                  (progn
                    (when-let ((command-from-exec-path (executable-find (car orig-result))))  ;; resolve command from exec-path (in case not found in $PATH)
                      (setcar orig-result command-from-exec-path))
                    (message "Using emacs-lsp-booster for %s!" orig-result)
                    (cons "emacs-lsp-booster" orig-result))
                orig-result)))
          (advice-add 'lsp-resolve-final-command :around #'lsp-booster--advice-final-command)
      )

I then get all these errors when starting lsp-java

Error processing message (wrong-type-argument hash-table-p (:jsonrpc "2.0" :method "window/logMessage" :params (:message "17 Mar 2025, 8:36:33 am Static Commands: []" :type 3))).
Error processing message (wrong-type-argument hash-table-p (:jsonrpc "2.0" :method "window/logMessage" :params (:message "17 Mar 2025, 8:36:33 am Non-Static Commands: [sts.java.javadocHoverLink]" :type 3))).
Error processing message (wrong-type-argument hash-table-p (:jsonrpc "2.0" :method "window/logMessage" :params (:message "17 Mar 2025, 8:36:33 am Static Commands: []" :type 3))).
Error processing message (wrong-type-argument hash-table-p (:jsonrpc "2.0" :method "window/logMessage" :params (:message "17 Mar 2025, 8:36:33 am Non-Static Commands: [sts.java.location]" :type 3))).
Error processing message (wrong-type-argument hash-table-p (:jsonrpc "2.0" :method "window/logMessage" :params (:message "17 Mar 2025, 8:36:33 am Static Commands: []" :type 3))).
Error processing message (wrong-type-argument hash-table-p (:jsonrpc "2.0" :method "window/logMessage" :params (:message "17 Mar 2025, 8:36:33 am Non-Static Commands: [sts.java.search.packages, sts.java.search.types]" :type 3))).
Error processing message (wrong-type-argument hash-table-p (:jsonrpc "2.0" :method "window/logMessage" :params (:message "17 Mar 2025, 8:36:33 am Static Commands: []" :type 3))).
Error processing message (wrong-type-argument hash-table-p (:jsonrpc "2.0" :method "window/logMessage" :params (:message "17 Mar 2025, 8:36:33 am Non-Static Commands: [sts.java.hierarchy.supertypes, sts.java.hierarchy.subtypes]" :type 3))).
Error processing message (wrong-type-argument hash-table-p (:jsonrpc "2.0" :method "window/logMessage" :params (:message "17 Mar 2025, 8:36:33 am Started org.eclipse.m2e.core 1ms" :type 3))).
Error processing message (wrong-type-argument hash-table-p (:jsonrpc "2.0" :method "window/logMessage" :params (:message "17 Mar 2025, 8:36:33 am ProjectRegistryRefreshJob finished 0ms" :type 3))).
Error processing message (wrong-type-argument hash-table-p (:jsonrpc "2.0" :method "window/logMessage" :params (:message "17 Mar 2025, 8:36:34 am Started org.eclipse.buildship.core 6ms" :type 3))).
Error processing message (wrong-type-argument hash-table-p (:jsonrpc "2.0" :method "language/status" :params (:message "Init..." :type "Starting"))).

I tried adding the following instead to lsp-mode config but as the OP mentioned, wasn't seeing a change.

lsp-mode = {
  ...
  config = ''
    (setq lsp-use-plists t)
     (defun lsp-booster--advice-json-parse (old-fn &rest args)
            "Try to parse bytecode instead of json."
            (or
             (when (equal (following-char) ?#)
               (let ((bytecode (read (current-buffer))))
                 (when (byte-code-function-p bytecode)
                   (funcall bytecode))))
             (apply old-fn args)))
          (advice-add (if (progn (require 'json)
                                 (fboundp 'json-parse-buffer))
                          'json-parse-buffer
                        'json-read)
                      :around
                      #'lsp-booster--advice-json-parse)

          (defun lsp-booster--advice-final-command (old-fn cmd &optional test?)
            "Prepend emacs-lsp-booster command to lsp CMD."
            (let ((orig-result (funcall old-fn cmd test?)))
              (if (and (not test?)                             ;; for check lsp-server-present?
                       (not (file-remote-p default-directory)) ;; see lsp-resolve-final-command, it would add extra shell wrapper
                       lsp-use-plists
                       (not (functionp 'json-rpc-connection))  ;; native json-rpc
                       (executable-find "emacs-lsp-booster"))
                  (progn
                    (when-let ((command-from-exec-path (executable-find (car orig-result))))  ;; resolve command from exec-path (in case not found in $PATH)
                      (setcar orig-result command-from-exec-path))
                    (message "Using emacs-lsp-booster for %s!" orig-result)
                    (cons "emacs-lsp-booster" orig-result))
                orig-result)))
          (advice-add 'lsp-resolve-final-command :around #'lsp-booster--advice-final-command)
    ...
  '';
};

@blahgeek
Copy link
Owner

@ldeck Based on the error message "Error processing message (wrong-type-argument hash-table-p " I'm guessing that maybe the lsp-use-plists flag did not take effect. Please refer to this page about how to set that. Note that apparently it needs to be set during byte-compilation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants