Azure / azure-cli

Azure Command-Line Interface
MIT License
4.01k stars 2.98k forks source link

Autocomplete Not Working for Names of Values (e.g. Resource Group) #28760

Closed michaelsaul closed 5 months ago

michaelsaul commented 6 months ago

Autocomplete in macOS Sonoma is not completing values of things like resource groups. For example, the following is happening:

~ > az aks show -g -

Entering tab added a hyphen to type the next flag rather than autocompleting values. Trying to give the first two characters of a resource group that exists does not complete anything, and just sounds the terminal bell.

This was previously working, but I"m not sure if an update to the cli or to python is what may have made this stop working.

Relevant .zshrc configuration:

# Load tabcomplete
if type brew &>/dev/null; then
  FPATH=$(brew --prefix)/share/zsh/site-functions:$FPATH
  FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

  autoload -Uz compinit && compinit
  autoload -U +X bashcompinit && bashcompinit
fi

# Az cli autocomplete
source $(brew --prefix)/etc/bash_completion.d/az

Related command

az aks show --resource-group <tab>
az vm show --resource-group <tab>

Errors

No errors, but a hyphen is entered in place of a name value.

Issue script & Debug output

cli.knack.cli: Command arguments: ['aks', 'show', '-g', '--debug'] cli.knack.cli: init debug log: Enable color in terminal. cli.knack.cli: Event: Cli.PreExecute [] cli.knack.cli: Event: CommandParser.OnGlobalArgumentsCreate [<function CLILogging.on_global_arguments at 0x1016f9580>, <function OutputProducer.on_global_arguments at 0x1017a4720>, <function CLIQuery.on_global_arguments at 0x1017ce200>] cli.knack.cli: Event: CommandInvoker.OnPreCommandTableCreate [] cli.azure.cli.core: Modules found from index for 'aks': ['azure.cli.command_modules.acs'] cli.azure.cli.core: Loading command modules: cli.azure.cli.core: Name Load Time Groups Commands cli.azure.cli.core: acs 0.024 14 76 cli.azure.cli.core: Total (1) 0.024 14 76 cli.azure.cli.core: Loaded 14 groups, 76 commands. cli.azure.cli.core: Found a match in the command table. cli.azure.cli.core: Raw command : aks show cli.azure.cli.core: Command table: aks show cli.knack.cli: Event: CommandInvoker.OnPreCommandTableTruncate [<function AzCliLogging.init_command_file_logging at 0x10250efc0>] cli.azure.cli.core.azlogging: metadata file logging enabled - writing logs to '/Users/michaelsaul/.azure/commands/2024-04-16.12-41-14.aks_show.75763.log'. az_command_data_logger: command args: aks show -g --debug cli.knack.cli: Event: CommandInvoker.OnPreArgumentLoad [<function register_global_subscription_argument..add_subscription_parameter at 0x102539a80>] cli.knack.cli: Event: CommandInvoker.OnPostArgumentLoad [] cli.knack.cli: Event: CommandInvoker.OnPostCommandTableCreate [<function register_ids_argument..add_ids_arguments at 0x10255fce0>, <function register_cache_arguments..add_cache_arguments at 0x10255fe20>] cli.knack.cli: Event: CommandInvoker.OnCommandTableLoaded [] cli.knack.cli: Event: CommandInvoker.OnPreParseArgs [] urllib3.connectionpool: Starting new HTTPS connection (1): app.aladdin.microsoft.com:443 urllib3.connectionpool: https://app.aladdin.microsoft.com:443 "GET /api/v1.0/suggestions?query=%7B%22command%22%3A+%22aks+show%22%2C+%22parameters%22%3A+%22--resource-group%22%7D&clientType=AzureCli&context=%7B%22versionNumber%22%3A+%222.59.0%22%2C+%22errorType%22%3A+%22ExpectedArgument%22%2C+%22correlationId%22%3A+%22e31faaf4-d42e-4abf-b565-a9264c677ea9%22%2C+%22eventId%22%3A+%22c3511034-1ec3-47f4-84b0-0a3154e70579%22%7D HTTP/1.1" 200 None cli.azure.cli.core.azclierror: Traceback (most recent call last): File "/opt/homebrew/Cellar/python@3.11/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/argparse.py", line 1907, in parse_known_args namespace, args = self._parse_known_args(args, namespace) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.11/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/argparse.py", line 2128, in _parse_known_args start_index = consume_optional(start_index) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.11/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/argparse.py", line 2058, in consume_optional arg_count = match_argument(action, selected_patterns) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.11/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/argparse.py", line 2222, in _match_argument raise ArgumentError(action, msg) argparse.ArgumentError: argument --resource-group/-g: expected one argument

cli.azure.cli.core.azclierror: argument --resource-group/-g: expected one argument az_command_data_logger: argument --resource-group/-g: expected one argument

Examples from AI knowledge base: az aks show --name MyManagedCluster --resource-group MyResourceGroup Show the details for a managed Kubernetes cluster

https://docs.microsoft.com/en-US/cli/azure/aks#az_aks_show Read more about the command in reference docs cli.knack.cli: Event: Cli.PostExecute [<function AzCliLogging.deinit_cmd_metadata_logging at 0x10250f240>] az_command_data_logger: exit code: 2 cli.main: Command ran in 0.351 seconds (init: 0.060, invoke: 0.292) telemetry.main: Begin splitting cli events and extra events, total events: 1 telemetry.client: Accumulated 0 events. Flush the clients. telemetry.main: Finish splitting cli events and extra events, cli events: 1 telemetry.save: Save telemetry record of length 3726 in cache telemetry.main: Begin creating telemetry upload process. telemetry.process: Creating upload process: "/opt/homebrew/Cellar/azure-cli/2.59.0/libexec/bin/python /opt/homebrew/Cellar/azure-cli/2.59.0/libexec/lib/python3.11/site-packages/azure/cli/telemetry/init.py /Users/michaelsaul/.azure" telemetry.process: Return from creating process telemetry.main: Finish creating telemetry upload process.

Expected behavior

The name of a resource group, vm, aks cluster, etc. will be autocompleted

Environment Summary

az --version azure-cli 2.59.0

core 2.59.0 telemetry 1.1.0

Extensions: init 0.1.0

Dependencies: msal 1.27.0 azure-mgmt-resource 23.1.0b2

Python location '/opt/homebrew/Cellar/azure-cli/2.59.0/libexec/bin/python' Extensions directory '/Users/michaelsaul/.azure/cliextensions'

Python (Darwin) 3.11.9 (main, Apr 2 2024, 08:25:04) [Clang 15.0.0 (clang-1500.3.9.4)]

Legal docs and information: aka.ms/AzureCliLegal

Your CLI is up-to-date.

Additional context

No response

yonzhan commented 6 months ago

Thank you for opening this issue, we will look into it.

bebound commented 6 months ago

I can't repro. AFAIK, the related code is not changed.

Does this command produce any strange errors? _ARC_DEBUG=1 IFS=$'\013' COMP_LINE='az vm show -g ' COMP_POINT=14 COMP_TYPE="" _ARGCOMPLETE=1 _ARGCOMPLETE_SUPPRESS_SPACE=0 az 8>&1 1>&2 2>&2 9>&1

michaelsaul commented 6 months ago

I can't repro. AFAIK, the related code is not changed.

Does this command produce any strange errors? _ARC_DEBUG=1 IFS=$'\013' COMP_LINE='az vm show -g ' COMP_POINT=14 COMP_TYPE="" _ARGCOMPLETE=1 _ARGCOMPLETE_SUPPRESS_SPACE=0 az 8>&1 1>&2 2>&2 9>&1

Not sure if this counts as strange:

❯ _ARC_DEBUG=1  IFS=$'\013' COMP_LINE='az vm show -g ' COMP_POINT=14 COMP_TYPE="" _ARGCOMPLETE=1 _ARGCOMPLETE_SUPPRESS_SPACE=0 az 8>&1 1>&2 2>&2 9>&1

word -g split, lexer state: ' '
In trailing whitespace

LINE: 'az vm show -g ' 
POINT: 14 
PREQUOTE: '' 
PREFIX: '' 
SUFFIX: '' 
WORDS: ['az', 'vm', 'show', '-g']
Active parsers: [MonkeyPatchedIntrospectiveArgumentParser(prog='az', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
Visited positionals: [MonkeyPatchedIntrospectiveArgumentParser(prog='az', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
invoking parser with ['vm', 'show', '-g']
Action stub called on IntrospectAction(option_strings=[], dest='_command_package', nargs='A...', const=None, default=None, type=None, choices={'vm': AzCliCommandParser(prog='az vm', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)}, required=True, help=None, metavar=None)
    args: MonkeyPatchedIntrospectiveArgumentParser(prog='az', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True) Namespace(_log_verbosity_verbose=False, _log_verbosity_debug=False, _log_verbosity_only_show_errors=False, _output_format='json', _jmespath_query=None, _command_package=None) ['vm', 'show', '-g'] None
    orig class: <class 'argparse._SubParsersAction'>
    orig callable: <bound method _SubParsersAction.__call__ of IntrospectAction(option_strings=[], dest='_command_package', nargs='A...', const=None, default=None, type=None, choices={'vm': AzCliCommandParser(prog='az vm', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)}, required=True, help=None, metavar=None)>
orig class is a subparsers action: patching and running it
Action stub called on IntrospectAction(option_strings=[], dest='_subcommand', nargs='A...', const=None, default=None, type=None, choices={'show': AzCliCommandParser(prog='az vm show', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)}, required=True, help=None, metavar=None)
    args: MonkeyPatchedIntrospectiveArgumentParser(prog='az vm', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True) Namespace(_subcommand=None) ['show', '-g'] None
    orig class: <class 'argparse._SubParsersAction'>
    orig callable: <bound method _SubParsersAction.__call__ of IntrospectAction(option_strings=[], dest='_subcommand', nargs='A...', const=None, default=None, type=None, choices={'show': AzCliCommandParser(prog='az vm show', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)}, required=True, help=None, metavar=None)>
orig class is a subparsers action: patching and running it

exception <class 'ValueError'> too many values to unpack (expected 3) while parsing args
all active parsers: [MonkeyPatchedIntrospectiveArgumentParser(prog='az', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), MonkeyPatchedIntrospectiveArgumentParser(prog='az vm', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), MonkeyPatchedIntrospectiveArgumentParser(prog='az vm show', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
active_parser: MonkeyPatchedIntrospectiveArgumentParser(prog='az vm show', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)
optional options: ['-h', '--help', '--verbose', '--debug', '--only-show-errors', '--output', '-o', '--query', '--subscription', '--resource-group', '-g', '--name', '-n', '--show-details', '-d', '--include-user-data', '-u', '--ids']
next_positional: None
Active actions (L=0): []
active options: ['-h', '--help', '--verbose', '--debug', '--only-show-errors', '--output', '-o', '--query', '--subscription', '--resource-group', '-g', '--name', '-n', '--show-details', '-d', '--include-user-data', '-u', '--ids']
display completions: {'-h': 'show this help message and exit', '--help': 'show this help message and exit', '--verbose': 'Increase logging verbosity. Use --debug for full debug logs.', '--debug': 'Increase logging verbosity to show all debug logs.', '--only-show-errors': 'Only show errors, suppressing warnings.', '--output': 'Output format', '-o': 'Output format', '--query': 'JMESPath query string. See http://jmespath.org/ for more information and examples.', '--subscription': 'Name or ID of subscription. You can configure the default subscription using `az account set -s NAME_OR_ID`', '--cmd': '==SUPPRESS==', '--resource-group': 'Name of resource group. You can configure the default group using `az configure --defaults group=<name>`', '-g': 'Name of resource group. You can configure the default group using `az configure --defaults group=<name>`', '--name': 'The name of the Virtual Machine. You can configure the default using `az configure --defaults vm=<name>`', '-n': 'The name of the Virtual Machine. You can configure the default using `az configure --defaults vm=<name>`', '--show-details': 'show public ip address, FQDN, and power states. command will run slow', '-d': 'show public ip address, FQDN, and power states. command will run slow', '--include-user-data': 'Include the user data properties in the query result.', '-u': 'Include the user data properties in the query result.', '--ids': "One or more resource IDs (space-delimited). It should be a complete resource ID containing all information of 'Resource Id' arguments. You should provide either --ids or other 'Resource Id' arguments."}

Returning completions: ['-h', '--help', '--verbose', '--debug', '--only-show-errors', '--output', '-o', '--query', '--subscription', '--resource-group', '-g', '--name', '-n', '--show-details', '-d', '--include-user-data', '-u', '--ids']
-h
  --help
        --verbose
                 --debug
                        --only-show-errors
                                          --output
                                                  -o
                                                    --query
                                                           --subscription
                                                                         --resource-group
                                                                                         -g
                                                                                           --name
                                                                                                 -n
                                                                                                   --show-details
  -d
    --include-user-data
                       -u
bebound commented 6 months ago

exception <class 'ValueError'> too many values to unpack (expected 3) while parsing args and the following content are strange. It should be

exception <class 'SystemExit'> 2 while parsing args
all active parsers: [MonkeyPatchedIntrospectiveArgumentParser(prog='az', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), MonkeyPatchedIntrospectiveArgumentParser(prog='az vm', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), MonkeyPatchedIntrospectiveArgumentParser(prog='az vm show', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
active_parser: MonkeyPatchedIntrospectiveArgumentParser(prog='az vm show', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)
optional options: ['-h', '--help', '--verbose', '--debug', '--only-show-errors', '--output', '-o', '--query', '--subscription', '--resource-group', '-g', '--name', '-n', '--show-details', '-d', '--include-user-data', '-u', '--ids']
next_positional: None
Active actions (L=1): [IntrospectAction(option_strings=['--resource-group', '-g'], dest='resource_group_name', nargs=None, const=None, default=None, type=None, choices=None, required=False, help='Name of resource group. You can configure the default group using `az configure --defaults group=<name>`', metavar=None)]
Resetting completions because IntrospectAction(option_strings=['--resource-group', '-g'], dest='resource_group_name', nargs=None, const=None, default=None, type=None, choices=None, required=False, help='Name of resource group. You can configure the default group using `az configure --defaults group=<name>`', metavar=None) must consume the next argument
Activating completion for IntrospectAction(option_strings=['--resource-group', '-g'], dest='resource_group_name', nargs=None, const=None, default=None, type=None, choices=None, required=False, help='Name of resource group. You can configure the default group using `az configure --defaults group=<name>`', metavar=None) <class 'argparse._StoreAction'>
Completions: ['xxxxx']
xxxxx

I guess your installation is broken. Reinstalling may help.

michaelsaul commented 6 months ago

Thanks for looking into this. I have attempted to uninstall and remove from my home directory the .azure directory. After reinstallation, I still get the wrong autocomplete. Is there something else that I might be doing wrong?

For what it's worth, I am seeing this on separate machines (corporate owned and personal) on different, unrelated subscriptions. Both are macOS Sonoma with zsh and homebrew packages all up to date.

❯ az aks stop -g -
the following arguments are required: --name/-n

Examples from AI knowledge base:
https://aka.ms/cli_ref
Read more about the command in reference docs
❯ 
❯ brew uninstall azure-cli
Uninstalling /opt/homebrew/Cellar/azure-cli/2.59.0... (27,162 files, 636.8MB)
❯ rm -rf ~/.azure
❯ brew install azure-cli
==> Downloading https://ghcr.io/v2/homebrew/core/azure-cli/manifests/2.59.0
Already downloaded: /Users/michaelsaul/Library/Caches/Homebrew/downloads/089b83c5de720d3e3e50c52adce5b57551a7a67635bb05f0458060efa9aa5b31--azure-cli-2.59.0.bottle_manifest.json
==> Fetching azure-cli
==> Downloading https://ghcr.io/v2/homebrew/core/azure-cli/blobs/sha256:0abaf4f43b8af2cb406ff2dccc1a8aea716734333abdcb35f1f00
Already downloaded: /Users/michaelsaul/Library/Caches/Homebrew/downloads/75cd309278615f3584d4e773c64cd9f558ff4025cfca21d092dddcf3fb7049bb--azure-cli--2.59.0.arm64_sonoma.bottle.tar.gz
==> Pouring azure-cli--2.59.0.arm64_sonoma.bottle.tar.gz
Warning: Cask azure-cli was renamed to homebrew/core/azure-cli.
🍺  /opt/homebrew/Cellar/azure-cli/2.59.0: 23,530 files, 553.7MB
==> Running `brew cleanup azure-cli`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
❯ az login
A web browser has been opened at https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
[
  {
    "cloudName": "AzureCloud",
    "homeTenantId": "GUID",
    "id": "GUID",
    "isDefault": true,
    "managedByTenants": [],
    "name": "SUB_NAME",
    "state": "Enabled",
    "tenantId": "GUID",
    "user": {
      "name": "EMAIL",
      "type": "user"
    }
  }
]
❯ source ~/.zshrc
 ~  az vm show -g -
--debug              --name               --resource-group     -d                   -o
--help               --only-show-errors   --show-details       -g                   -u
--ids                --output             --subscription       -h                   
--include-user-data  --query              --verbose            -n                   
bebound commented 6 months ago

I find the cause. This only happens in Python 3.11.9, this commit introduce the API change: https://github.com/python/cpython/pull/115674

Similar issue: https://github.com/kislyuk/argcomplete/issues/483 Fixed in argcomplete 3.3.0: https://github.com/kislyuk/argcomplete/commit/54a4846b3a774cd9037167898f3cce38ba9d58e8

michaelsaul commented 6 months ago

Thank you so much for finding that! I did check and had updated to argcomplete 3.3.0, still getting an error. Does this need to be reflected somewhere in the homebrew package for azure-cli?

❯ echo $FPATH
/opt/homebrew/share/zsh-completions:/opt/homebrew/share/zsh/site-functions:/usr/local/share/zsh/site-functions:/usr/share/zsh/site-functions:/usr/share/zsh/5.9/functions
❯ brew info python-argcomplete
==> python-argcomplete: stable 3.3.0 (bottled)
Tab completion for Python argparse
https://kislyuk.github.io/argcomplete/
Installed
/opt/homebrew/Cellar/python-argcomplete/3.3.0 (53 files, 288.7KB) *
  Poured from bottle using the formulae.brew.sh API on 2024-04-24 at 14:44:41
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/p/python-argcomplete.rb
License: Apache-2.0
==> Dependencies
Build: python@3.11 ✔, python@3.12 ✔
==> Analytics
install: 7,142 (30 days), 32,204 (90 days), 72,956 (365 days)
install-on-request: 412 (30 days), 1,019 (90 days), 1,878 (365 days)
build-error: 0 (30 days)
❯ ls -al $HOMEBREW_PREFIX/share/zsh/site-functions | grep argcomplete
-rw-r--r--   1 michaelsaul  admin  9852 Apr 24 14:55 _python-argcomplete
❯ pip list | grep argcomplete
argcomplete                   3.3.0
bebound commented 6 months ago

CLI has its own site-packages, you need to update it there. The path is /opt/homebrew/Cellar/azure-cli/2.59.0/libexec/lib64/python3.11/site-packages

michaelsaul commented 6 months ago

Thank you @bebound In case anyone else needs an update before the new version is shipped, you can do the following:

cd /opt/homebrew/Cellar/azure-cli/2.59.0/libexec/lib/python3.11/site-packages
mv argcomplete argcomplete.bak
mv argcomplete-3.1.1.dist-info argcomplete-3.1.1.dist-info.bak
ln -s /opt/homebrew/Cellar/python-argcomplete/3.3.0/lib/python3.11/site-packages/argcomplete argcomplete
ln -s /opt/homebrew/Cellar/python-argcomplete/3.3.0/lib/python3.11/site-packages/argcomplete-3.3.0.dist-info argcomplete-3.3.0.dist-info