質問

I have the problem with my emacs init.el file which drives me crazy.

my init.el sets load-path to bunch of add-ins and then calls a number of requires.

Emacs loads fine and everything works but flycheck for elisp flags the first require statement in the init.el with "can't open load file" and if I byte compile it also gives me the same error.

I created the simplest example possible to recreate the problem and tested it on both MacOs X and Ubuntu with emacs 24.3 (9.0). both tests give me the same error.

here is the example:

;; init.el --- entry point for configuration

(add-to-list 'load-path "~/.emacs.d/.")
(add-to-list 'load-path "~/.emacs.d/exec-path-from-shell")
(require 'test)
(require 'exec-path-from-shell)

;; test.el --- test lives in "~/.emacs.d"
(defvar test-var "this is test")
(provide 'test)

the error :

vagrant@elsa:~/.emacs.d$ pwd
/home/vagrant/.emacs.d
vagrant@elsa:~/.emacs.d$ sudo make
emacs --batch --eval "(byte-recompile-directory \".\" 0)"
Loading 00debian-vars...
Loading /etc/emacs/site-start.d/50autoconf.el (source)...
Checking /home/vagrant/.emacs.d...
Compiling /home/vagrant/.emacs.d/init.el...

In toplevel form:
init.el:5:1:Error: Cannot open load file: test
Checking /home/vagrant/.emacs.d/auto-save-list...
Checking /home/vagrant/.emacs.d/bin...
Checking /home/vagrant/.emacs.d/exec-path-from-shell...
Done (Total of 0 files compiled, 1 failed, 2 skipped in 2 directories)
vagrant@elsa:~/.emacs.d$

if I change the order of require statements ()

(require 'exec-path-from-shell)
(require 'test)

the error will become:

In toplevel form:
init.el:5:1:Error: Cannot open load file: exec-path-from-shell

what is going on? what am I doing wrong?

Regards, Roman

役に立ちましたか?

解決

The byte compiler does not evaluate top-level forms normally. require is a special case, but the add-to-list calls, which extend load-path, are not visible to the byte compiler.

You need to wrap them in eval-and-compile:

(eval-and-compile
  (add-to-list 'load-path "~/.emacs.d/.")
  (add-to-list 'load-path "~/.emacs.d/exec-path-from-shell"))

This forces the byte compiler to evaluate these calls, so the load-path is properly extended at compile time.

Note that you should not add ~/.emacs.d to load-path, and the byte compiler will warn you if you do.

You should probably just use package.el to install packages. It'll handle all of this automatically.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top