Subject: bug#68041: 30.0.50; Tramp error with project-find-file

From: Dmitry Gutov <[email-masked]>
Date: Tue, 26 Dec 2023 18:27:17 +0200

Hi!

On 26/12/2023 15:06, Frédéric Giquel via Bug reports for GNU Emacs, the 
Swiss army knife of text editors wrote:
> Hi,
> 
> Commits from last week on project.el
> (338409c1f19e1d3a97d12edaf5ac36f1a781d08a and
> 	0d518b78d785613967fb1b375aa7932385991891) introduce problems when fileshistorycontainsTrampreference.
> 
> Here is the procedure to repoduce :
> - launch an 'emacs -Q' session
> - eval (add-to-list 'file-name-history "/ssh:inaccessible-host:~/") to
> add Tramp reference in file history
> - open a local file from a project with 'find-file'
> - do 'M-x project-find-file'
> 
> This results in a 'Tramp failed to connect' error. If the host is
> accessible, there is no error but we can see trace of an SSH connection
> (with the associated latency). It's not something we expect when we're
> working on an entirely local project.
> 
> As far as I understand the 'project--read-file-cpd-relative' function,
> a simple fix is to limit 'expand-file-name' to local files (as in the
> attached patch).

Thanks for the report.

How about the attached patch instead?

Skipping expand-file-name altogether would likely drop the respective 
entries from history as well.

I'm curious whether expand-file-name hits the remote every time when 
called, though (100 times the latency would be a wait too long). If you 
have remote entries in history, could you try and see how the 
performance is when working on that remote, too?

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 0082f12666a..dc0fd9a6efe 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1147,14 +1147,15 @@ project--read-file-cpd-relative
          (new-collection (project--file-completion-table substrings))
          (abs-cpd (expand-file-name common-parent-directory))
          (abs-cpd-length (length abs-cpd))
-         (relname (cl-letf (((symbol-value hist)
-                             (mapcan
-                              (lambda (s)
-                                (setq s (expand-file-name s))
-                                (and (string-prefix-p abs-cpd s)
-                                     (not (eq abs-cpd-length (length s)))
-                                     (list (substring s abs-cpd-length))))
-                              (symbol-value hist))))
+         (relname (cl-letf* ((non-essential t) ;Avoid new connections in Tramp.
+                             ((symbol-value hist)
+                              (mapcan
+                               (lambda (s)
+                                 (setq s (expand-file-name s))
+                                 (and (string-prefix-p abs-cpd s)
+                                      (not (eq abs-cpd-length (length s)))
+                                      (list (substring s abs-cpd-length))))
+                               (symbol-value hist))))
                     (project--completing-read-strict prompt
                                                      new-collection
                                                      predicate