flycheck eslint config file missing or not found

我是archlinux 环境, node版本6.11.3, eslint版本5.9.0

 javascript-eslint (disabled)
    - may enable:  Automatically disabled!
    - executable:  Found at $PROJECT_ROOT/node_modules/.bin/eslint
    - config file: missing or incorrect

总是提示配置文件找不到。 我尝试在项目根目录执行 eslint --print-config . 也没有任何报错。

exec-path手动设置 eslint 的路径也没用。

有哪位有类似经验的吗?使用windows和mac都没有遇到类似的问题。。。

P.S. 使用命令行是可以正常对项目里面的js文件进行eslint检查的。

(flycheck-eslint-config-exists-p) 返回的是 nil , 这个函数的实现是

(defun flycheck-eslint-config-exists-p ()
  "Whether there is a valid eslint config for the current buffer."
  (let* ((executable (flycheck-find-checker-executable 'javascript-eslint))
         (exitcode (and executable (call-process executable nil nil nil
                                                 "--print-config" "."))))
    (eq exitcode 0)))

这里面的 call-process 返回的是127, 其他平台返回的都是0。

我在命令行执行 eslint --print-config . 是正常的。。。 太奇怪了。

怎么感觉像找不到程序路径?

程序路径是对的,emacs 里面运行就不对。 升级了 emacs 27.0也不行。。。

解决了, 修改 exec-path 指向 nodeeslint 的路径 。

同时,必须添加一个 symbol link 把你安装的 node 指到 /usr/bin/node。(https://cloud.tencent.com/developer/article/1028166)

可以通过下面的脚本来检测错误:

(with-temp-buffer 
  (list (call-process "ls" nil (current-buffer) nil "-h" "-l")
        (buffer-string)))


;; we could wrap it up nicely:
(defun process-exit-code-and-output (program &rest args)
  "Run PROGRAM with ARGS and return the exit code and output in a list."
  (with-temp-buffer 
    (list (apply 'call-process program nil (current-buffer) nil args)
          (buffer-string))))

(process-exit-code-and-output "ls" "-h" "-l" "-a") ;; => (0 "-r-w-r-- 1 ...")

;; 这一步很关键,可以检查 emacs 里面运行 eslint 的错误返回码
(process-exit-code-and-output "eslint" "--print-config" ".")

(call-process "eslint" nil nil nil "--help")
(call-process "node" nil nil nil "--version")

如果报错 /usr/bin/env: node: No such file or directory 这个错误码是 127, 则需要建立软链接。。。

如果下次你的 eslint 还是报错,你可以如下步骤来 check:

  1. enable flycheck
  2. M-x flycheck-verity-setup
  3. 检测是否可以找到 eslint的可执行程序和配置文件
  4. 如果找不到 eslint 可执行程序,需要往 exec-path 里面添加 eslint 所在路径
  5. 如果配置找不到,可以试着在项目的根目录执行 eslint --print-config . (解决任何可能的错误)
  6. 如果步骤5正常,则需要check flycheck-eslint-config-exists-p 函数执行情况,如果返回 nil,则使用本文的方案。

fuuuuuuuuuuuuuuuuuuck 呀!!!浪费我几个小时。:joy:

2 个赞

2019.7.5 号更新:

如果 eslint的版本大于5会导致 eslint --print-config . 出错:

Oops! Something went wrong! :(

ESLint: 6.0.0.
ESLint couldn't find a configuration file. To set up a configuration file for this project, please run:

    eslint --init

ESLint looked for configuration files in /home/user/www and its ancestors.

Github上面有讨论:

而 flycheck内部判断 eslint 配置文件是否存在是通过 eslint --print-config . 这个语句的返回值来判断的。

(defun flycheck-eslint-config-exists-p ()
  "Whether there is a valid eslint config for the current buffer."
  (let* ((executable (flycheck-find-checker-executable 'javascript-eslint))
         (exitcode (and executable (call-process executable nil nil nil
                                                 "--print-config" "."))))
    (eq exitcode 0)))

这个时候,可以通过 npm install eslint@5 降级来解决。坑了我半个小时。。 记录一下。

1 个赞

This problem is fixed by flycheck PR 1588. So far, using the package in Melpa will be fine.

1 个赞

emacs好像不会识别项目目录下的eslintrc,而是识别了全局的,如何做到识别项目目录下的eslintrc呢?