Re: [PATCH] Provide completion candidates for remote containers over a TRAMP connection

Michael Albinus <[email protected]>
Newsgroups gmane.emacs.tramp
Message-ID <[email protected]>
Gene Goykhman <[email protected]> writes:

Hi Gene,

>> No. Don't do that. Instead, bind tramp-last-hop-directory to nil in that
>> let-clause:
>
> I'm not sure how best to handle the lifetime of
> tramp--last-hop-directory. It needs to be explicity set to nil in
> tramp-completion-handle-file-name-all-completions UNLESS we're
> completing a multi-hop path, and only in that case should it be set to
> the parsed last hop directory.

As I have said above: Bind it in a let-clause.

> That value is then consumed later by
> tramp-container--completion-function. The value remains set after use
> however, so it is important that it is subsequently reset to nil the
> next time tramp-completion-handle-file-name-all-completions is called,
> otherwise we could end up with a stale value of
> tramp--last-hop-directory for a subsequent single-hop TRAMP
> connection.
>
> If you can suggest a better way to handle this I'd appreciate it.

The let-clause shall be the one in
tramp-completion-handle-file-name-all-completions, see below. When you
leave that clause, the original value (nil) is re-set, everything is fine.

>> Replace this by
>
>>       (setq hop (match-string 1 fullname)
>> 	    fullname (replace-match "" nil nil fullname 1)
>> 	    tramp-last-hop-directory
>> 	    (file-remote-p
>> 	     (tramp-make-tramp-file-name (tramp-dissect-hop-name hop)))))
>
> I tried this but ended up with a tramp--last-hop-directory that I wasn't able to use to execute the container executable remotely.
>
> Here is a specific example. For the path:
>
> /ssh:[email protected]|sudo:line5.timetiger.com|docker:
>
> My original patch produces this value of tramp--last-hop-directory:
>
> #("/sudo:[email protected]:/" 6 10
>   (tramp-default t))
>
> When I set this as the default-directory, I am able to run the container program on the remote host and obtain the list of completion candidates (running containers).
>
> After incorporating your suggested changes, the value of tramp--last-hop-directory becomes:
>
> "/ssh:[email protected]|sudo:line5.timetiger.com:"
>
> Setting this as the default-directory, I am /not/ able to run the container program (TRAMP seems to hang, at least on my macOS system).
>
> I think that, at least on my system, the root@ is required, and I think the multi-hop path also doesn't work as a default-directory.

Well, testing everything I was hit by a similar problem. It took me a
while of debugging until I found the culprit.

For good reasons, tramp-completion-handle-file-name-all-completions
binds tramp-default-user and friends to nil. This is in order to get
proper completion results. But then, the code I have proposed above does
not expand to root@.

The solution is, that computing tramp-last-hop-directory must happen in
time, before tramp-default-* variables are let-bound to nil. The
following has worked in my tests:

--8<---------------cut here---------------start------------->8---
(defun tramp-completion-handle-file-name-all-completions (filename directory)
  "Like `file-name-all-completions' for partial Tramp files."
  (let ((fullname
	 (tramp-drop-volume-letter (expand-file-name filename directory)))
	(directory (tramp-drop-volume-letter directory))
	tramp-last-hop-directory hop result result1)

    ;; Suppress hop from completion.
    (when (string-match
	   (rx
	    (regexp tramp-prefix-regexp)
	    (group (+ (regexp tramp-remote-file-name-spec-regexp)
		      (regexp tramp-postfix-hop-regexp))))
	   fullname)
      (setq hop (match-string 1 fullname)
	    fullname (replace-match "" nil nil fullname 1)
	    tramp-last-hop-directory
	    (tramp-make-tramp-file-name (tramp-dissect-hop-name hop))))

    (let (;; When `tramp-syntax' is `simplified', we need a default method.
	  (tramp-default-method
	   (and (string-empty-p tramp-postfix-method-format)
		tramp-default-method))
	  (tramp-default-method-alist
	   (and (string-empty-p tramp-postfix-method-format)
		tramp-default-method-alist))
	  tramp-default-user tramp-default-user-alist
	  tramp-default-host tramp-default-host-alist)

      ;; Possible completion structures.
      (dolist (elt (tramp-completion-dissect-file-name fullname))
      ...
--8<---------------cut here---------------end--------------->8---

> Thanks for your help so far!

Best regards, Michael.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.