Hi Michael,

The current behavior of tramp-get-remote-path is to remove duplicate and
non-existing directories from remote-path. The attached patch adds
variable tramp-optimize-remote-path, setting which to nil will disable
those optimizations. The default value is t for backward compatibility.

Sometimes it's important to preserve the exact value of PATH, even if it
contains duplicate or "invalid" entries. In my scenario I was using a
Docker image prepopulated with a build cache for a large project based
on a build system called Bazel. When I ran the container using Docker
over Tramp, tramp-get-remote-path removed a nonexisting directory from
remote-path, and so Bazel insisted on doing an expensive rebuild because
PATH had changed.

I've tested the change in that scenario. For this "PATH-conservative"
usecase I essentially needed this combination:
--8<---------------cut here---------------start------------->8---
(setq tramp-optimize-remote-path nil
      tramp-remote-path '(tramp-own-remote-path))
--8<---------------cut here---------------end--------------->8---

If you think this can go in, I will follow up with an addition to the
manual and etc/NEWS. Let me know if you'd like me to adapt the patch in
any way or make any other changes.

Thank you,
Andrey.
>From de8075a491ad65a9b76a77b672292539fa918e9b Mon Sep 17 00:00:00 2001
From: Andrey Portnoy <[email protected]>
Date: Sat, 21 Sep 2024 14:34:27 -0400
Subject: [PATCH] * tramp-sh.el (tramp-optimize-remote-path): Add it.

---
 lisp/tramp-sh.el | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/lisp/tramp-sh.el b/lisp/tramp-sh.el
index 9e813ecc..6891c3b2 100644
--- a/lisp/tramp-sh.el
+++ b/lisp/tramp-sh.el
@@ -5538,6 +5538,9 @@ raises an error."
   "Check whether REGEXP matches the connection property \"uname\"."
   (string-match-p regexp (tramp-get-connection-property vec "uname" "")))
 
+(defvar tramp-optimize-remote-path t
+  "Whether to remove duplicate and non-existing directories from remote PATH.")
+
 ;;;###tramp-autoload
 (defun tramp-get-remote-path (vec)
   "Compile list of remote directories for PATH.
@@ -5610,17 +5613,21 @@ Nonexistent directories are removed from spec."
 		   (cdr elt2)))
 	  (setq remote-path (delq 'tramp-own-remote-path remote-path)))
 
-	;; Remove double entries.
-	(setq remote-path
-	      (cl-remove-duplicates
-	       remote-path :test #'string-equal :from-end t))
-
-	;; Remove non-existing directories.
-	(let (remote-file-name-inhibit-cache)
-	  (tramp-bundle-read-file-names vec remote-path)
-	  (cl-remove-if
-	   (lambda (x) (not (tramp-get-file-property vec x "file-directory-p")))
-	   remote-path))))))
+	(when tramp-optimize-remote-path
+	  ;; Remove double entries.
+	  (setq remote-path
+		(cl-remove-duplicates
+		 remote-path :test #'string-equal :from-end t))
+
+	  ;; Remove non-existing directories.
+	  (let (remote-file-name-inhibit-cache)
+	    (tramp-bundle-read-file-names vec remote-path)
+	    (setq remote-path
+		  (cl-remove-if
+		   (lambda (x)
+		     (not (tramp-get-file-property vec x "file-directory-p")))
+		   remote-path))))
+	remote-path))))
 
 ;; The PIPE_BUF in POSIX [1] can be as low as 512 [2]. Here are the values
 ;; on various platforms:
-- 
2.39.5 (Apple Git-154)

Reply via email to