Subject: Re: bug#71356: use-package doesn't load org from elpa

From: Andrea Corallo <[email-masked]>
Date: Tue, 04 Jun 2024 17:44:37 -0400

tags 71356 patch
thanks

Pedro Andres Aranda Gutierrez <[email-masked]> writes:

> A minimal init.el:
> -----
> (package-initialize)
> (package-refresh-contents)
> (use-package org
>   :ensure t
>   :pin gnu)
> -----
> Expected result would be C-h v org-version returning 9.7.2, but I see 9.6.15 (the builtin package)

I can reproduce it.

Seems the issue is in 'use-package-ensure-elpa' where we gate any
installation with "(unless (package-installed-p package)".  I think we
should progress also if we see that the package is built-in and is
actually pinned.

The attached seems to do the job for me, but I'm not 100% sure it's the
best/right fix so I'd appretiate someone else to have a look.

Thanks

  Andrea

>From 4942ef85c2db0fb7b24e87da57456be208e83605 Mon Sep 17 00:00:00 2001
From: Andrea Corallo <[email-masked]>
Date: Tue, 4 Jun 2024 23:30:07 +0200
Subject: [PATCH] Fix use-package for built-in pinned packages

* lisp/use-package/use-package-ensure.el (use-package-ensure-elpa):
Always install built-in pinned packages.
---
 lisp/use-package/use-package-ensure.el | 47 ++++++++++++++------------
 1 file changed, 26 insertions(+), 21 deletions(-)

diff --git a/lisp/use-package/use-package-ensure.el b/lisp/use-package/use-package-ensure.el
index 5f75b6b59ea..a6ed980610f 100644
--- a/lisp/use-package/use-package-ensure.el
+++ b/lisp/use-package/use-package-ensure.el
@@ -157,28 +157,33 @@ use-package-ensure-elpa
                ensure)))
       (when package
         (require 'package)
-        (when (consp package)
-          (use-package-pin-package (car package) (cdr package))
-          (setq package (car package)))
-        (unless (package-installed-p package)
-          (condition-case-unless-debug err
-              (progn
-                (when (assoc package (bound-and-true-p
-                                      package-pinned-packages))
-                  (package-read-all-archive-contents))
-                (if (assoc package package-archive-contents)
-                    (package-install package)
-                  (package-refresh-contents)
-                  (when (assoc package (bound-and-true-p
-                                        package-pinned-packages))
+        (let* ((pinned (assoc package (bound-and-true-p
+                                       package-pinned-packages)))
+               (need-upgrade (and pinned (package-built-in-p package))))
+          (when (consp package)
+            (use-package-pin-package (car package) (cdr package))
+            (setq package (car package)))
+          (when (or (not (package-installed-p package)) need-upgrade)
+            (condition-case-unless-debug err
+                (progn
+                  (when pinned
                     (package-read-all-archive-contents))
-                  (package-install package))
-                t)
-            (error
-             (display-warning 'use-package
-                              (format "Failed to install %s: %s"
-                                      name (error-message-string err))
-                              :error))))))))
+                  (if (assoc package package-archive-contents)
+                      (if need-upgrade
+                          (package-upgrade package)
+                        (package-install package))
+                    (package-refresh-contents)
+                    (when pinned
+                      (package-read-all-archive-contents))
+                    (if need-upgrade
+                        (package-upgrade package)
+                      (package-install package)))
+                  t)
+              (error
+               (display-warning 'use-package
+                                (format "Failed to install %s: %s"
+                                        name (error-message-string err))
+                                :error)))))))))
 
 ;;;###autoload
 (defun use-package-handler/:ensure (name _keyword ensure rest state)
-- 
2.34.1