Skip to content
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

Crash with ansible language server #15

Closed
fredericgiquel opened this issue Jan 17, 2024 · 2 comments
Closed

Crash with ansible language server #15

fredericgiquel opened this issue Jan 17, 2024 · 2 comments

Comments

@fredericgiquel
Copy link

Thanks for this program. It works well for all language servers I tested with lsp-mode, with the exception of ansible.

How to reproduce

  1. Be sure that ansible-language-server is installed and in your PATH
  2. export LSP_USE_PLISTS=true
  3. Launch Emacs with following minimal init.el
;; Initialize package sources
(require 'package)

(setq package-archives '(("gnu" . "https://elpa.gnu.org/packages/")
                         ("nongnu" . "https://elpa.nongnu.org/nongnu/")
                         ("melpa" . "https://melpa.org/packages/")))

(package-initialize)
(unless package-archive-contents
  (package-refresh-contents))

(require 'use-package)
(setq use-package-always-ensure t)

;; Install packages
(use-package lsp-mode
  :config
  (setq lsp-log-io t))

(use-package yaml-mode)

(use-package ansible
  :after lsp-mode yaml-mode
  :hook
  (yaml-mode . ansible)
  (ansible . lsp))

;; Copied from https://github.com/blahgeek/emacs-lsp-booster#configure-lsp-mode
(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
          (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)
  1. Open a yaml file and, if necessary, import project root in lsp-mode

Logs

*ansible-ls::stderr buffer:

[2024-01-17T21:06:08Z INFO  emacs_lsp_booster::app] Running server "/home/fred/.local/bin/ansible-language-server" "--stdio"
[2024-01-17T21:06:08Z INFO  emacs_lsp_booster::app] Will convert server json to bytecode! bytecode options: BytecodeOptions { object_type: Plist, null_value: Nil, false_value: Nil }
thread '<unnamed>' panicked at /github/workspace/src/app.rs:145:18:
called `Result::unwrap()` on an `Err` value: Server->client read thread failed

Caused by:
    Invalid header format
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Process ansible-ls stderr finished

Last exchange in *lsp-log: ansible-ls* buffer:

[Trace - 10:06:08 ] Received request 'workspace/configuration - (2).
Params: {
  "items": [
    {
      "scopeUri": "file:///home/fred/ansible-test/ansible.yml",
      "section": "ansible"
    }
  ]
}


[Trace - 10:06:08 ] Sending response 'workspace/configuration - (2)'. Processing request took 0ms
Params: {
  "jsonrpc": "2.0",
  "id": 2,
  "result": [
    {
      "ansible": {
        "path": "ansible",
        "useFullyQualifiedCollectionNames": true
      },
      "validation": {
        "enabled": true,
        "lint": {
          "arguments": "",
          "enabled": true,
          "path": "ansible-lint"
        }
      },
      "completion": {
        "provideRedirectModules": true,
        "provideModuleOptionAliases": true
      },
      "executionEnvironment": {
        "containerEngine": "auto",
        "enabled": false,
        "image": "quay.io/ansible/creator-ee:latest",
        "pull": {
          "policy": "missing",
          "arguments": ""
        },
        "containerOptions": "",
        "volumeMounts": []
      },
      "python": {
        "interpreterPath": "",
        "activationScript": ""
      }
    }
  ]
}
@blahgeek
Copy link
Owner

blahgeek commented Jan 18, 2024

Thanks for your report.

I can reproduce this issue in my side. Apparently the ansible language server sends unexpected content to stdout so emacs-lsp-booster cannot correctly handle it. Seems to be the same issue as ansible/ansible-language-server#21 According to the discussion, disabling the ansible-lint feature may fix this issue.

I will also try to see if emacs-lsp-booster could be more tolerant to this kind of error

@fredericgiquel
Copy link
Author

You're right. The problem comes from ansible language server that writes debug messages to stdout. This has been fixed recently (in ansible/ansible-language-server#604) but there has been no new release since.
I tested with a server built from the last commit and there is no crash anymore.

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

2 participants