>From f8b01ebdbde63d32299f7d0e3290b5b725c48652 Mon Sep 17 00:00:00 2001 From: Andrey Portnoy Date: Tue, 1 Oct 2024 18:29:57 -0400 Subject: [PATCH] Use tramp-optimize-remote-path inside tramp-set-remote-path --- lisp/tramp-sh.el | 56 +++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/lisp/tramp-sh.el b/lisp/tramp-sh.el index 9e813ecc..1eb3f1c2 100644 --- a/lisp/tramp-sh.el +++ b/lisp/tramp-sh.el @@ -4129,6 +4129,9 @@ This function expects to be in the right *tramp* buffer." (setq result (buffer-substring (point) (line-end-position))))) result))) +(defvar tramp-optimize-remote-path t + "Whether to remove duplicate and non-existing directories from remote PATH.") + ;; On hydra.nixos.org, the $PATH environment variable is too long to ;; send it. This is likely not due to PATH_MAX, but PIPE_BUF. We ;; check it, and use a temporary file in case of. See Bug#33781. @@ -4137,18 +4140,27 @@ This function expects to be in the right *tramp* buffer." I.e., for each directory in `tramp-remote-path', it is tested whether it exists and if so, it is added to the environment variable PATH." - (let ((command - (format - "PATH=%s && export PATH" - (string-join (tramp-get-remote-path vec) ":"))) - (pipe-buf (tramp-get-remote-pipe-buf vec)) - tmpfile chunk chunksize) - (tramp-message vec 5 "Setting $PATH environment variable") - (if (length< command pipe-buf) - (tramp-send-command vec command) + (let* ((should-set-remote-path ; skip setting remote path in some cases + (or tramp-optimize-remote-path + (not (equal tramp-remote-path '(tramp-own-remote-path))))) + (command + (and should-set-remote-path + (format + "PATH=%s && export PATH" + (string-join (tramp-get-remote-path vec) ":")))) + (pipe-buf (tramp-get-remote-pipe-buf vec)) + tmpfile chunk chunksize) + (cond + ((not should-set-remote-path) + nil) + ((length< command pipe-buf) + (tramp-message vec 5 "Setting $PATH environment variable") + (tramp-send-command vec command)) + (t ;; Use a temporary file. We cannot use `write-region' because ;; setting the remote path happens in the early connection ;; handshake, and not all external tools are determined yet. + (tramp-message vec 5 "Setting $PATH environment variable") (setq command (concat command "\n") tmpfile (tramp-make-tramp-temp-file vec)) (while (not (string-empty-p command)) @@ -5610,17 +5622,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)