Subject: Re: bug#69606: [PATCH] Ensure default-directory exists when generating diff

From: Philip Kaludercic <[email-masked]>
Date: Wed, 26 Feb 2025 19:25:47 +0000

Michael Albinus <[email-masked]> writes:

> Philip Kaludercic <[email-masked]> writes:
>
> Hi Philip,
>
>>> But even with the default settings of reusing the same diff buffer,
>>> why change the default directory when it exists?  Why not to check
>>> if the dir still exists like Philip proposed initially?
>>
>> I have also run into this issue when trying to save a diff buffer, and
>> it not appearing in the expected place but under /tmp/.
>
> As I said the other message, I cannot reproduce. I'm not a diff user (I
> use ediff), so I need a recipe for triggering the issue.

In my case I compared two files in Dired using `dired-diff' and then
`default-directory' in the resulting buffer is "/tmp", not the same
directory as I had open using Dired.

> However, what about the patch below?
>
> Best regards, Michael.
>
> diff --git a/lisp/vc/diff.el b/lisp/vc/diff.el
> index 875deb68724..c8a1b7c0efa 100644
> --- a/lisp/vc/diff.el
> +++ b/lisp/vc/diff.el
> @@ -187,7 +187,8 @@ diff-no-select
>  					       (prin1-to-string new))))
>                                    (list (or old-alt old)
>                                          (or new-alt new)))))
> -		     " ")))
> +		     " "))
> +	 (thisdir default-directory))
>      (with-current-buffer buf
>        (setq buffer-read-only t)
>        (buffer-disable-undo (current-buffer))
> @@ -198,14 +199,15 @@ diff-no-select
>        (setq-local revert-buffer-function
>                    (lambda (_ignore-auto _noconfirm)
>                      (diff-no-select old new switches no-async (current-buffer))))
> -      (setq default-directory temporary-file-directory)
> +      (setq default-directory thisdir)
>        (setq diff-default-directory default-directory)
>        (let ((inhibit-read-only t))
>  	(insert command "\n"))
>        (with-file-modes #o600
>          (if (and (not no-async) (fboundp 'make-process))
> -	    (let ((proc (start-process "Diff" buf shell-file-name
> -                                       shell-command-switch command)))
> +	    (let* ((default-directory temporary-file-directory)
> +                   (proc (start-process "Diff" buf shell-file-name
> +                                        shell-command-switch command)))
>  	      (set-process-filter proc #'diff-process-filter)
>                (set-process-sentinel
>                 proc (lambda (proc _msg)
> @@ -213,7 +215,8 @@ diff-no-select
>                          (diff-sentinel (process-exit-status proc)
>                                         old-alt new-alt)))))
>  	  ;; Async processes aren't available.
> -	  (let ((inhibit-read-only t))
> +	  (let* ((default-directory temporary-file-directory)
> +                 (inhibit-read-only t))
>  	    (diff-sentinel
>  	     (call-process shell-file-name nil buf nil
>  			   shell-command-switch command)

I didn't try it, but it looks like it should fix the issue.