Subject: Re: bug#56877: 29.0.50; Built-in packages are not always package-installed-p

From: Matt Armstrong <[email-masked]>
Date: Mon, 01 Aug 2022 14:26:57 -0700

Matt Armstrong <[email-masked]> writes:

> Matt Armstrong <[email-masked]> writes:
>
>> I have a fix prepared, which I will send once i have a bug number.
>
> Now attached:

Second try.  Replaced "Foo" with a real docstring!

>From f66a0e6bf84f10d2688b2892a08e2d090e5bd6c7 Mon Sep 17 00:00:00 2001
From: Matt Armstrong <[email-masked]>
Date: Mon, 1 Aug 2022 11:18:37 -0700
Subject: [PATCH] Consider built-in packages to be installed

With this change (package-installed-p PACKAGE nil) evaluates to the
same thing before and after package initialization.

* lisp/emacs-lisp/package.el (package-installed-p): Check for built-in
packages before initialization. (bug#56877)
---
 lisp/emacs-lisp/package.el            |  5 ++++-
 test/lisp/emacs-lisp/package-tests.el | 15 +++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index df70f908da..482de52f85 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -2085,7 +2085,10 @@ package-installed-p
          package-activated-list)
     ;; We used the quickstart: make it possible to use package-installed-p
     ;; even before package is fully initialized.
-    (memq package package-activated-list))
+    (or
+     (memq package package-activated-list)
+     ;; Also check built-in packages.
+     (package-built-in-p package min-version)))
    (t
     (or
      (let ((pkg-descs (cdr (assq package (package--alist)))))
diff --git a/test/lisp/emacs-lisp/package-tests.el b/test/lisp/emacs-lisp/package-tests.el
index d7a55998c2..b903cd781b 100644
--- a/test/lisp/emacs-lisp/package-tests.el
+++ b/test/lisp/emacs-lisp/package-tests.el
@@ -638,6 +638,21 @@ package-test-update-archives/ignore-nil-entry
       (package-refresh-contents)
       (should (equal (length package-archive-contents) 2)))))
 
+(ert-deftest package-test-package-installed-p ()
+  "Test package-installed-p before and after package initialization."
+  (with-package-test ()
+    ;; Verify that `package-installed-p' evaluates true for a built-in
+    ;; package, in this case `project', before package initialization.
+    (should (not package--initialized))
+    (should (package-installed-p 'project nil))
+    (should (not (package-installed-p 'imaginary-package nil)))
+
+    ;; The results don't change after package initialization.
+    (package-initialize)
+    (should package--initialized)
+    (should (package-installed-p 'project nil))
+    (should (not (package-installed-p 'imaginary-package nil)))))
+
 (ert-deftest package-test-describe-package ()
   "Test displaying help for a package."
 
-- 
2.35.1