Subject: Re: bug#65267: 30.0.50; modifying debug-ignored-errors during startup with --debug-init is broken

From: Eli Zaretskii <[email-masked]>
Date: Thu, 17 Aug 2023 12:31:48 +0300

> From: Stefan Monnier <[email-masked]>
> Cc: Eli Zaretskii <[email-masked]>,  [email-masked],
>     Noam Postavsky <[email-masked]>
> Date: Sun, 13 Aug 2023 15:47:33 -0400
> 
> [ Added Noam, since we're mentioning a problem linked to yasnippet.el.  ]
> 
> >> Yeah, it's wrong, but I'm not sure how to fix it:
> >> `--debug-init` needs to change the value of `debug-ignored-errors` while
> >> `init.el` is executed,
> >
> > FWIW, I'm sure I'm missing something, but the above is far from obvious
> > to me.  Why does --debug-init need to muck with debug-ignored-errors at
> > all?  debug-ignored-errors usually/mostly contains errors unlikely to
> > occur during startup,
> 
> "unlikely" doesn't mean they should be ignored: `debug-ignored-errors`
> is for errors which should not bring up a debugger if they occur in
> interactive use, but they're still errors, and if they occur during
> `init.el` they are definitely not ignored: they stop the processing of
> `init.el`.  So when debugging init errors, we *should* bring up the
> debugger for them.
> 
> >> Maybe we should introduce a new `inhibit-debug-ignored-errors` which we
> >> could let-bind while loading `init.el` without having to modify
> >> `debug-ignored-errors` itself?
> >
> > If ignoring debug-ignored-errors during startup is really necessary,
> > that sounds like a possibly cleaner way, indeed
> 
> ..
> 
> > (albeit introducing yet another form to ignore another form ignoring
> > errors...).
> 
> Yes, that's what's making me hesitate.

What about the minor changes below?  They do seem to yield the
expected results in the original recipe.  The idea is to detect the
situation where the init file _added_ to the value of
debug-ignored-errors, and re-add the additions after loading the init
file.

diff --git a/lisp/startup.el b/lisp/startup.el
index 43d6bf7..4d0e59b 100644
--- a/lisp/startup.el
+++ b/lisp/startup.el
@@ -1023,6 +1023,7 @@ startup--load-user-init-file
          ;; Use (startup--witness) instead of nil, so we can detect when the
          ;; init files set `debug-ignored-errors' to nil.
          (if init-file-debug '(startup--witness) debug-ignored-errors))
+        (d-i-e-standard debug-ignored-errors)
         ;; The init file might contain byte-code with embedded NULs,
         ;; which can cause problems when read back, so disable nul
         ;; byte detection.  (Bug#52554)
@@ -1111,8 +1112,16 @@ startup--load-user-init-file
 
       ;; If we can tell that the init file altered debug-on-error,
       ;; arrange to preserve the value that it set up.
-      (or (eq debug-ignored-errors d-i-e-initial)
-          (setq d-i-e-from-init-file (list debug-ignored-errors)))
+      (unless (eq debug-ignored-errors d-i-e-initial)
+        (if (memq 'startup--witness debug-ignored-errors)
+            ;; The init file wants to add errors to the standard
+            ;; value, so we need to emulate that.
+            (setq d-i-e-from-init-file
+                  (list (append d-i-e-standard
+                                (remq 'startup--witness
+                                      debug-ignored-errors))))
+          ;; The init file _replaces_ the standard value.
+          (setq d-i-e-from-init-file (list debug-ignored-errors))))
       (or (eq debug-on-error debug-on-error-initial)
           (setq debug-on-error-should-be-set t
                 debug-on-error-from-init-file debug-on-error)))