Subject: 31.0.50; sql-mode interacts poorly with package gnu-elpa

From: Stephen Berman <[email-masked]>
Date: Sat, 21 Sep 2024 16:32:05 +0200

0. Install the GNU ELPA package gnu-elpa.
1. emacs -Q
2. M-x package-initialize
3. Visit an SQL file (one with the extension .sql, like the attached
   file test.sql, so it will be visited in sql-mode).
4. At the prompt "Install package sql-indent? (yes or no) " type "no".
5. Now the current buffer displays the SQL file and the mode line
   displays "(SQL[ANSI])", but the buffer is not fontified and the echo
   area displays "File mode specification error: (error Abort!)".

This also happens in emacs-29 and emacs-30.  Typing `M-x
font-lock-fontify-buffer' makes the buffer correctly fontified.

When I add `M-x toggle-debug-on-error' to step 2 above and repeat the
recipe, I get the attached backtrace. According to this, running
sql-mode-hook invokes sql-indent-enable, whose definition is:

(when (fboundp 'sqlind-minor-mode)
    (sqlind-minor-mode (if sql-use-indent-support +1 -1)))

Further, the activated gnu-elpa package loads the feature
sqlind-minor-mode, so the above when-clause condition is satisfied, but
since the package sql-indent was not installed, sqlind-minor-mode is not
defined, causing the file mode specification error.  It's not clear to
me from this why the SQL file is not fontified, since IIUC the mode hook
is run at the end of the mode definition, so the mode fontification code
should already have been executed; and indeed, when I instrument
sql-mode, carry out the above recipe and step through sql-mode, the SQL
file gets fontified and there is no error.  Nevertheless, without
instrumentation the above recipe reliably results in the error and
unfontified display.  The following patch prevents the error and
displays the SQL file correctly fontified:

diff --git a/lisp/progmodes/sql.el b/lisp/progmodes/sql.el
index 5273ba2bee1..ffb29075cce 100644
--- a/lisp/progmodes/sql.el
+++ b/lisp/progmodes/sql.el
@@ -734,8 +734,10 @@ sql-use-indent-support

 (defun sql-indent-enable ()
   "Enable `sqlind-minor-mode' if available and requested."
-  (when (fboundp 'sqlind-minor-mode)
-    (sqlind-minor-mode (if sql-use-indent-support +1 -1))))
+  (condition-case nil
+      (when (fboundp 'sqlind-minor-mode)
+        (sqlind-minor-mode (if sql-use-indent-support +1 -1)))
+    (error nil)))

 ;; Secure Password wallet

I haven't yet tested what happens on answering yes in step 4 of the
recipe, but assuming that then the sql-indent package is installed and
the file is visited with correct fontification, the patch shouldn't
change that.  However, if you answer no in step 4, then next time you
visit a SQL file you get prompted again, and the patch doesn't change
that either.  It may be desirable to limit the prompting, but I'm not
sure if the required changes should be in sql.el, the gnu-elpa package,
or both.

In GNU Emacs 31.0.50 (build 3, x86_64-pc-linux-gnu, GTK+ Version
 3.24.43, cairo version 1.18.2) of 2024-09-12 built on strobelfssd
Repository revision: 31e8500b061b6963708e66468fc89db1006226d7
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101013
System Description: Linux From Scratch r12.2-5-systemd

Configured using:
 'configure -C 'CFLAGS=-Og -g3''

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
LCMS2 LIBSYSTEMD LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY PDUMPER PNG
RSVG SECCOMP SOUND SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS TREE_SITTER
WEBP X11 XDBE XIM XINPUT2 XPM GTK3 ZLIB