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

Flycheck fails in an indirect region buffer #2011

Open
4 of 7 tasks
bvraghav opened this issue Mar 16, 2023 · 2 comments
Open
4 of 7 tasks

Flycheck fails in an indirect region buffer #2011

bvraghav opened this issue Mar 16, 2023 · 2 comments

Comments

@bvraghav
Copy link

bvraghav commented Mar 16, 2023

Thank you for taking the time to report an issue and improve Flycheck. This template is for actual bugs you observed. If you have trouble setting up Flycheck, or if you have a question, please use the relevant issue template instead.

Checklist

  • I have checked existing issues for potential duplicates before creating this one.
  • I have read the Troubleshooting guide.

Bug description

flycheck-eslint fails in an edit-indirect setting, maybe since there's no filename associated.

My guess is that because flycheck-eslint command is configured with

  :command ("eslint" "--format=json"
            (option-list "--rulesdir" flycheck-eslint-rules-directories)
            (eval flycheck-eslint-args)
            "--stdin" "--stdin-filename" source-original)

, the part where "--stdin-filename" source-original is hard-coded, source-original evaluates to nil in case of an indirect buffer causing the eslint executable to fail.

Steps to reproduce

In file ${PROJ}/tmp/foo.js flycheck-verify-setup gives

Syntax checkers for buffer foo.js in js2-mode:

First checker to run:

  javascript-eslint
    - may enable:  yes
    - executable:  Found at /XXXXXXXX/.config/nvm/versions/node/v18.14.2/bin/eslint
    - config file: found

In file ${PROJ}/tmp/foo.org, flycheck-verify-setup gives

Syntax checkers for buffer *Org Src foo.org[ js ]* in js2-mode:

First checker to run:

  javascript-eslint
    - may enable:  yes
    - executable:  Found at /XXXXXXXX/.config/nvm/versions/node/v18.14.2/bin/eslint
    - config file: found

But, at cursor position [I]

#+begin_src js
[I]
#+end_src

do C-c ' to edit indirect-ly as js buffer, and
flycheck fails with this this message

Suspicious state from syntax checker javascript-eslint: Flycheck checker javascript-eslint returned 2, but its output contained no errors: 
Oops! Something went wrong! :(

ESLint: 8.36.0

Error: 'options.filePath' must be a non-empty string or undefined
    at ESLint.lintText (/XXXXXXXX/.config/nvm/versions/node/v18.14.2/lib/node_modules/eslint/lib/eslint/eslint.js:583:19)
    at Object.execute (/XXXXXXXX/.config/nvm/versions/node/v18.14.2/lib/node_modules/eslint/lib/cli.js:412:36)
    at async main (/XXXXXXXX/.config/nvm/versions/node/v18.14.2/lib/node_modules/eslint/bin/eslint.js:135:24)

Try installing a more recent version of javascript-eslint, and please open a bug report if the issue persists in the latest release.  Thanks!

Expected behavior

I don't know for sure, but I guess that flycheck should send the buffer-contents as stdin, because there's no associated (buffer-file-name)

Screenshots

If applicable, add screenshots to help explain your problem.

System configuration

Syntax checkers for buffer *Org Src foo.org[ js ]* in js2-mode:

First checker to run:

  javascript-eslint
    - may enable:  yes
    - executable:  Found at /XXXXXXXXX/.config/nvm/versions/node/v18.14.2/bin/eslint
    - config file: found

Checkers that are compatible with this mode, but will not run until properly configured:

  javascript-jshint (automatically disabled) reset
    - may enable:         no
    - executable:         Not found
    - configuration file: Not found

  javascript-standard (automatically disabled) reset
    - may enable: no
    - executable: Not found

  javascript-tide
    - may enable:        yes
    - may run:           nil
    - Typescript server: not running
    - Tide mode:         disabled

Flycheck Mode is enabled.  Use C-u C-c ! x to enable
disabled checkers.

--------------------

Flycheck version: 33snapshot (package: 20230306.414)
Emacs version:    28.2
System:           x86_64-pc-linux-gnu
Window system:    x

Emacs configuration:

  • Plain Emacs / Custom configuration
  • Spacemacs
  • Doom Emacs
  • Other shared configuration

Additional notes

The difference in invoked flycheck-command

I had tried to get to the root cause using this step

(setq-local flycheck-command-wrapper-function (lambda (lst) (message "%s" lst) lst))
(/xxxxx/.config/nvm/versions/node/v18.14.2/bin/eslint --format=json --fix-dry-run --stdin --stdin-filename /home/rbv23/webr/vue-scratch/tmp/foo.js)

VS

(/xxxxx/.config/nvm/versions/node/v18.14.2/bin/eslint --format=json --stdin --stdin-filename )
@bvraghav
Copy link
Author

I was able to cirvumvent the problem by customising the checker to use source instead of source-original

:command ("eslint" ;; ... 
          "--stdin" "--stdin-filename" source)

My complete customisation looks as follows. For some reason I ended up hard-coding the find-working-directory routine, which may not be necessary.

(defun bvr-flycheck-eslint--find-working-directory (_checker)
  "Look for a working directory to run ESLint CHECKER in.

This will be the directory that contains the `node_modules'
directory.  If no such directory is found in the directory
hierarchy, it looks first for `.eslintignore' and then for
`.eslintrc' files to detect the project root."
  (let* ((file (or (buffer-file-name) default-directory)))
    (when file
      (or (locate-dominating-file file "node_modules")
          (locate-dominating-file file ".eslintignore")
          (locate-dominating-file file ".eslintrc")
          (locate-dominating-file file ".eslintrc.js")
          (locate-dominating-file file ".eslintrc.json")
          (locate-dominating-file file ".eslintrc.yaml")
          (locate-dominating-file file ".eslintrc.yml")))))



(flycheck-define-checker bvr-javascript-eslint
  "A Javascript syntax and style checker using eslint.

See URL `https://eslint.org/'."
  :command ("eslint" "--format=json"
            (option-list "--rulesdir" flycheck-eslint-rules-directories)
            (eval flycheck-eslint-args)
            "--stdin" "--stdin-filename" source)
  :standard-input t
  :error-parser flycheck-parse-eslint
  :enabled (lambda () (flycheck-eslint-config-exists-p))
  :modes (js-mode js-jsx-mode js2-mode js2-jsx-mode js3-mode rjsx-mode
                  typescript-mode js-ts-mode typescript-ts-mode tsx-ts-mode)
  :working-directory bvr-flycheck-eslint--find-working-directory
  :verify
  (lambda (_)
    (let* ((default-directory
             (flycheck-compute-working-directory 'bvr-javascript-eslint))
           (have-config (flycheck-eslint-config-exists-p)))
      (list
       (flycheck-verification-result-new
        :label "config file"
        :message (if have-config "found" "missing or incorrect")
        :face (if have-config 'success '(bold error))))))
  :error-explainer
  (lambda (err)
    (let ((error-code (flycheck-error-id err))
          (url "https://eslint.org/docs/rules/%s"))
      (and error-code
           ;; skip non-builtin rules
           (not ;; `seq-contains-p' is only in seq >= 2.21
            (with-no-warnings (seq-contains error-code ?/)))
           `(url . ,(format url error-code))))))


(add-to-list 'flycheck-checkers 'bvr-javascript-eslint)

bvraghav added a commit to bvraghav/dotelisp that referenced this issue Mar 16, 2023
This code is merely an adaptation for flycheck
eslint to use `source' instead of `source-original'
as the argument for commandline.

For more details, ref:
flycheck/flycheck#2011
@bvraghav
Copy link
Author

For posterity, I have created this simple workaround el.

https://github.com/bvraghav/dotelisp/blob/master/bvr-flycheck-eslint.el

And I leave it to the good judgement of maintainers to close this issue.

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

No branches or pull requests

1 participant