Skip to content

Can't get suggestions to work #1085

Description

@pieter-venter

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

  1. jonsequitur commented on Nov 19, 2020

    @jonsequitur
    Contributor

    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.

    This is correct.The overall dotnet-suggest flow and debugging steps are outlined here: #794 (comment)

    The fact that try and trace are completable does look wrong.

  2. added
    Area-Completions
    on Nov 19, 2020
  3. pieter-venter commented on Nov 19, 2020

    @pieter-venter
    ContributorAuthor

    The fact that try and trace are completable does look wrong.
    Did some troubleshooting.

    Looks like a bug to me in dotnet-suggest-shim.bash. This line

    complete -F _dotnet_bash_complete `dotnet-suggest list`
    

    uses backticks around dotnet-suggest list, which does command substitution. Command substitution will also do word splitting.
    Since dotnet-suggest list returns dotnet try (among others), it effectively becomes

    complete -F _dotnet_bash_complete dotnet try
    

    Based on the documentation of complete, that means both dotnet and try now be registered (it allows multiple commands to be listed).

    The result is that you have multiple entries registered

    complete -F _dotnet_bash_complete dotnet
    complete -F _dotnet_bash_complete try
    

    I assume what was meant was that each line returned by dotnet-suggest list should 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_complete already:

    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

  4. pieter-venter commented on Nov 19, 2020

    @pieter-venter
    ContributorAuthor

    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:

    • Create a console application called ConsoleApp1
    • Delete all files in /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.
    • Setup the debug environment as per dotnet-suggest either no results at all or an error #794 (comment)
    • Monitor the contents of ~/.config/dotnet-suggest/debug.log
    • Monitor what dotnet-suggest list returns
    • Monitor the contents of ~/.dotnet-suggest-registration.txt
    • Monitor what is registered using complete | grep _dot

    I found that dotnet-suggest add was 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.cs

            public Registration FindRegistration(FileInfo soughtExecutable)
            {
                if (soughtExecutable == null)
                {
                    return null;
                }
    
                if (soughtExecutable.Exists)
                {
                    return new Registration(soughtExecutable.FullName);
                }

    The soughtExecutable.Exists returns 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.txt as 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.txt and I refreshed my bash session with source ~/.bashrc, the suggestions are in fact working!

  5. jonsequitur commented on Nov 19, 2020

    @jonsequitur
    Contributor

    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?

  6. added a commit that references this issue on Nov 21, 2020
    f565fd9
  7. pieter-venter commented on Nov 21, 2020

    @pieter-venter
    ContributorAuthor

    Taking a look at the first issue again, I created a fix and it will correctly register

    complete -F _dotnet_bash_complete dotnet-try
    complete -F _dotnet_bash_complete dotnet try

    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 string dotnet try is 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\ try to see if that works, but I could not get it to work.

    According to the docs, it seems like a name cannot 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.

  8. jonsequitur commented on Nov 21, 2020

    @jonsequitur
    Contributor

    This fix looks good. Would you mind opening a PR?

    And yeah, the space after dotnet breaks completion (#343). Completions for the dotnet CLI do handle this (https://docs.microsoft.com/en-us/dotnet/core/tools/enable-tab-autocomplete) and if the dotnet-suggest functionality is made available there, we can resolve it that way. Or dotnet-suggest could potentially parse the second token and look for a registration that matches.

  9. pieter-venter commented on Nov 21, 2020

    @pieter-venter
    ContributorAuthor

    PR created.

    I would like to take a look at the second, more important issue.

    The change was intended to make registration unnecessary, so that local builds will work, not just installed tools.

    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 list is called. And for that to happen, it needs to be either

    1. an installed tool under ~/.dotnet/tools
    2. it needs to be listed in ~/.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.

  10. added a commit that references this issue on Nov 22, 2020
    c5995f6
  11. pieter-venter commented on Nov 22, 2020

    @pieter-venter
    ContributorAuthor

    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

    Unhandled exception. System.ArgumentException: Incorrect token type: Argument: ./ConsoleApp1
       at System.CommandLine.Parsing.CommandNode..ctor(Token token, ICommand command, CommandNode parent)
       at System.CommandLine.Parsing.RootCommandNode..ctor(Token token, ICommand command)
       at System.CommandLine.Parsing.ParseOperation.ParseRootCommand()
       at System.CommandLine.Parsing.ParseOperation.Parse()
       at System.CommandLine.Parsing.Parser.Parse(IReadOnlyList`1 arguments, String rawInput)
       at System.CommandLine.CommandExtensions.GetInvocationPipeline(Command command, String[] args)
       at System.CommandLine.CommandExtensions.InvokeAsync(Command command, String[] args, IConsole console)
    

    and it can be reproduced by passing the program's own name, for example

    $ ./ConsoleApp1 ./ConsoleApp1

    I found this because for the suggestions it tries to pass

    $./ConsoleApp1 [suggest:14] ./ConsoleApp1 
    

    and throws an exception. To reproduce just use the latest nuget package vs the latest main.

  12. jonsequitur commented on Nov 23, 2020

    @jonsequitur
    Contributor

    Thanks!

    If this isn't present in the latest main then it sounds like we just need to publish a new package, which is overdue in any case.

  13. added 2 commits that reference this issue on Nov 24, 2020
    01f2691
    c4e8a8f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Completions

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions