11 March 2015

Flycheck is a great Emacs extension for on-the-fly syntax check. It is easy to configure, use, and extend. With shellcheck as the backend, it worked perfectly for my shell scripts until recently. The other day, I was not able to enable flycheck for a shell script. After some test, it is clear that flycheck only cannot handle ksh scripts. Luckily, the code of flycheck.el was very easy to follow. After digging into it, I soon figured out the root cause and a simple solution.

The root cause is that, when using shellcheck as backend, flycheck supports ksh88 while ksh are mapped to pdksh (public domain ksh). Furthermore, as the code snippet indicates, this issue occurs only on Linux boxes.

;; in flycheck.el
(defconst flycheck-shellcheck-supported-shells '(bash ksh88 sh zsh)
  "Shells supported by Shellcheck.")

;; in sh-script.el
(defcustom sh-alias-alist
  (append (if (eq system-type 'gnu/linux)
         '((csh . tcsh)
           (ksh . pdksh))) ; <----- OOPS
     ;; for the time being
     '((ksh . ksh88)
           (bash2 . bash)
       (sh5 . sh)))
  ;; ...
  )

The "fix" is quite straightforward. And, it doesn't involve any modification of flycheck. Instead, I simply customize the sh-alias-alist by putting the following lines into my Emacs init file.

(when (eq system-type 'gnu/linux)
  (setq sh-alias-alist
        '((csh . tcsh)
          (ksh . ksh88)                 ; flycheck doesn't handle pdksh
          (bash2 . bash)
          (sh5 . sh))))


blog comments powered by Disqus