Subject: bug#65551: 29.1; Eshell on MS-Windows using plink: 'plink' is not recognized as an internal or external command...

From: Jim Porter <[email-masked]>
Date: Mon, 28 Aug 2023 09:47:47 -0700

On 8/28/2023 9:29 AM, Jim Porter wrote:
> On 8/28/2023 3:27 AM, Michael Albinus wrote:
>> I've applied the following sledge-hammer patch, which cures the problem
>> for me. But I'm pretty sure there are better ways in Eshell to fix this.
> 
> How about something like this? It's a little less invasive (but no less 
> of a hack).

Oops, something more like this (let-binding 'tramp-remote-path').

From 0025ae9429d97501c493653593beb88ec9ea0259 Mon Sep 17 00:00:00 2001
From: Jim Porter <[email-masked]>
Date: Sun, 27 Aug 2023 12:53:40 -0700
Subject: [PATCH] Fix remote path setting in Eshell

This ensures that we supply Tramp with the local PATH so that it can
do its job of starting the local "ssh", or whatever the method uses
(bug#65551).

* lisp/eshell/esh-proc.el (eshell-gather-process-output): Add special
handling for remote processes.

* test/lisp/eshell/esh-proc-tests.el
(esh-var-test/remote/remote-path): New test.
---
 lisp/eshell/esh-proc.el            | 15 ++++++++++++++-
 test/lisp/eshell/esh-proc-tests.el | 15 +++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/lisp/eshell/esh-proc.el b/lisp/eshell/esh-proc.el
index fcd59ab9f37..49577201ddd 100644
--- a/lisp/eshell/esh-proc.el
+++ b/lisp/eshell/esh-proc.el
@@ -265,6 +265,8 @@ eshell-last-sync-output-start
   "A marker that tracks the beginning of output of the last subprocess.
 Used only on systems which do not support async subprocesses.")
 
+(defvar tramp-remote-path)
+
 (defun eshell-gather-process-output (command args)
   "Gather the output from COMMAND + ARGS."
   (require 'esh-var)
@@ -272,7 +274,9 @@ eshell-gather-process-output
   (unless (and (file-executable-p command)
 	       (file-regular-p (file-truename command)))
     (error "%s: not an executable file" command))
-  (let* ((delete-exited-processes
+  (let* ((real-path (getenv "PATH"))
+         (tramp-remote-path tramp-remote-path)
+         (delete-exited-processes
 	  (if eshell-current-subjob-p
 	      eshell-delete-exited-processes
 	    delete-exited-processes))
@@ -280,6 +284,15 @@ eshell-gather-process-output
          (coding-system-for-read coding-system-for-read)
          (coding-system-for-write coding-system-for-write)
 	 proc stderr-proc decoding encoding changed)
+    ;; HACK: We want to supply our subprocess with the all the
+    ;; environment variables we've set in Eshell.  However, supplying
+    ;; a remote PATH this way can break Tramp, which needs the *local*
+    ;; PATH for calling "ssh", etc.  Instead, set the local path in
+    ;; our `process-environment' and pass the remote PATH via
+    ;; `tramp-remote-path'.
+    (when (file-remote-p default-directory)
+      (push (concat "PATH=" real-path) process-environment)
+      (setq tramp-remote-path (eshell-get-path)))
     ;; MS-Windows needs special setting of encoding/decoding, because
     ;; (a) non-ASCII text in command-line arguments needs to be
     ;; encoded in the system's codepage; and (b) because many Windows
diff --git a/test/lisp/eshell/esh-proc-tests.el b/test/lisp/eshell/esh-proc-tests.el
index 8e02fbb5497..7d0432dbe68 100644
--- a/test/lisp/eshell/esh-proc-tests.el
+++ b/test/lisp/eshell/esh-proc-tests.el
@@ -259,4 +259,19 @@ esh-proc-test/kill-pipeline-head
                      output-start (eshell-end-of-output))
                     "")))))
 
+
+;; Remote processes
+
+(ert-deftest esh-var-test/remote/remote-path ()
+  "Ensure that setting the remote PATH in Eshell doesn't interfere with Tramp.
+See bug#65551."
+  (skip-unless (and (eshell-tests-remote-accessible-p)
+                    (executable-find "echo")))
+  (let ((default-directory ert-remote-temporary-file-directory))
+    (with-temp-eshell
+     (eshell-insert-command "set PATH ''")
+     (eshell-match-command-output
+      (format "%s hello" (executable-find "echo" t))
+      "\\`hello\n"))))
+
 ;;; esh-proc-tests.el ends here
-- 
2.25.1