Subject: [PATCH] bug#77136 and bug#76567: package.el: Add package--newer-with-priority-p for built-in upgrades

From: david <[email-masked]>
Date: Thu, 20 Mar 2025 12:55:42 -0400

Hello Emacs maintainers,

This patch improves built-in package upgrade checks by adding
package--newer-with-priority-p.  Combines version and priority checks:
1. New numeric version > built-in
2. Priority version > built-in (when enabled)
Replaces direct comparisons in package-menu--find-upgrades.  Uses
package-desc-status instead of array indexes.

Happy to adjust as needed for inclusion in master.

Best regards,
David D.

>From 78c9c9a49a1240926934e0f731458b4ca93b0c78 Mon Sep 17 00:00:00 2001
From: dimagid <[email-masked]>
Date: Tue, 18 Mar 2025 13:02:40 -0400
Subject: [PATCH] package.el: Add package--newer-with-priority-p for built-in
 upgrades

Detect newer available packages than built-in versions using both
version and archive priority checks. The new function:
1. Marks built-ins as upgradable only if:
   a) Available version > built-in version numerically, AND
   b) Available priority version > built-in version (when enabled)
2. Replaces direct version comparisons with centralized logic

* lisp/emacs-lisp/package.el (package--newer-with-priority-p): New
function combining numeric version check and priority validation for
built-ins.
(package-menu--find-upgrades): Use new comparison. Query status via
package-desc-status instead of array indexes.
---
 lisp/emacs-lisp/package.el | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index b9a8dacab15..9652da189c7 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -3844,6 +3844,16 @@ package-desc-priority-version
   (cons (package-desc-priority pkg-desc)
         (package-desc-version pkg-desc)))
 
+(defun package--newer-with-priority-p (old new)
+  "Return non-nil if NEW is newer than OLD, considering priority.
+Both OLD and NEW are `package-desc' objects.
+Priority is only considered if NEW is newer than OLD."
+  (let ((old-version (package-desc-version old)))
+    (and (version-list-< old-version (package-desc-version new))
+         (or (not (package--active-built-in-p old))
+             (version-list-< old-version
+                             (package-desc-priority-version new))))))
+
 (defun package-menu--find-upgrades ()
   "In Package Menu, return an alist of packages that can be upgraded.
 The alist has the same form as `package-alist', namely a list
@@ -3853,19 +3863,21 @@ package-menu--find-upgrades
     ;; Build list of installed/available packages in this buffer.
     (dolist (entry tabulated-list-entries)
       ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
-      (let ((pkg-desc (car entry))
-            (status (aref (cadr entry) 2)))
-        (cond ((member status '("installed" "dependency" "unsigned" "external" "built-in"))
+      (let ((pkg-desc (car entry)))
+        (cond ((member
+                (package-desc-status pkg-desc)
+                '("installed" "dependency" "unsigned" "external" "built-in"))
                (push pkg-desc installed))
-              ((member status '("available" "new"))
-               (setq available (package--append-to-alist pkg-desc available))))))
+              ((member (package-desc-status pkg-desc)
+                       '("available" "new"))
+               (setq available
+                     (package--append-to-alist pkg-desc available))))))
     ;; Loop through list of installed packages, finding upgrades.
     (dolist (pkg-desc installed)
       (let* ((name (package-desc-name pkg-desc))
              (avail-pkg (cadr (assq name available))))
         (and avail-pkg
-             (version-list-< (package-desc-priority-version pkg-desc)
-                             (package-desc-priority-version avail-pkg))
+             (package--newer-with-priority-p pkg-desc avail-pkg)
              (or (not (package--active-built-in-p pkg-desc))
                  package-install-upgrade-built-in)
              (push (cons name avail-pkg) upgrades))))
-- 
2.48.1