Run other flycheck syntax checkers after the one defined by the LSP server

Hi!

For a long time, I have been using the following construct in my Emacs configuration to use specific flycheck checkers in addition to the default one provided by the respective LSP servers:

  ;; Add buffer local Flycheck checkers after LSP for different major modes.
  ;;
  ;; The following code was motivated by a discussion on the `lsp-mode' Discord
  ;; channel (credit goes to @yyoncho).
  (defun my-flycheck-local-checker-get (fn checker property)
    (or (alist-get property (alist-get checker my-flycheck-local-cache))
        (funcall fn checker property)))
  (defvar-local my-flycheck-local-cache nil)
  (advice-add #'flycheck-checker-get :around #'my-flycheck-local-checker-get)
  (add-hook 'lsp-managed-mode-hook
            (lambda ()
              (when (derived-mode-p 'sh-mode)
                (setq my-flycheck-local-cache '((lsp . ((next-checkers . (sh-shellcheck)))))))))
  (add-hook 'lsp-managed-mode-hook
            (lambda ()
              (when (derived-mode-p 'tex-mode)
                (setq my-flycheck-local-cache '((lsp . ((next-checkers . (tex-chktex)))))))))
  (add-hook 'lsp-managed-mode-hook
            (lambda ()
              (when (derived-mode-p 'typescript-mode)
                (setq my-flycheck-local-cache '((lsp . ((next-checkers . (javascript-eslint)))))))))

That is, when editing a BASH script, I get flycheck errors from the language server I am using, and also from sh-shellcheck.

I thought that this was a common problem to solve and that my solution is rather complex. What are your solutions to using different flycheck checkers together with LSP servers?

@dschrempf In case of python the following works well for me to define checker chains:

(after! flycheck
  (flycheck-add-next-checker 'python-flake8 '(t . python-pylint))
  (flycheck-add-next-checker 'python-pylint '(t . python-mypy)))

However, it fails with “lsp is not a valid syntax checker”, when trying to do the same for the checker provided by the LSP:

(after! flycheck
  (flycheck-add-next-checker 'lsp '(t . python-mypy)))

The error message is even already displayed by flycheck when editing config.el.

Any suggestions very welcome.

I got this running with inspiration from Org mode source block error ("lsp is not a valid syntax checker") · Issue #2594 · emacs-lsp/lsp-mode · GitHub

(use-package! lsp-diagnostics)
(after! flycheck
  (lsp-diagnostics-flycheck-enable)
  (flycheck-add-next-checker 'lsp '(t . python-mypy)))

Great that you got it to work. However, I am not sure if this does what you want, because it runs python-mypy in EVERY lsp-mode buffer (also non-Python ones). Or did I miss something?

Good point, it seems not to be a practical limitation as flycheck loads without error for an elisp buffer, however, the following is cleaner:

(use-package! lsp-diagnostics)
(after! (flycheck python)
  (lsp-diagnostics-flycheck-enable)
  (flycheck-add-next-checker 'lsp '(t . python-mypy)))

Well, I think this just adds python-mypy as next lsp checker (for all modes) when you have loaded flycheck and python.