Subject: bug#53885: Autoload package-installed-p

From: Augusto Stoffel <[email-masked]>
Date: Wed, 09 Feb 2022 15:39:34 +0100

On Tue,  8 Feb 2022 at 23:39, Corwin Brust <[email-masked]> wrote:

> Corrected reproducer:
>
> (progn
>   (setq package-user-dir "~/elpa")
>   (setq package-archives
>     '(("MELPA" .
>     "https://melpa.org/packages/")))
>   (unless (package-installed-p 'use-package)
>     (package-install 'use-package)))

By the way, there are two issues with this code snippet.  First, you
should call '(package-refresh-contents)' to make sure you have the
package list from MELPA.

Second, and more subtly, calling package install during init time
doesn't add the package to the list of selected packages, so
'use-package' would be subject to removal by 'package-autoremove'.  To
fix this, something like

    (add-hook 'after-init-hook
              (lambda () (package--update-selected-packages '(use-package) nil)))

is needed.

I think a more user-friendly 'package-ensure-installed' function is in
order here.  I believe the use-package package provides some
functionality for this, but there is no good built-in equivalent (which
is needed even by use-package users).