Subject: bug#48461: 28.0.50; Renaming a directory in dired doesn't update buffers visiting files within it

From: Dale Sedivec <[email-masked]>
Date: Sun, 16 May 2021 03:45:36 -0500

If you rename a directory via dired, buffers within that directory will
not also have their paths updated with the new directory.

Steps to reproduce:

1. Start "emacs -Q""

2. Evaluate the following:

       (let* ((temp-dir (make-temp-file "dired_bug" t))
              (subdir (expand-file-name "foo" temp-dir))
              (a-file (expand-file-name "zzzz" subdir)))
         (make-directory subdir)
         (find-file a-file)
         (insert "blah blah blah")
         (save-buffer)
         (dired temp-dir))

This should create a temporary directory, a subdirectory "foo" within
that temp dir, it opens a buffer for a file "zzzz" within that
subdirectory "foo", and then a dired buffer on the temp dir.  For
example, you'll now have a buffer on file
"/tmp/dired_bug-123abc/foo/zzzz" and a dired buffer on directory
"/tmp/dired_bug-123abc".

3. In the dired buffer, rename the directory "foo" to "bar"

4. Go back to the buffer on "bar" and look at its path (i.e. eval
buffer-file-name, or C-x C-w and look at the path, or just try to
modify the file and then save it)

Expected result: The buffer that was visiting ".../foo/zzzz" will now
visit ".../bar/zzzz" because ".../foo" was renamed to ".../bar"

Observed result: The buffer visiting ".../foo/zzzz" is still visiting
the old path

I think function `dired-rename-subdir' would actually update the file's
path, but when `dired-rename-file' is called to rename the directory, I
think it never calls `dired-rename-subdir' because of the condition
guarding it (dired-aux.el, end of `dired-rename-file', ca. line 1797):

    ;; See if it's an inserted subdir, and rename that, too.
    (when (file-directory-p file)
      (dired-rename-subdir file newname))

`file' here is the *old* file name, as far as I can tell.  Because the
rename has already been performed, I think `file-directory-p' will
always return nil here.  I suspect changing `file' to `newname' in that
invocation of `file-directory-p' might yield the desired result.

A little work with "git blame" says this condition was added in commit
9e5fd29bede, which says that it "[Fixes a] previous manual mis-merge of
dired-aux".  However, it looks like the lines surrounding this change
were not modified in quite a long time.  I could be misreading, though!

In GNU Emacs 28.0.50 (build 1, x86_64-apple-darwin20.4.0, NS appkit-2022.44 Version 11.3 (Build 20E232))
of 2021-04-28 built on Dale
Repository revision: e62e87d51651d8dba9e6e612004e689c23428f99
Repository branch: master
System Description:  macOS 11.3

Configured using:
'configure --without-x --with-modules --with-threads --with-xwidgets
--with-zlib --with-xml2 --with-json --with-cairo --with-gnutls
--with-xpm --with-jpeg --with-tiff --with-gif --with-png --with-rsvg
--with-native-compilation --with-ns --enable-ns-self-contained
'CFLAGS=-O2 -I/opt/local/include/gcc10' LDFLAGS=-L/opt/local/lib/gcc10'

Configured features:
ACL GLIB GNUTLS JSON LCMS2 LIBXML2 MODULES NATIVE_COMP NOTIFY KQUEUE NS
PDUMPER PNG RSVG THREADS TOOLKIT_SCROLL_BARS XIM XWIDGETS ZLIB

Important settings:
  value of $LC_COLLATE: C
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  show-paren-mode: t
  electric-pair-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny rfc822 mml mml-sec epa
derived epg epg-config gnus-util rmail rmail-loaddefs auth-source eieio
eieio-core eieio-loaddefs password-cache json map time-date mm-decode
mm-bodies mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader
sendmail comp comp-cstr warnings subr-x rx cl-seq rfc2047 rfc2045
ietf-drums mm-util mail-prsvr mail-utils dired-aux dired dired-loaddefs
vc-git diff-mode easy-mmode vc-dispatcher cl-extra shortdoc
text-property-search seq paren cl-print debug backtrace find-func
cus-start cus-load elec-pair thingatpt help-fns radix-tree help-mode
cl-macs cl-loaddefs cl-lib pcase term/screen term/xterm xterm byte-opt
gv bytecomp byte-compile cconv jka-compr iso-transl tooltip eldoc
electric uniquify ediff-hook vc-hooks lisp-float-type mwheel term/ns-win
ns-win ucs-normalize mule-util term/common-win tool-bar dnd fontset
image regexp-opt fringe tabulated-list replace newcomment text-mode
elisp-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch easymenu timer select scroll-bar mouse jit-lock
font-lock syntax font-core term/tty-colors frame minibuffer cl-generic
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite charscript charprop
case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice button loaddefs faces cus-face macroexp files
window text-properties overlay sha1 md5 base64 format env code-pages
mule custom widget hashtable-print-readable backquote threads
xwidget-internal kqueue cocoa ns lcms2 multi-tty make-network-process
nativecomp emacs)

Memory information:
((conses 16 216885 12751)
(symbols 48 9937 1)
(strings 32 29808 2866)
(string-bytes 1 983898)
(vectors 16 16676)
(vector-slots 8 315497 10417)
(floats 8 85 37)
(intervals 56 31760 0)
(buffers 992 16))