emacsflycheck

Flycheck reports "Cannot open load file" despite proper "load-path" list


In my ~/.emacs.d/init.el, I have just three lines:

(push (expand-file-name "lisp" user-emacs-directory) load-path)
(require 'init-vars)
(provide 'init)

In my ~/.emacs.d/lisp/init-vars.el, I have below three lines:

(require 'dired)
(setq dired-use-ls-dired nil)
(provide 'init-vars)

Flycheck reports error at line 2 of ~/.emacs.d/init.el:

Cannot open load file: No such file or directory, init-vars

What am I missing here?

If I change the line to (require 'init-vars (expand-file-name "lisp/init-vars.el" user-emacs-directory)) then the error is gone. I don't get why load-path isn't working.


Solution

  • Flycheck does not run the code, but compiles it instead (to get the compiler's warnings). When the compiler processes the (require 'init-vars) it will try to load init-vars from the "current" load-path but since the first line was compiled rather than executed, the Emacs session where the file is compiled still has the default load-path value, with the extra ~/.emacs.d/lisp directory added to it.

    One way to fix the problem is with eval-when-compile:

    (eval-when-compile
      (push (expand-file-name "lisp" user-emacs-directory) load-path))
    

    BTW, I recommend you use add-to-list instead of push here (or else, use cl-pushnew) so that repeated execution of that code doesn't keep adding redundant copies of that directory to your load-path.