Subject: bug#63625: 29.0.90; package-install inserts package directory into load-path twice.

From: Robert Pluim <[email-masked]>
Date: Mon, 22 May 2023 18:57:13 +0200

>>>>> On Mon, 22 May 2023 18:53:47 +0300, Eli Zaretskii <[email-masked]> said:

    >> Cc: Philip Kaludercic <[email-masked]>, [email-masked],
    >> todd smith <[email-masked]>
    >> From: Robert Pluim <[email-masked]>
    >> Date: Mon, 22 May 2023 17:36:09 +0200
    >> 
    Stefan> (package--reload-previously-loaded pkg-desc))
    Stefan> (with-demoted-errors "Error loading autoloads: %s"
    Stefan> (load (package--autoloads-file-name pkg-desc) nil t))
    Stefan> -        (add-to-list 'load-path (directory-file-name pkg-dir)))
    Stefan> +        ;; FIXME: Since 2013 (commit 4fac34cee97a), the autoload files take
    Stefan> +        ;; care of changing the `load-path', so maybe it's time to
    Stefan> +        ;; remove this fallback code?
    Stefan> +        (unless (or (member (file-name-as-directory pkg-dir) load-path)
    Stefan> +                    (member (directory-file-name pkg-dir) load-path))
    Stefan> +          (add-to-list 'load-path pkg-dir)))
    Stefan> Maybe we can have that patch in emacs-29 and remove the code altogether
    Stefan> on `master`?
    >> 
    >> That seems reasonable. Eli?

    Eli> I'm a bit confused by "that patch" and stuff, and would prefer to see
    Eli> the patch for emacs-29 and another for master, please.

3 patches, 2 for emacs-29 below.

The 3rd one for master is just

diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index 2892728ebd9..28bac0401ed 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -902,7 +902,6 @@ package-activate-1
           (package--reload-previously-loaded pkg-desc))
         (with-demoted-errors "Error loading autoloads: %s"
           (load (package--autoloads-file-name pkg-desc) nil t))
-        (add-to-list 'load-path (directory-file-name pkg-dir)))
       ;; Add info node.
       (when (file-exists-p (expand-file-name "dir" pkg-dir))
         ;; FIXME: not the friendliest, but simple.

Robert
-- 

>From 713da42a9c4569093368dcc41f606fb460fcd0e1 Mon Sep 17 00:00:00 2001
From: Robert Pluim <[email-masked]>
Date: Mon, 22 May 2023 15:44:21 +0200
Subject: [PATCH 1/2] Avoid duplicate load-path entry when generating package
 autoloads
To: [email-masked]

'file-name-directory' produces a path ending in '/', so that needs to be
run through 'directory-file-name' to avoid duplicate entries in
'load-path'.  (Bug#63625)

* lisp/emacs-lisp/package.el (package-generate-autoloads): Call
'directory-file-name' on the directory of 'load-file-name'.
---
 lisp/emacs-lisp/package.el | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index c684840ab7e..3d3da7909d7 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -1110,8 +1110,12 @@ package-generate-autoloads
         ;; Add the directory that will contain the autoload file to
         ;; the load path.  We don't hard-code `pkg-dir', to avoid
         ;; issues if the package directory is moved around.
-        (or (and load-file-name (file-name-directory load-file-name))
-            (car load-path)))))
+        ;; `loaddefs-generate' has code to do this for us, but it's
+        ;; not currently exposed.  (Bug#63625)
+        (or (and load-file-name
+                 (directory-file-name
+                  (file-name-directory load-file-name)))
+             (car load-path)))))
     (let ((buf (find-buffer-visiting output-file)))
       (when buf (kill-buffer buf)))
     auto-name))
-- 
2.38.1.420.g319605f8f0

>From 983ed8b5d1f30e58be5d2e165cefefc8c70ae2f9 Mon Sep 17 00:00:00 2001
From: Stefan Monnier <[email-masked]>
Date: Mon, 22 May 2023 18:49:26 +0200
Subject: [PATCH 2/2] Avoid duplicates when adding package dirs to load-path
To: [email-masked]

* lisp/emacs-lisp/package.el (package-activate-1): Check if the path
we're about to add is already in 'load-path', since package autoload
files have been updating 'load-path' for a decade.

Do not merge to master, we're going to delete this code there.
---
 lisp/emacs-lisp/package.el | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index 3d3da7909d7..4665ef0aa8e 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -904,7 +904,12 @@ package-activate-1
           (package--reload-previously-loaded pkg-desc))
         (with-demoted-errors "Error loading autoloads: %s"
           (load (package--autoloads-file-name pkg-desc) nil t))
-        (add-to-list 'load-path (directory-file-name pkg-dir)))
+        ;; FIXME: Since 2013 (commit 4fac34cee97a), the autoload files take
+        ;; care of changing the `load-path', so maybe it's time to
+        ;; remove this fallback code?
+        (unless (or (member (file-name-as-directory pkg-dir) load-path)
+                    (member (directory-file-name pkg-dir) load-path))
+          (add-to-list 'load-path pkg-dir)))
       ;; Add info node.
       (when (file-exists-p (expand-file-name "dir" pkg-dir))
         ;; FIXME: not the friendliest, but simple.
-- 
2.38.1.420.g319605f8f0