GNU bug report logs - #54838
28.1; Buffers are not updated when a directory is renamed in dired

Previous Next

Package: emacs;

Reported by: Nicolas Martyanoff <khaelin <at> gmail.com>

Date: Sun, 10 Apr 2022 15:26:01 UTC

Severity: normal

Found in version 28.1

Done: Eli Zaretskii <eliz <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 54838 in the body.
You can then email your comments to 54838 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#54838; Package emacs. (Sun, 10 Apr 2022 15:26:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Nicolas Martyanoff <khaelin <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 10 Apr 2022 15:26:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Nicolas Martyanoff <khaelin <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 28.1; Buffers are not updated when a directory is renamed in dired
Date: Sun, 10 Apr 2022 17:25:40 +0200
Hi,

Following an update from 27.2 to 28.1, when a directory is renamed in
dired, buffers visiting files located in this directory are not updated
to visit the file at its new location.

To reproduce the problem in emacs -Q:

- C-x C-f /tmp/dir1/file

- C-x C-s
  Answer 'y' to the prompt to create the directory. At this point the
  buffer 'file' visits /tmp/dir1/file, and the file exists on the disk.

- Run dired in /tmp.

- Rename /tmp/dir1 into /tmp/dir2, e.g. using 'R'.

- Switch to the 'file' buffer. It still visits /tmp/dir1/file even
  though the file is now located in /tmp/dir2.

-- 
Nicolas Martyanoff
http://snowsyn.net
khaelin <at> gmail.com




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#54838; Package emacs. (Sun, 10 Apr 2022 17:15:02 GMT) Full text and rfc822 format available.

Message #8 received at 54838 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Nicolas Martyanoff <khaelin <at> gmail.com>, Tassilo Horn <tsdh <at> gnu.org>
Cc: 54838 <at> debbugs.gnu.org
Subject: Re: bug#54838: 28.1;
 Buffers are not updated when a directory is renamed in dired
Date: Sun, 10 Apr 2022 20:14:33 +0300
> From: Nicolas Martyanoff <khaelin <at> gmail.com>
> Date: Sun, 10 Apr 2022 17:25:40 +0200
> 
> - C-x C-f /tmp/dir1/file
> 
> - C-x C-s
>   Answer 'y' to the prompt to create the directory. At this point the
>   buffer 'file' visits /tmp/dir1/file, and the file exists on the disk.
> 
> - Run dired in /tmp.
> 
> - Rename /tmp/dir1 into /tmp/dir2, e.g. using 'R'.
> 
> - Switch to the 'file' buffer. It still visits /tmp/dir1/file even
>   though the file is now located in /tmp/dir2.

Please try the patch below and see if it solves the problem, including
in other similar use cases.

Tassilo, part of this bug is yet another fallout from replacing
dired-in-this-tree-p with file-in-directory-p: the latter returns nil
if its second arg doesn't exist, and in this scenario we are dealing
with a directory that was already renamed to the new name.  Do you
agree with my proposed solution for that below?  I intend to install
it on the release branch, as this is a regression from Emacs 27.

Thanks.

diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
index 15f95eb..fc50cce 100644
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -1838,22 +1838,23 @@ dired-rename-file
   "Rename FILE to NEWNAME.
 Signal a `file-already-exists' error if a file NEWNAME already exists
 unless OK-IF-ALREADY-EXISTS is non-nil."
-  (dired-handle-overwrite newname)
-  (dired-maybe-create-dirs (file-name-directory newname))
-  (if (and dired-vc-rename-file
-           (vc-backend file)
-           (ignore-errors (vc-responsible-backend newname)))
-      (vc-rename-file file newname)
-    ;; error is caught in -create-files
-    (rename-file file newname ok-if-already-exists))
-  ;; Silently rename the visited file of any buffer visiting this file.
-  (and (get-file-buffer file)
-       (with-current-buffer (get-file-buffer file)
-	 (set-visited-file-name newname nil t)))
-  (dired-remove-file file)
-  ;; See if it's an inserted subdir, and rename that, too.
-  (when (file-directory-p file)
-    (dired-rename-subdir file newname)))
+  (let ((file-is-dir-p (file-directory-p file)))
+    (dired-handle-overwrite newname)
+    (dired-maybe-create-dirs (file-name-directory newname))
+    (if (and dired-vc-rename-file
+             (vc-backend file)
+             (ignore-errors (vc-responsible-backend newname)))
+        (vc-rename-file file newname)
+      ;; error is caught in -create-files
+      (rename-file file newname ok-if-already-exists))
+    ;; Silently rename the visited file of any buffer visiting this file.
+    (and (get-file-buffer file)
+         (with-current-buffer (get-file-buffer file)
+	   (set-visited-file-name newname nil t)))
+    (dired-remove-file file)
+    ;; See if it's an inserted subdir, and rename that, too.
+    (when file-is-dir-p
+      (dired-rename-subdir file newname))))
 
 (defun dired-rename-subdir (from-dir to-dir)
   (setq from-dir (file-name-as-directory from-dir)
@@ -1866,7 +1867,13 @@ dired-rename-subdir
     (while blist
       (with-current-buffer (car blist)
 	(if (and buffer-file-name
-		 (file-in-directory-p buffer-file-name expanded-from-dir))
+                 ;; If FROM-DIR was renamed/removed, fall back to
+                 ;; 'dired-in-this-tree-p', since
+                 ;; 'file-in-directory-p' will punt.
+                 (or (and (file-exists-p expanded-from-dir)
+		          (file-in-directory-p buffer-file-name
+                                               expanded-from-dir))
+                     (dired-in-this-tree-p buffer-file-name expanded-from-dir)))
 	    (let ((modflag (buffer-modified-p))
 		  (to-file (replace-regexp-in-string
 			    (concat "^" (regexp-quote from-dir))
@@ -1885,7 +1892,8 @@ dired-rename-subdir-1
     (while alist
       (setq elt (car alist)
 	    alist (cdr alist))
-      (if (file-in-directory-p (car elt) expanded-dir)
+      (if (or (file-in-directory-p (car elt) expanded-dir)
+              (dired-in-this-tree-p (car elt) expanded-dir))
 	  ;; ELT's subdir is affected by the rename
 	  (dired-rename-subdir-2 elt dir to)))
     (if (equal dir default-directory)




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#54838; Package emacs. (Sun, 10 Apr 2022 17:31:02 GMT) Full text and rfc822 format available.

Message #11 received at 54838 <at> debbugs.gnu.org (full text, mbox):

From: Tassilo Horn <tsdh <at> gnu.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: Nicolas Martyanoff <khaelin <at> gmail.com>, 54838 <at> debbugs.gnu.org
Subject: Re: bug#54838: 28.1; Buffers are not updated when a directory is
 renamed in dired
Date: Sun, 10 Apr 2022 19:19:26 +0200
Eli Zaretskii <eliz <at> gnu.org> writes:

Hi Eli,

> Tassilo, part of this bug is yet another fallout from replacing
> dired-in-this-tree-p with file-in-directory-p: the latter returns nil
> if its second arg doesn't exist, and in this scenario we are dealing
> with a directory that was already renamed to the new name.  Do you
> agree with my proposed solution for that below?

I would just revert back to using dired-in-this-tree-p and never
file-in-directory-p in those places.  I've done that for many places in
61677ac3e4685d8f81c3b90eb751d9b5e8a3732d, too, because it turned out
that the change from dired-in-this-tree-p to file-in-directory-p caused
performance problems.  The fact that dired-in-this-tree-p works for
non-existing files and dired relies on that didn't even occur to me. :-(

Bye,
Tassilo




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#54838; Package emacs. (Sun, 10 Apr 2022 17:39:01 GMT) Full text and rfc822 format available.

Message #14 received at 54838 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Tassilo Horn <tsdh <at> gnu.org>
Cc: khaelin <at> gmail.com, 54838 <at> debbugs.gnu.org
Subject: Re: bug#54838: 28.1; Buffers are not updated when a directory is
 renamed in dired
Date: Sun, 10 Apr 2022 20:38:34 +0300
> From: Tassilo Horn <tsdh <at> gnu.org>
> Cc: Nicolas Martyanoff <khaelin <at> gmail.com>, 54838 <at> debbugs.gnu.org
> Date: Sun, 10 Apr 2022 19:19:26 +0200
> 
> > Tassilo, part of this bug is yet another fallout from replacing
> > dired-in-this-tree-p with file-in-directory-p: the latter returns nil
> > if its second arg doesn't exist, and in this scenario we are dealing
> > with a directory that was already renamed to the new name.  Do you
> > agree with my proposed solution for that below?
> 
> I would just revert back to using dired-in-this-tree-p and never
> file-in-directory-p in those places.  I've done that for many places in
> 61677ac3e4685d8f81c3b90eb751d9b5e8a3732d, too, because it turned out
> that the change from dired-in-this-tree-p to file-in-directory-p caused
> performance problems.

Thanks, will do when installing the changes.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#54838; Package emacs. (Mon, 11 Apr 2022 08:00:02 GMT) Full text and rfc822 format available.

Message #17 received at 54838 <at> debbugs.gnu.org (full text, mbox):

From: Nicolas Martyanoff <khaelin <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: Nicolas Martyanoff <khaelin <at> gmail.com>, 54838 <at> debbugs.gnu.org,
 Tassilo Horn <tsdh <at> gnu.org>
Subject: Re: bug#54838: 28.1; Buffers are not updated when a directory is
 renamed in dired
Date: Mon, 11 Apr 2022 09:59:03 +0200
Hi Eli,

With your patch applied on HEAD, directory renaming seems to be working
correctly (tested with direct children and with nested directories).

Thank you so much for such a quick patch!

Regards,

-- 
Nicolas Martyanoff
http://snowsyn.net
khaelin <at> gmail.com




Reply sent to Eli Zaretskii <eliz <at> gnu.org>:
You have taken responsibility. (Mon, 11 Apr 2022 11:34:01 GMT) Full text and rfc822 format available.

Notification sent to Nicolas Martyanoff <khaelin <at> gmail.com>:
bug acknowledged by developer. (Mon, 11 Apr 2022 11:34:01 GMT) Full text and rfc822 format available.

Message #22 received at 54838-done <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Nicolas Martyanoff <khaelin <at> gmail.com>
Cc: 54838-done <at> debbugs.gnu.org, khaelin <at> gmail.com, tsdh <at> gnu.org
Subject: Re: bug#54838: 28.1; Buffers are not updated when a directory is
 renamed in dired
Date: Mon, 11 Apr 2022 14:33:18 +0300
> From: Nicolas Martyanoff <khaelin <at> gmail.com>
> Cc: Nicolas Martyanoff <khaelin <at> gmail.com>,  Tassilo Horn <tsdh <at> gnu.org>,
>   54838 <at> debbugs.gnu.org
> Date: Mon, 11 Apr 2022 09:59:03 +0200
> 
> With your patch applied on HEAD, directory renaming seems to be working
> correctly (tested with direct children and with nested directories).
> 
> Thank you so much for such a quick patch!

Thanks for testing.  I've now installed the changes on the emacs-28
branch, and they will appear in Emacs 28.2.  I'm therefore closing
this bug.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Tue, 10 May 2022 11:24:06 GMT) Full text and rfc822 format available.

This bug report was last modified 3 years and 38 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.