1

The following init file does not work if flycheck is not installed:

(defun install-my-packs ()
 
  (add-to-list 'package-archives  '("melpa" . "https://melpa.org/packages/"))
 
  ;; Install use-package
  (unless (package-installed-p 'use-package)
    (package-refresh-contents)
    (package-install 'use-package))
 
  ;; Config
  ;; https://jwiegley.github.io/use-package/keywords/
  (require 'use-package-ensure)
  (setq use-package-always-pin "melpa"
        use-package-always-ensure t
        use-package-compute-statistics t
        use-package-verbose t
        use-package-always-defer t)
 
  (use-package flycheck)
  ;; More use-package calls ...
  )
 
(install-my-packs)

The use-package-always-ensure default is not effective and I get the error:

Error (use-package): Cannot load flycheck

However, if I don't use the function wrapper install-my-packs, but I call straight the code in the body of this function, then use-package downloads flycheck from MELPA. The init file works, too, if I use (install-my-packs) but I individually set (use-package flycheck :ensure t) for each package to install.

For obvious reasons, I find convenient to put all the code to initialise my packages in a dedicated function. How can I obtain this?

1 Answer 1

1

use-package is a macro, so those settings you are setqing will need to have already been evaluated before your macro call is expanded. This is byte-compilation time if you're compiling the code, or load time for uncompiled code if eager macro expansion is happening.

I expect your immediate problem is arising from eager macro expansion, and that the solution will be to set those values at the top-level, outside of the function which contains the macro call.

As it is the expansion is happening without those settings in effect, as the settings aren't made until the function is called, but the macro is expanded while that function is being defined.

Try this:

(add-to-list 'package-archives  '("melpa" . "https://melpa.org/packages/"))

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

;; Config
;; https://jwiegley.github.io/use-package/keywords/
(require 'use-package-ensure)
(setq use-package-always-pin  "melpa"
      use-package-always-ensure t
      use-package-compute-statistics t
      use-package-verbose t
      use-package-always-defer t)

(defun install-my-packs ()
  (use-package flycheck)
  ;; More use-package calls ...
  )

(install-my-packs)
3
  • Is there a possibility to better control when a macro expansion should happen? Does thunk-delaywork for macros?
    – antonio
    Feb 7, 2022 at 16:33
  • No, I don't believe so. I'm a little surprised that use-package isn't expanding to code which checks those variables at run time (it's somewhat unusual for a macro to have such dynamic dependencies), but I guess in this instance it was deemed to be reasonable. I suggest that you raise a documentation bug report for this if it's not clear, though.
    – phils
    Feb 7, 2022 at 21:27
  • See also emacs.stackexchange.com/questions/17321/… (but outside of debugging I wouldn't recommend doing anything like that).
    – phils
    Feb 7, 2022 at 21:36

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.