Skip to content

Bash completion function conflict with dotnet-suggest #24645

Open
@erichiller

Description

Describe the bug

A clear and concise description of what the bug is.

Both dotnet-suggest and dotnet complete use the function _dotnet_bash_complete for their bash completion scripts as seen in the dotnet cli enable-tab-autocomplete docs and the dotnet-suggest shim script dotnet-suggest-shim.bash.

This causes a conflict causing only the last registered command to work.

To Reproduce

Create a file that is sourced by your .bashrc containing the two scripts:

_dotnet_bash_complete()
{
  local word=${COMP_WORDS[COMP_CWORD]} ;

  local completions ;
  completions="$(dotnet complete --position "${COMP_POINT}" "${COMP_LINE}" 2>/dev/null)" ;
  if [ $? -ne 0 ]; then
    completions=""
  fi ;

  COMPREPLY=( $(compgen -W "$completions" -- "$word") ) ;
}
complete -f -F _dotnet_bash_complete dotnet ;

# dotnet suggest shell complete script start
_dotnet_bash_complete()
{
    local fullpath=`type -p ${COMP_WORDS[0]}`
    local escaped_comp_line=$(echo "$COMP_LINE" | sed s/\"/'\\\"'/g)
    local completions=`dotnet-suggest get --executable "${fullpath}" --position ${COMP_POINT} -- "${escaped_comp_line}"`

    if [ "${#COMP_WORDS[@]}" != "2" ]; then
        return
    fi

    local IFS=$'\n'
    local suggestions=($(compgen -W "$completions"))

    if [ "${#suggestions[@]}" == "1" ]; then
        local number="${suggestions[0]/%\ */}"
        COMPREPLY=("$number")
    else
        for i in "${!suggestions[@]}"; do
            suggestions[$i]="$(printf '%*s' "-$COLUMNS"  "${suggestions[$i]}")"
        done

        COMPREPLY=("${suggestions[@]}")
    fi
}

_dotnet_bash_register_complete()
{
    local IFS=$'\n'
    complete -F _dotnet_bash_complete `dotnet-suggest list`
}
_dotnet_bash_register_complete
export DOTNET_SUGGEST_SCRIPT_VERSION="1.0.1"
# dotnet suggest shell complete script end

Now create a new bash session, and try completions.
eg.
dotnet <tab><tab?>
or
dotnet-suggest <tab><tab>

And since dotnet-suggest's _dotnet_bash_complete was registered last, it will work, while dotnet's completion will not.

Of course I can simply change the function name, but that shouldn't be necessary.

Further technical details

dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.201
 Commit:    ef40e6aa06

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  21.04
 OS Platform: Linux
 RID:         ubuntu.21.04-x64
 Base Path:   /usr/share/dotnet/sdk/6.0.201/

Host (useful for support):
  Version: 6.0.3
  Commit:  c24d9a9c91

.NET SDKs installed:
  5.0.406 [/usr/share/dotnet/sdk]
  6.0.201 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.15 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.3 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.15 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.3 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download
dotnet tool list -g
Package Id                        Version         Commands          
--------------------------------------------------------------------
dotnet-ef                         6.0.3           dotnet-ef         
dotnet-suggest                    1.1.310101      dotnet-suggest    
microsoft.dotnet-interactive      1.0.317502      dotnet-interactive

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions