Subject: Re: bug#59334: 29.0.50; loading native-compiled init file sets user-init-file to .eln

From: Juanma Barranquero <[email-masked]>
Date: Fri, 18 Nov 2022 10:05:50 +0100

On Fri, Nov 18, 2022 at 9:46 AM Eli Zaretskii <[email-masked]> wrote:

> I thought about a possibility that the session loaded a .eln file, but
> then the user or some Lisp explicitly loaded the .el file by hand.
> I'm not sure in this case the hash table is updated.

That's a whole another problem, isn't it?

On one hand, it would not affect user-init-file, as it's not the usual
startup procedure. And, on the other hand, my patch sets user-init-file to
the source .el, so after reloading that file it would still have the right
value, wouldn't it?

Anyway, here's the reworked patch.

diff --git i/lisp/startup.el w/lisp/startup.el
index 70267fc857..d7d8743336 100644
--- i/lisp/startup.el
+++ w/lisp/startup.el
@@ -1064,17 +1064,25 @@ startup--load-user-init-file
             ;; If we loaded a compiled file, set `user-init-file' to
             ;; the source version if that exists.
-            (when (equal (file-name-extension user-init-file)
-                         "elc")
-              (let* ((source (file-name-sans-extension user-init-file))
-                     (alt (concat source ".el")))
-                (setq source (cond ((file-exists-p alt) alt)
-                                   ((file-exists-p source) source)
-                                   (t nil)))
-                (when source
-                  (when (file-newer-than-file-p source user-init-file)
-                    (message "Warning: %s is newer than %s"
-                             source user-init-file)
-                    (sit-for 1))
-                  (setq user-init-file source))))
+            (if (equal (file-name-extension user-init-file) "elc")
+                (let* ((source (file-name-sans-extension user-init-file))
+                       (alt (concat source ".el")))
+                  (setq source (cond ((file-exists-p alt) alt)
+                                     ((file-exists-p source) source)
+                                     (t nil)))
+                  (when source
+                    (when (file-newer-than-file-p source user-init-file)
+                      (message "Warning: %s is newer than %s"
+                               source user-init-file)
+                      (sit-for 1))
+                    (setq user-init-file source)))
+              ;; Else, perhaps the user init file was compiled
+              (when (equal (file-name-extension user-init-file) "eln")
+                (if-let (source (gethash (file-name-nondirectory
user-init-file)
+                                         comp-eln-to-el-h))
+                    ;; source exists or the .eln file would not load
+                    (setq user-init-file source)
+                  (message "Warning: unknown source file for init file %S"
+                           user-init-file)
+                  (sit-for 1))))

             (when (and load-defaults

The original code is untouched, other than changing `when' to `if'; the
else part deals with the .eln.

I've checked that gethash returns a value, but not for the file's existence
because in that case comp already complains:

2022-11-18 10:01:15+0100 Warning (comp): Cannot look up eln file as no
source file was found for d:/Home/.emacs.d/init.elc

The warning above is only for the unlikely case that user-init-file points
to an .eln but the gethash lookup returns nil.