Subject: Re: bug#64821: 30.0.50; write-region errs when overwriting an already open file

From: Eli Zaretskii <[email-masked]>
Date: Thu, 27 Jul 2023 22:02:45 +0300

> From: Mattias Engdegård <[email-masked]>
> Date: Thu, 27 Jul 2023 19:47:46 +0200
> Cc: [email-masked],
>  [email-masked]
> 
> 27 juli 2023 kl. 18.31 skrev Eli Zaretskii <[email-masked]>:
> 
> > Is that this line:
> > 
> >          (with-current-buffer (get-file-buffer (file-truename filename))
> >            (set-visited-file-modtime))
> 
> Right, and it's probably because ERT creates temporary files in /var/something but on macOS, /var is a symlink to /private/var. In the lines above, filename (and file-truename) is the "/private/var/..." version.
> Thus get-file-buffer won't return anything because buffer-file-name contains the non-true name, "/var/...".
> 
> I have no idea if this is the right solution (probably not) but it makes filelock-tests all pass:

Thanks, but I think we should make userlock.el robust in the face of
such applications.  How about the patch below instead?

diff --git a/lisp/userlock.el b/lisp/userlock.el
index 96de17d..92fea11 100644
--- a/lisp/userlock.el
+++ b/lisp/userlock.el
@@ -141,8 +141,10 @@ userlock--check-content-unchanged
           ;; modtime in that buffer, to cater to use case where the
           ;; file is about to be written to from some buffer that
           ;; doesn't visit any file, like a temporary buffer.
-          (with-current-buffer (get-file-buffer (file-truename filename))
-            (set-visited-file-modtime))
+          (let ((buf (get-file-buffer (file-truename filename))))
+            (when buf
+              (with-current-buffer buf
+                (set-visited-file-modtime))))
           'unchanged)))))
 
 ;;;###autoload