# Buggy code
def _load_extension_images_from_publisher(publisher):
    from msrestazure.azure_exceptions import CloudError

    try:
        types = client.virtual_machine_extension_images.list_types(location, publisher)
    except CloudError:  # PIR image publishers might not have any extension images, exception could raise
        types = []
    if name:
        types = [t for t in types if _matched(name, t.name, partial_match)]
    for t in types:
        versions = client.virtual_machine_extension_images.list_versions(
            location, publisher, t.name
        )
        if version:
            versions = [v for v in versions if _matched(version, v.name, partial_match)]

        if show_latest:
            # pylint: disable=no-member
            versions.sort(key=lambda v: LooseVersion(v.name), reverse=True)
            all_images.append(
                {"publisher": publisher, "name": t.name, "version": versions[0].name}
            )
        else:
            for v in versions:
                all_images.append(
                    {"publisher": publisher, "name": t.name, "version": v.name}
                )


# Traceback (IndexError)
$ az vm extension image list --latest -l westcentralus -o tsv
The command failed with an unexpected error. Here is the traceback:

list index out of range
Traceback (most recent call last):
File "/opt/az/lib/python3.6/site-packages/knack/cli.py", line 206, in invoke
cmd_result = self.invocation.execute(args)
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 560, in execute
raise ex
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 618, in _run_jobs_serially
results.append(self._run_job(expanded_arg, cmd_copy))
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 611, in _run_job
six.reraise(*sys.exc_info())
File "/opt/az/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 588, in _run_job
result = cmd_copy(params)
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 297, in __call__
return self.handler(*args, **kwargs)
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/__init__.py", line 461, in default_command_handler
return op(**command_args)
File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/vm/custom.py", line 1272, in list_vm_extension_images
cmd.cli_ctx, publisher_name, name, version, image_location, latest)
File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/vm/_actions.py", line 155, in load_extension_images_thru_services
t.result()  # don't use the result but expose exceptions from the threads
File "/opt/az/lib/python3.6/concurrent/futures/_base.py", line 425, in result
return self.__get_result()
File "/opt/az/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/opt/az/lib/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/vm/_actions.py", line 137, in _load_extension_images_from_publisher
'version': versions[0].name})
IndexError: list index out of range

# Fixed code
def _load_extension_images_from_publisher(publisher):
    from msrestazure.azure_exceptions import CloudError

    try:
        types = client.virtual_machine_extension_images.list_types(location, publisher)
    except CloudError:  # PIR image publishers might not have any extension images, exception could raise
        types = []
    if name:
        types = [t for t in types if _matched(name, t.name, partial_match)]
    for t in types:
        versions = client.virtual_machine_extension_images.list_versions(
            location, publisher, t.name
        )
        if version:
            versions = [v for v in versions if _matched(version, v.name, partial_match)]

        if show_latest:
            # pylint: disable=no-member
            versions.sort(key=lambda v: LooseVersion(v.name), reverse=True)
            try:
                all_images.append(
                    {
                        "publisher": publisher,
                        "name": t.name,
                        "version": versions[0].name,
                    }
                )
            except IndexError:
                pass  # if no versions for this type continue to next type.
        else:
            for v in versions:
                all_images.append(
                    {"publisher": publisher, "name": t.name, "version": v.name}
                )