Azure / azure-cli

Azure Command-Line Interface
MIT License
3.96k stars 2.94k forks source link

az cli incorrectly returns exit code 0 even on failure #28523

Open bugrasan opened 6 months ago

bugrasan commented 6 months ago

Describe the bug

The command az account list silently fails when not already logged in and the process returns exit code 0. This makes breaks Unix convention and makes it more difficult in automation to detect failure.

Related command

az account list compared with az account show that provides the correct exit code.

$ az account list
Please run "az login" to access your accounts.
[]
$ echo $?
0
$ az account show
Please run 'az login' to setup account.
$ echo $?
1

Errors

no error - which is the problem

Issue script & Debug output

$ az account list --debug
cli.knack.cli: Command arguments: ['account', 'list', '--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 0x7fc50c266660>, <function OutputProducer.on_global_arguments at 0x7fc50c209f80>, <function CLIQuery.on_global_arguments at 0x7fc50c243ba0>]
cli.knack.cli: Event: CommandInvoker.OnPreCommandTableCreate []
cli.azure.cli.core: Modules found from index for 'account': ['azure.cli.command_modules.profile', 'azure.cli.command_modules.resource']
cli.azure.cli.core: Loading command modules:
cli.azure.cli.core: Name                  Load Time    Groups  Commands
cli.azure.cli.core: profile                   0.002         2         8
cli.azure.cli.core: resource                  0.098        51       228
cli.azure.cli.core: Total (2)                 0.100        53       236
cli.azure.cli.core: Loaded 52 groups, 236 commands.
cli.azure.cli.core: Found a match in the command table.
cli.azure.cli.core: Raw command  : account list
cli.azure.cli.core: Command table: account list
cli.knack.cli: Event: CommandInvoker.OnPreCommandTableTruncate [<function AzCliLogging.init_command_file_logging at 0x7fc50b4ced40>]
cli.azure.cli.core.azlogging: metadata file logging enabled - writing logs to '/home/vscode/.azure/commands/2024-03-07.10-12-22.account_list.9263.log'.
az_command_data_logger: command args: account list --debug
cli.knack.cli: Event: CommandInvoker.OnPreArgumentLoad [<function register_global_subscription_argument.<locals>.add_subscription_parameter at 0x7fc50b51d4e0>]
cli.knack.cli: Event: CommandInvoker.OnPostArgumentLoad []
cli.knack.cli: Event: CommandInvoker.OnPostCommandTableCreate [<function register_ids_argument.<locals>.add_ids_arguments at 0x7fc50b51f6a0>, <function register_cache_arguments.<locals>.add_cache_arguments at 0x7fc50b51f7e0>]
cli.knack.cli: Event: CommandInvoker.OnCommandTableLoaded []
cli.knack.cli: Event: CommandInvoker.OnPreParseArgs []
cli.knack.cli: Event: CommandInvoker.OnPostParseArgs [<function OutputProducer.handle_output_argument at 0x7fc50c20a020>, <function CLIQuery.handle_query_parameter at 0x7fc50c243c40>, <function register_ids_argument.<locals>.parse_ids_arguments at 0x7fc50b51f740>]
cli.azure.cli.command_modules.profile.custom: Please run "az login" to access your accounts.
cli.knack.cli: Event: CommandInvoker.OnTransformResult [<function _resource_group_transform at 0x7fc50b51c720>, <function _x509_from_base64_to_hex_transform at 0x7fc50b51c7c0>]
cli.knack.cli: Event: CommandInvoker.OnFilterResult []
[]
cli.knack.cli: Event: Cli.SuccessfulExecute []
cli.knack.cli: Event: Cli.PostExecute [<function AzCliLogging.deinit_cmd_metadata_logging at 0x7fc50b4cefc0>]
az_command_data_logger: exit code: 0
cli.__main__: Command ran in 0.205 seconds (init: 0.098, invoke: 0.106)

Expected behavior

exit code of 1 similar to az account show

Environment Summary

$ az --version
azure-cli                         2.57.0 *

core                              2.57.0 *
telemetry                          1.1.0

Dependencies:
msal                              1.26.0
azure-mgmt-resource             23.1.0b2

Python location '/usr/local/pipx/venvs/azure-cli/bin/python'
Extensions directory '/home/vscode/.azure/cliextensions'

Python (Linux) 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]

Additional context

No response

azure-client-tools-bot-prd[bot] commented 6 months ago

Hi @bugrasan,

2.57.0 is not the latest Azure CLI(2.58.0).

If you haven't already attempted to do so, please upgrade to the latest Azure CLI version by following https://learn.microsoft.com/en-us/cli/azure/update-azure-cli.

yonzhan commented 6 months ago

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

bugrasan commented 5 months ago

Hi @bugrasan,

2.57.0 is not the latest Azure CLI(2.58.0).

If you haven't already attempted to do so, please upgrade to the latest Azure CLI version by following https://learn.microsoft.com/en-us/cli/azure/update-azure-cli.

same with v2.58:

$ az account list
Please run "az login" to access your accounts.
[]
$ echo $?
0
$ az --version
azure-cli                         2.58.0

core                              2.58.0
telemetry                          1.1.0

Dependencies:
msal                              1.26.0
azure-mgmt-resource             23.1.0b2
trkoch commented 2 months ago

Same for az containerapp show, i.e. exit code 0 even on failure (when stderr is populated).