Subject: Re: bug#60499: 28.2; tramp: Remote host name not always accounted when computing multi-hops

From: Michael Albinus <[email-masked]>
Date: Wed, 04 Jan 2023 18:26:25 +0100

Jani Juhani Sinervo <[email-masked]> writes:

Hi Jani,

> When using a custom TRAMP method with a program that expects the host
> name to be passed in as an argument to a command-line switch, the
> natural way of writing the `tramp-login-args' like (("--switch" "%h"))
> does not work, since `tramp-compute-multi-hops' only considers the host
> name to have been used within the command template, if it is in its own
> list, as in (("--switch") ("%h")).
>
> This is a problem, because there are tools, like Toolbox[0], which
> allow running the command within a specific container. For example with
> Toolbox, it would look something like the following:
>
> `toolbox run -c container-name-here command'
>
> This can be turned into a TRAMP method relatively easily, and the most
> natural way to write the `tramp-login-args' would be the following:
>
> (tramp-login-args (("run") ("-c" "%h") ("sh")))

Thanks for the bug report.

> The code responsible can be found in `lisp/net/tramp.el' and within the
> function `tramp-compute-multi-hops'. Excerpt:
>
> We notice that the culprit here is the call to `member', where it
> erroneously checks for the host name being used by the command template
> by only checking whether the literal list '("%h") is within the
> `tramp-login-args'.
>
> My proposed fix would be the go through the `tramp-login-args' method
> parameter, and go through each of the list-of-lists to check whether
> there is a string "%h" there.

I've pushed a slightly simpler fix to the emacs-29 branch, see
appended. It will appear with the upcoming Emacs 29.1.

Until it is released, you could also try to apply it in Emacs 28.2 (I
didn't check myself). Or you wait for the next GNU ELPA release of
Tramp, 2.6.0.1, which will appear later this month.

Btw, the idea with toolbox looks good. I'll see whether I could add it
to Tramp 2.7, living in the Emacs master branch. However, I'd prefer to say

(tramp-login-args (("enter") ("-c" "%h")))

Best regards, Michael.

diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index 61a606c3383..f0b17ef3934 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -4694,7 +4694,8 @@ tramp-compute-multi-hops
 	      (or
 	       ;; The host name is used for the remote shell command.
 	       (member
-		'("%h") (tramp-get-method-parameter item 'tramp-login-args))
+		"%h" (tramp-compat-flatten-tree
+		      (tramp-get-method-parameter item 'tramp-login-args)))
 	       ;; The host name must match previous hop.
 	       (string-match-p previous-host host))
 	    (setq tramp-default-proxies-alist saved-tdpa)