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

From: Philip Kaludercic <[email-masked]>
Date: Sun, 16 Feb 2025 14:07:36 +0000

Stefan Kangas <[email-masked]> writes:

> Michael Albinus <[email-masked]> writes:
>
>> Philip Kaludercic <[email-masked]> writes:
>>
>> Hi Philip,
>>
>>> The reason I was thinking about a more general solution, is that
>>> something like
>>>
>>> (let ((default-directory "/this/does/not/exists"))
>>>   (make-process :command '("true")))
>>>
>>> will always fail, even though "true" doesn't use the current working
>>> directory.  I think it would be useful to have some :fallback option for
>>> these situations, to ensure that if `default-directory' doesn't exist,
>>> any other directory should be used instead.
>>
>> This is a more general request than just make it work for "diff".
>>
>> Well, there is a reason that `default-directory' isn't set to something
>> else behind your back, if it doesn't exist. Processes can use relative
>> file names as arguments, and it is always better to fail with an error
>> message instead of doing something unexpected you even don't know about.
>
> I tend to agree with Michael that we shouldn't make our fundamentals to
> DWIMy.
>
> But this bug fix is clearly useful, though I'd also agree that we might
> as well use a temporary directory for this instead.  I'd also set the
> file mode to 600 so that we don't inadvertently leak user data to random
> places on the file system.

Temporary directories are 700 by default anyway, so I don't think we
need any additional provisions (especially seeing as if I am not
mistaken, diffing doesn't create any files), which would just bloat up
the patch.

> Philip, could you update your patch along these lines and install?

Sure, hope this is OK?  If so, I'll push it:

>From 761ee105380c60bcf410e3f3f6e15af8073b1549 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <[email-masked]>
Date: Thu, 7 Mar 2024 10:06:48 +0100
Subject: [PATCH] Ensure default-directory exists when generating diff

* lisp/vc/diff.el (diff-no-select): Fall back to a fresh temporary
directory if 'default-directory' points to an invalid path.
---
 lisp/vc/diff.el | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lisp/vc/diff.el b/lisp/vc/diff.el
index 70497a97d56..1aa4f87d1ea 100644
--- a/lisp/vc/diff.el
+++ b/lisp/vc/diff.el
@@ -188,7 +188,9 @@ diff-no-select
                                   (list (or old-alt old)
                                         (or new-alt new)))))
 		     " "))
-	 (thisdir default-directory))
+	 (thisdir (if (file-exists-p default-directory)
+                      default-directory
+                    (make-temp-file "emacs-diff" t))))
     (with-current-buffer buf
       (setq buffer-read-only t)
       (buffer-disable-undo (current-buffer))
-- 
2.47.2