Repository navigation
Description
I've spend a lot of time trying to get suggestions to work but to no avail. I tried both on Windows and Ubuntu Linux using .Net Core 3.1 SDK.
I'm using the latest Nuget Package and followed the instructions here.
Using dotnet-suggest (1.1.142701) and System.CommandLine 2.0.0-beta1.20371.2
There are some things I find confusing and would appreciate some help. The bash script added to my .bashrc contains this:
complete -F _dotnet_bash_complete `dotnet-suggest list`
My understanding is that is going to register a completion handler, which in turn calls a function called _dotnet_bash_complete, which has been provided.
dotnet-suggest list returns a list of strings. Looking at the code, it defaults to all the dotnet tools, in two forms. One with dash, one without:
dotnet-try
dotnet try
dotnet-suggest
dotnet suggest
dotnet-trace
dotnet trace
It also looks for additional "user" paths registered in a special file here
~/.dotnet-suggest-registration.txt
These items can be registered manually, or it seems, automatically the first time you run your System.Console based app. However, it only puts the path in the list.
Next, I can inspect what has been registered as follows:
complete | grep _dot
complete -F _dotnet_bash_complete dotnet-suggest
complete -F _dotnet_bash_complete dotnet
complete -F _dotnet_bash_complete trace
complete -F _dotnet_bash_complete suggest
complete -F _dotnet_bash_complete dotnet-try
complete -F _dotnet_bash_complete try
complete -F _dotnet_bash_complete dotnet-trace
This seems consistent with the output of dotnet-suggest list and what we're telling complete to register - but surely this is not what we wanted?
My understanding is, we're telling the shell "If you type trace then call _dotnet_bash_complete. The same for all the other registered commands. This in itself seems wrong - I don't want to call the script for items such as try and trace.
Could someone explain the flow for a console app called myapp? Surely it must actually register the string myapp (and not just the path where it's located) so that when you hit tab, it will run the script and in turn call dotnet-suggest, which calls myapp with the correct arguments to get help and then return to the user.
I may well be missing something here! Help appreciated.
Activity
jonsequitur commentedon Nov 19, 2020
This is correct.The overall
dotnet-suggestflow and debugging steps are outlined here: #794 (comment)The fact that
tryandtraceare completable does look wrong.pieter-venter commentedon Nov 19, 2020
Looks like a bug to me in
dotnet-suggest-shim.bash. This lineuses backticks around
dotnet-suggest list, which does command substitution. Command substitution will also do word splitting.Since
dotnet-suggest listreturnsdotnet try(among others), it effectively becomesBased on the documentation of
complete, that means bothdotnetandtrynow be registered (it allows multiple commands to be listed).The result is that you have multiple entries registered
I assume what was meant was that each line returned by
dotnet-suggest listshould be separately registered.My bash skills are not great, but it looks like one way would be to change the IFS value prior to executing the command (to not split on spaces) and then resetting it. The same technique is actually used inside
_dotnet_bash_completealready:local IFS=$'\n'https://unix.stackexchange.com/questions/39473/command-substitution-splitting-on-newline-but-not-space
https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash
pieter-venter commentedon Nov 19, 2020
Moving to the issue of why suggestions are not appearing
FYI this is all on Ubuntu Linux
Here is a summary of what I did:
/tmp/system-commandline-sentinel-files. If a file is present here for your application, it will never try to register again. This can be problematic when repeating tests, so I just clear it between runs.~/.config/dotnet-suggest/debug.logdotnet-suggest listreturns~/.dotnet-suggest-registration.txtcomplete | grep _dotI found that
dotnet-suggest addwas correctly being called, however nothing was added to~/.dotnet-suggest-registration.txt.Looking at the code, I've narrowed it down to this code in
FileSuggestionRegistration.csThe
soughtExecutable.Existsreturns true (since ConsoleApp1 with full path does in fact exist!) and then returns a Registration object. The caller of this function will not add it to~/.dotnet-suggest-registration.txtas a result. If you comment out that check, it will get added.For reference, a change was made in this commit. Perhaps @jonsequitur can confirm if this behaviour was intended? Maybe there is something I don't understand.
However, after I confirmed an entry was created for ConsoleApp1 inside
~/.dotnet-suggest-registration.txtand I refreshed my bash session withsource ~/.bashrc, the suggestions are in fact working!jonsequitur commentedon Nov 19, 2020
Thanks for the in-depth investigation. This area is tricky to test.
The change was intended to make registration unnecessary, so that local builds will work, not just installed tools.
Do we understand the repro steps for a bug and do you have a suggested fix?
Fix complete registration issue where items with spaces are registere…
pieter-venter commentedon Nov 21, 2020
Taking a look at the first issue again, I created a fix and it will correctly register
However, during my testing it looks like the second variant (
dotnet try) does not work with tab completion anyways. In other words, even though the stringdotnet tryis registered to trigger the function_dotnet_bash_complete, the shell seems to consider them as two words and doesn't trigger the completion function. I've tried things like$ dotnet\ tryto see if that works, but I could not get it to work.According to the docs, it seems like a
namecannot contain spaces for the complete builtin.What was the original use case @jonsequitur? Was the idea to support completion for both forms for dotnet tools? It would seem like this would not be possible on Linux. So we'd have to modify this based on platform, or filter it out in the bash script. I have not tried to see if this works on Windows.
jonsequitur commentedon Nov 21, 2020
This fix looks good. Would you mind opening a PR?
And yeah, the space after
dotnetbreaks completion (#343). Completions for thedotnetCLI do handle this (https://docs.microsoft.com/en-us/dotnet/core/tools/enable-tab-autocomplete) and if thedotnet-suggestfunctionality is made available there, we can resolve it that way. Ordotnet-suggestcould potentially parse the second token and look for a registration that matches.pieter-venter commentedon Nov 21, 2020
PR created.
I would like to take a look at the second, more important issue.
Could you elaborate a bit? From what I can tell, the only way that a program can be registered for auto completion, is if it is returned as one of the items when
dotnet-suggest listis called. And for that to happen, it needs to be either~/.dotnet/tools~/.dotnet-suggest-registration.txt.I will take a look at fixing 2 - but I don't want to break whatever was meant by "so that local builds will work". LMK.
Fix dotnet-suggest issue that prevents files from being registered
pieter-venter commentedon Nov 22, 2020
FYI I found another issue that seems to be present in all the existing Nuget packages I tried but is working in the latest main branch. I'm not sure if a separate issue is warranted? (there was an old report in #590 but no reproduction. This commit likely have fixed it)
The short summary is you get a
and it can be reproduced by passing the program's own name, for example
$ ./ConsoleApp1 ./ConsoleApp1I found this because for the suggestions it tries to pass
and throws an exception. To reproduce just use the latest nuget package vs the latest main.
jonsequitur commentedon Nov 23, 2020
Thanks!
If this isn't present in the latest
mainthen it sounds like we just need to publish a new package, which is overdue in any case.Fix complete registration issue where items with spaces are registere…
Fix dotnet-suggest issue that prevents files from being registered