Subject: bug#74208: 31.0.50; minibuffer read-file-name-default mutates global value of default-directory incorrectly

From: Madhu <[email-masked]>
Date: Sun, 08 Dec 2024 16:19:24 +0530 (IST)

*  Michael Albinus <[email-masked]> <[email-masked]>
Wrote on Sun, 08 Dec 2024 09:46:30 +0100
>
> unhandled-file-name-directory expects, that special constructs in
> FILENAME are handled via a file name handler. Per default,
> "https://www.gnu.org/" is not covered by a file name handler, so I
> believe it is a non-valid value for default-directory.
>
> See
>
> --8<---------------cut here---------------start------------->8---
> (progn
>   (url-handler-mode)
>   (let ((default-directory "https://www.gnu.org/"))
>     (unhandled-file-name-directory default-directory)))
> => nil
> --8<---------------cut here---------------end--------------->8---
>
> Whether nil is a proper value to be returned is questionable. However,
> call-process and friends are able to handle a nil default-directory.

Thanks, something like the attached? the implementation of "turn on
url-handler-mode temporarily" looks a bit gross but i guess it can't
be avoided.  should call to turn it on be outside the
minibuffer-setup-hook? --Regards, Madhu

>From 91c22af6c317855370eb405f8daf6f56c68c4f9f Mon Sep 17 00:00:00 2001
From: Madhu <[email-masked]>
Date: Tue, 5 Nov 2024 07:19:55 +0530
Subject: [PATCH] minibuffer.el: (read-file-name-default): handle
 default-directory urls

* minibuffer.el: (read-file-name-default): bind default-directory
(in dynamic scope) instead of modifying it, protect setting against
non-local exits. Turn on url-handler-mode temporarily, so any calls to
call-process and friends do not fail because the default-directory is a
URL. (bug#24708)
---
 lisp/minibuffer.el | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index c970752ec2a..607a118dd53 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -3667,11 +3667,18 @@ read-file-name-default
                   ;; just use `default-directory', but in order to avoid
                   ;; changing `default-directory' in the current buffer,
                   ;; we don't let-bind it.
-                  (let ((dir (file-name-as-directory
-                              (expand-file-name dir))))
+                  (let* ((dir (file-name-as-directory
+                               (expand-file-name dir)))
+                         (default-directory dir)
+                         (orig-uhm url-handler-mode))
                     (minibuffer-with-setup-hook
                         (lambda ()
-                          (setq default-directory dir)
+                          ;; turn on url-handler-mode temporarily so
+                          ;; call-process and friends don't fail on on a
+                          ;; encountering a url default-directory
+                          ;; (bug#74208)
+                          (unless orig-uhm
+                            (url-handler-mode 1))
                           ;; When the first default in `minibuffer-default'
                           ;; duplicates initial input `insdef',
                           ;; reset `minibuffer-default' to nil.
@@ -3690,9 +3697,12 @@ read-file-name-default
                                      (window-buffer (minibuffer-selected-window))
 				   (read-file-name--defaults dir initial))))
 			  (set-syntax-table minibuffer-local-filename-syntax))
-                      (completing-read prompt 'read-file-name-internal
-                                       pred require-match insdef
-                                       'file-name-history default-filename)))
+                      (unwind-protect
+                          (completing-read prompt 'read-file-name-internal
+                                           pred require-match insdef
+                                           'file-name-history default-filename)
+                        (unless orig-uhm
+                          (url-handler-mode -1)))))
                 ;; If DEFAULT-FILENAME not supplied and DIR contains
                 ;; a file name, split it.
                 (let ((file (file-name-nondirectory dir))
-- 
2.46.0.27.gfa3b914457