bug#81509: 31.0.91; Calling dired on nonexistent directory changes current buffer

Stephen Berman via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <[email protected]>
Newsgroups gmane.emacs.bugs
Message-ID <[email protected]>
The following was reported on emacs-devel and I'm turning it into a bug
report.

> Date: Mon, 27 Jul 2026 12:25:11 +0530
> From: Madhu via "Emacs development discussions." <[email protected]>
> 
> 
> The current dired behaviour on master when reading a wildcard which
> doesn't exist is thus:
> 
> mkdir /tmp/a
> emacs -Q /tmp/a
> 
>   - this starts up with dired with a single window showing the empty
>     directory /tmp/a
> 
> M-x dired /tmp/a/*nonexist* RET
> 
> This results in two windows, and shows *scratch* in the top window and
> *ls error* in the bottom window. now repeat the same command:
> 
> M-x dired /tmp/a/*nonexist* RET
> 
> This switches the buffer in the top window to show `/tmp/a' again
> 
> The behaviour I expect is that if dired cannot display the directory it
> shouln't switch the buffer currently being displayed.  What do you
> think?

This behavior is a by-product of 3b7d9e37ce0c.  (BTW, the nonexistent
directory doesn't have to be represented by a wildcard in the sense of a
shell glob (e.g. *nonexist*), i.e. C-x d /tmp/a/nonexist also shows the
same behavior.  But /tmp/a/nonexist is a wildcard in the sense of
insert-directory and maybe that's what the OP meant.)  Before that
change, when attempting to visit in Dired a nonexistent directory,
insert-directory signalled an error, which short-circuited the Dired
command, so that the current buffer when the Dired command was invoked
remained current.  Since 3b7d9e37ce0c, ls errors, such as trying to
display a nonexistent directory, are now shown in a special buffer after
completing the Dired command, as you observed.  But the Dired commands
use `pop-to-buffer-same-window' and
`switch-to-buffer-other-{window,frame,tab}' and calling these with a
nonexistent directory - i.e. with nil - as argument makes another buffer
current.  I agree this is surprising behavior.  The attached patch
prevents this, i.e., the buffer that was current when the Dired command
was invoked remains current when the directory to be visited is
nonexistent.  Madhu, can you confirm?

Eli, if this patch DTRT for Madhu can it be installed in emacs-31, since
the undesirable behavior was due to changes there?

Steve Berman



In GNU Emacs 31.0.91 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.52, cairo version 1.18.4) of 2026-07-27 built on strobelfs
Repository revision: b65f8ef93d03f9d9d9d0504cd22938ad0b975e6a
Repository branch: emacs-31
Windowing system distributor 'The X.Org Foundation', version 11.0.12101024
System Description: Linux From Scratch r13.0-156

Configured using:
 'configure -C --with-native-compilation=no 'CFLAGS=-Og -g3'
 PKG_CONFIG_PATH=/opt/qt6/lib/pkgconfig'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
LCMS2 LIBSYSTEMD LIBXML2 MODULES NOTIFY INOTIFY PDUMPER PNG RSVG SECCOMP
SOUND SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS WEBP X11 XDBE XIM
XINERAMA XINPUT2 XPM XRANDR GTK3 ZLIB
(unnamed) (text/x-patch, 2.5 KB)
diff --git a/lisp/dired.el b/lisp/dired.el
index 53ec6779061..1b88f2eab36 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -1238,8 +1238,11 @@ dired
 If DIRNAME is already in a Dired buffer, that buffer is used without refresh."
   ;; Cannot use (interactive "D") because of wildcards.
   (interactive (dired-read-dir-and-switches ""))
-  (prog1 (pop-to-buffer-same-window (dired-noselect dirname switches))
-    (dired--display-ls-error)))
+  (let ((buf (dired-noselect dirname switches)))
+    (prog1 (if buf
+               (pop-to-buffer-same-window buf)
+             (current-buffer))
+    (dired--display-ls-error))))
 
 ;; This lets clicks on the menu bar invoke Dired even if some feature
 ;; remaps the Dired command to another command that does not handle this
@@ -1258,24 +1261,33 @@ dired-other-window
 the user options `split-height-threshold' and `split-width-threshold',
 when it decides whether to split the window horizontally or vertically."
   (interactive (dired-read-dir-and-switches "in other window "))
-  (prog1 (switch-to-buffer-other-window (dired-noselect dirname switches))
-    (dired--display-ls-error)))
+  (let ((buf (dired-noselect dirname switches)))
+    (prog1 (if buf
+               (switch-to-buffer-other-window buf)
+             (current-buffer))
+      (dired--display-ls-error))))
 
 ;;;###autoload (keymap-set ctl-x-5-map "d" #'dired-other-frame)
 ;;;###autoload
 (defun dired-other-frame (dirname &optional switches)
   "\"Edit\" directory DIRNAME.  Like `dired' but make a new frame."
   (interactive (dired-read-dir-and-switches "in other frame "))
-  (prog1 (switch-to-buffer-other-frame (dired-noselect dirname switches))
-    (dired--display-ls-error)))
+  (let ((buf (dired-noselect dirname switches)))
+    (prog1 (if buf
+               (switch-to-buffer-other-frame buf)
+             (current-buffer))
+      (dired--display-ls-error))))
 
 ;;;###autoload (keymap-set tab-prefix-map "d" #'dired-other-tab)
 ;;;###autoload
 (defun dired-other-tab (dirname &optional switches)
   "\"Edit\" directory DIRNAME.  Like `dired' but make a new tab."
   (interactive (dired-read-dir-and-switches "in other tab "))
-  (prog1 (switch-to-buffer-other-tab (dired-noselect dirname switches))
-    (dired--display-ls-error)))
+  (let ((buf (dired-noselect dirname switches)))
+    (prog1 (if buf
+               (switch-to-buffer-other-tab buf)
+             (current-buffer))
+        (dired--display-ls-error))))
 
 ;;;###autoload
 (defun dired-noselect (dir-or-list &optional switches)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.