Subject: bug#77468: 30.1.50; package-quickstart file is not relocatable

From: Spencer Baugh <[email-masked]>
Date: Wed, 02 Apr 2025 17:22:22 -0400

Stefan Monnier <[email-masked]> writes:
>> Maybe package-quickstart.el could use relative names to packages in
>> package-user-dir (since it itself lives in package-user-dir), and
>> absolute names to packages outside that?
>
> Sounds OK.  Tho maybe we should check `file*-in-directory-p` (or the
> presence of "../" in the output of `file-relative-name`) rather than
> distinguishing based on `package-user-dir` vs `package-directory-list`?
>
> In any case, it sounds like a good compromise.

Makes sense.  How about this?

>From 7f465bee1605e0687f4f7713bcfeb9ac39b28355 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <[email-masked]>
Date: Wed, 2 Apr 2025 17:21:24 -0400
Subject: [PATCH] Use relative names where possible in package-quickstart.el

package-quickstart.el hardcodes many absolute file names, which
makes it break if user-emacs-directory moves, or in other
situations.  To be slightly more robust to this, use relative
file names to packages that are located in the same directory as
package-quickstart.el.

* lisp/emacs-lisp/package.el (package--quickstart-relative):
Add.
(package-quickstart-refresh): Use package--quickstart-relative
on file names.  (bug#77468)
---
 lisp/emacs-lisp/package.el | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index b9a8dacab15..1269a9c95e7 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -4591,6 +4591,14 @@ package--quickstart-maybe-refresh
     (delete-file (concat package-quickstart-file "c"))
     (delete-file package-quickstart-file)))
 
+(defun package--quickstart-relative (load-name file)
+  "Return an expression which evaluates to FILE while loading LOAD-NAME."
+  (let* ((dir (file-name-directory (expand-file-name load-name)))
+         (relname (file-relative-name file dir)))
+    (if (string-prefix-p ".." relname)
+        file
+      `(file-name-concat (file-name-directory load-file-name) ,relname))))
+
 (defun package-quickstart-refresh ()
   "(Re)Generate the `package-quickstart-file'."
   (interactive)
@@ -4605,7 +4613,8 @@ package-quickstart-refresh
         ;; aren't truncated.
         (print-length nil)
         (print-level nil)
-        (Info-directory-list '("")))
+        (Info-directory-list '(""))
+        (qs-dir (file-name-directory (expand-file-name package-quickstart-file))))
     (dolist (elt package-alist)
       (condition-case err
           (package-activate (car elt))
@@ -4622,7 +4631,7 @@ package-quickstart-refresh
                 ;; Prefer uncompiled files (and don't accept .so files).
                 (let ((load-suffixes '(".el" ".elc")))
                   (locate-library (package--autoloads-file-name pkg))))
-               (pfile (prin1-to-string file)))
+               (pfile (prin1-to-string (package--quickstart-relative package-quickstart-file file))))
           (insert "(let* ((load-file-name " pfile ")\
 \(load-true-file-name load-file-name))\n")
           (insert-file-contents file)
@@ -4638,7 +4647,10 @@ package-quickstart-refresh
                   (append ',(mapcar #'package-desc-name package--quickstart-pkgs)
                           package-activated-list)))
           (current-buffer))
-      (let ((info-dirs (butlast Info-directory-list)))
+      (let ((info-dirs
+             (mapcar (lambda (info-dir)
+                       (package--quickstart-relative package-quickstart-file info-dir))
+                     (butlast Info-directory-list))))
         (when info-dirs
           (pp `(progn (require 'info)
                       (info-initialize)
-- 
2.39.3