jwiegley / use-package

A use-package declaration for simplifying your .emacs
https://jwiegley.github.io/use-package
GNU General Public License v3.0
4.42k stars 260 forks source link

Byte-compilation doesn't work ("malformed function" and "wrong type argument" for :bind) #864

Closed dmalyuta closed 2 years ago

dmalyuta commented 4 years ago

The beginning of my ~/.emacs.d/init.el file looks like this:

;; MELPA
(require 'package)
(setq package-enable-at-startup nil)
(setq package-check-signature nil)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(add-to-list 'package-archives '("elpy" . "https://jorgenschaefer.github.io/packages/"))

;; boostrap 'use-package'
(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))

(use-package windmove
  ;; move cursor between windows
  :demand
  :bind
  (("C-<left>" . windmove-left)
   ("C-<right>" . windmove-right)
   ("C-<up>" . windmove-up)
   ("C-<down>" . windmove-down)))

... more stuff ...

I am trying to byte compile my init.el file. I execute the following in the terminal, sinde the ~/.emacs.d/ directory:

$ emacs --batch --eval '(byte-compile-file "init.el")'

The output is:

In toplevel form:
init.el:59:1:Warning: ‘("C-<left>" . windmove-left)’ is a malformed function
init.el:59:14:Warning: reference to free variable ‘windmove’
init.el:62:3:Error: Wrong type argument: sequencep, windmove-right

Basically, the very first key binding definition fails... and there is also some sort of warning about a free variable. How do I properly byte-compile the init.el file - even for the above very simple example (imagine there's nothing else to the init.el file)?

a13 commented 4 years ago

Why do you guys compile init.el while using use-package?

dmalyuta commented 4 years ago

Is one not supposed to compile init.el when using use-package? Is that functionality not possible?

a13 commented 4 years ago

of course it's possible, I'm asking of curiosity, since to my experience it doesn't speed up loading at all, so I can't imagine why one may want to do it

dmalyuta commented 4 years ago

I guess my question is more out of curiosity for why this error comes up, and whether it is a symptom of me doing something wrong in my init.el

a13 commented 4 years ago

Does

(eval-when-compile
  (require 'use-package))

help?

a13 commented 4 years ago

Works for me, at least from dired

dmalyuta commented 4 years ago

If I use the following init.el:

;; MELPA
(require 'package)
(setq package-enable-at-startup nil)
(setq package-check-signature nil)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(add-to-list 'package-archives '("elpy" . "https://jorgenschaefer.github.io/packages/"))

;; boostrap 'use-package'
(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
(eval-when-compile
  (require 'use-package))

(use-package windmove
  ;; move cursor between windows
  :demand
  :bind
  (("C-<left>" . windmove-left)
   ("C-<right>" . windmove-right)
   ("C-<up>" . windmove-up)
   ("C-<down>" . windmove-down)))

Then I get the following byte compilation error:

$ emacs --batch --eval '(byte-compile-file "init.el")'

In toplevel form:
init.el:13:13:Error: Cannot open load file: No such file or directory, use-package
conao3 commented 4 years ago

Please see @a13 comment.

Does

(eval-when-compile
  (require 'use-package))

help?

dmalyuta commented 4 years ago

I did add the suggested statement in my last reply... There is an error Cannot open load file, like I posted.

a13 commented 4 years ago

Looks like a load-path issue

dmalyuta commented 4 years ago

It seems the issue goes away if I compile from inside Emacs using M-x byte-compile-init-dir. How come the terminal version then isn't working?

rprimus commented 4 years ago

Fri Aug 28 11:13:11 BST 2020

init.el:13:13:Error: Cannot open load file: No such file or directory, use-package

As @a13 mentioned, use-package is not found in your load-path. To show this, add the following before the eval-when-compile expression:

(message "current load-path: %s" load-path)

and run emacs --batch -l init.el

You'll see that use-package is not in the load-path. To include it, (package-initialize) needs to be run. Include this in the eval-when expression:

(eval-when-compile
  (package-initialize)
  (require 'use-package))

There should be no problems now when running the batch command.

skangas commented 2 years ago

As others have explained, this is a load-path issue. If use-package was installed with package-install, things should work automatically. If using an older version, try adding (package-initialize) to your init file.