Subject: bug#79236: 30.1; file-directory-p in read-file-name prevents TRAMP protocol, user, and host completions

From: Michael Albinus <[email-masked]>
Date: Sun, 17 Aug 2025 13:26:19 +0200

Eli Zaretskii <[email-masked]> writes:

Hi,

>> Michael, what do you think? Could this be fixed in Tramp's completion, 
>> or is there maybe a better way to use read-directory-name from 
>> project-prompt-project-dir?
>
> Careful: there be dragons.  We don't want Tramp to load and do its
> thing when there's no reason to expect remote files and directories
> are relevant, because that slows down completion when the user only
> wants to visit a local file or directory.  There was such a problem
> some years ago, and Michael fixed it.
>
> So whatever you do for this case, be sure not to cause regressions in
> the use cases where the user just wants to visit local files: Tramp
> should neither load nor attempt to connect to remote servers in that
> case.

Indeed, this is mined area.

I've debugged the recipe, and the problem isn't Tramp related. It seems
to be in function completion-file-name-table, which contains the
following code:

--8<---------------cut here---------------start------------->8---
              ;; Check the predicate, if necessary.
              (unless (memq pred '(nil file-exists-p))
                (let ((comp ())
                      (pred
                       (if (eq pred 'file-directory-p)
                           ;; Brute-force speed up for directory checking:
                           ;; Discard strings which don't end in a slash.
                           (lambda (s)
                             (let ((len (length s)))
                               (and (> len 0) (eq (aref s (1- len)) ?/))))
                         ;; Must do it the hard (and slow) way.
                         pred)))
                  (let ((default-directory (expand-file-name realdir)))
                    (dolist (tem all)
                      (if (funcall pred tem) (push tem comp))))
                  (setq all (nreverse comp))))
--8<---------------cut here---------------end--------------->8---

Hmmm. Every hit like "/ssh:" is discarded, when the predicate is file-directory-p.

Best regards, Michael.