1

In a package I am working on I want to define a flycheck checker only if the user has flycheck installed already. To do this, I am using the macro (with-eval-after-load "flycheck" ...) as follows:

(with-eval-after-load "flycheck"
  (flycheck-define-checker go-mod
    "A syntax checker for go.mod files."
    :command ("go" "list" "-m")
    :error-patterns
    ((error line-start (file-name) ":" line ": " (message) line-end))
    :modes go-mod-mode))

When I do this however, many flycheck errors appear. For example it says that go-mod and go-mod-mode are free variables,it's misinterpreting the (error line-start ...) part, and saying that the function flycheck-define-checker is not know to be defined.

Adding (require ...) resolves these warnings but I only want this to be required if the user has "flycheck" installed already. What can I do to suppress these errors.

1 Answer 1

2

Noe that the warnings you see are real: your code happens to work because you're lucky enough that it doesn't get byte-compiled, but if for some reason it gets byte-compiled then it won't work any more (unless flycheck was loaded already when the code got byte-compiled).

So to fix your code "right", you have two options:

  • add a (eval-when-compile (require 'flycheck)) which will fix the compilation problem but at the cost of requiring flycheck to be installed and forcing flycheck to be loaded eagerly at startup when the file is not byte-compiled (which apparently is your normal case).
  • hide the macro-call inside eval:

    (with-eval-after-load "flycheck"
      (eval
       '(flycheck-define-checker go-mod
         "A syntax checker for go.mod files."
         :command ("go" "list" "-m")
         :error-patterns
         ((error line-start (file-name) ":" line ": " (message) line-end))
         :modes go-mod-mode)))
    

Other options:

  • add a (declare-function flycheck-define-checker "flycheck" (&rest _)). This will silence the warning but will result in an incorrect .elc file (because you've lied to the byte-compiler promising that flycheck-define-checker was going to be a function whereas it turned out to be a macro).
  • convince the flycheck maintainers to offer a function that does "the same" as what flycheck-define-checker. The call would of course look a bit different, with a slightly more verbose syntax (a few more ' and/or parentheses).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.