Subject: Re: bug#50387: Possible bug in Tramp or in completions

From: Gregory Heytings <[email-masked]>
Date: Mon, 06 Sep 2021 16:34:41 +0000

Hi Michael,

Thanks for your answer!

>> I analyzed this bug a bit further, and the root problem is that
>>
>> (tramp-completion-handle-file-name-all-completions "" "/")
>>
>> returns ("sshfs:" "ssh:" "sshx:" ...) when tramp-default-method is 
>> "ssh", and ("scp:" "scpx:" ...) when tramp-default-method is "scp".
>>
>> This happens because "possible methods" are added to the result in 
>> tramp-completion-handle-file-name-all-completions.
>>
>> Therefore completion-substring-try-completion finds that "ssh:" is 
>> already a complete match.
>>
>> I'm not sure how this bug should be fixed.  Removing these two lines 
>> from that function fixes the bug, but that's not really optimal because 
>> in that case /s TAB does not show "ssh:", "sshfs:" and "sshx:" anymore. 
>> Not displaying these completions is perhaps better than not displaying 
>> hostnames, however.
>
> Tramp doesn't know anything about completion-styles, it just implements 
> file-name-all-completions and file-name-completion. Cutting valid 
> results from those functions seems to be wrong, IMO.
>

The problem is not that results are cut in this case, the problem is that 
because the return value of 
tramp-completion-handle-file-name-all-completions includes the methods 
("sshfs:" "ssh:" "sshx:"), some completion mechanisms do not look further 
when "ssh:" is entered and the user hits TAB to list host candidates:

- substring considers that there is nothing more to complete because 
"ssh:" is one completion candidate

- partial-completion and flex consider that "ssh:" is not complete, but 
the completion candidates they propose are "ssh:", "sshx:" and "sshfs:".

For the case of the substring completion mechanisms (i.e. after 
(add-to-list 'completion-styles 'substring),

(completion-substring-try-completion "/ssh:" #'completion-file-name-table 
#'file-exists-p 5)

returns t with tramp-default-method set to "ssh" (and therefore the 
completion system does not try further), and nil with tramp-default-method 
set to "scp" (and therefore the completion system does try further, and 
eventually finds the hosts).

Without

;; Possible methods.
(setq result (append result (tramp-get-completion-methods m)))

in tramp-completion-handle-file-name-all-completions, all completion 
mechanisms do what a user would expect, namely list host candidates. 
(But the tramp methods are not shown anymore in the completion candidates 
after e.g. /s TAB.)

It seems to me that adding methods (which are not file names), 
conditionally to tramp-default-method, to file-name-all-completions, is 
not TRT, hence this bug.