# Buggy code
def list_pkgs(versions_as_list=False, **kwargs):
    """
    List the packages currently installed in a dict::

        {'<package_name>': '<version>'}

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.list_pkgs
        salt '*' pkg.list_pkgs versions_as_list=True
    """
    versions_as_list = salt.utils.is_true(versions_as_list)
    # not yet implemented or not applicable
    if any([salt.utils.is_true(kwargs.get(x)) for x in ("removed", "purge_desired")]):
        return {}

    ret = {}
    name_map = _get_name_map()
    for pkg_name, val in six.iteritems(_get_reg_software()):
        if pkg_name in name_map:
            key = name_map[pkg_name]
            if val in ["(value not set)", "Not Found", None, False]:
                # Look up version from winrepo
                pkg_info = _get_package_info(key)
                if not pkg_info:
                    continue
                for pkg_ver in pkg_info.keys():
                    if pkg_info[pkg_ver]["full_name"] == pkg_name:
                        val = pkg_ver
        else:
            key = pkg_name
        __salt__["pkg_resource.add_pkg"](ret, key, val)

    __salt__["pkg_resource.sort_pkglist"](ret)
    if not versions_as_list:
        __salt__["pkg_resource.stringify"](ret)
    return ret


# Traceback (CommandExecutionError)
----------
ID: openvpn
Function: pkg.installed
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "C:\salt\bin\lib\site-packages\salt\state.py", line 1626, in call
**cdata['kwargs'])
File "C:\salt\bin\lib\site-packages\salt\loader.py", line 1492, in wrapper
return f(*args, **kwargs)
File "c:\salt\var\cache\salt\minion\extmods\states\pkg.py", line 984, in installed
**kwargs)
File "c:\salt\var\cache\salt\minion\extmods\states\pkg.py", line 294, in _find_install_targets
cur_pkgs = __salt__['pkg.list_pkgs'](versions_as_list=True, **kwargs)
File "c:\salt\var\cache\salt\minion\extmods\modules\win_pkg.py", line 244, in list_pkgs
name_map = _get_name_map()
File "c:\salt\var\cache\salt\minion\extmods\modules\win_pkg.py", line 1067, in _get_name_map
name_map = get_repo_data().get('name_map', {})
File "c:\salt\var\cache\salt\minion\extmods\modules\win_pkg.py", line 1050, in get_repo_data
'Windows repo cache doesn\'t exist, pkg.refresh_db likely '
CommandExecutionError: Windows repo cache doesn't exist, pkg.refresh_db likely needed

# Fixed code
def list_pkgs(versions_as_list=False, saltenv="base", **kwargs):
    """
    List the packages currently installed in a dict::

        {'<package_name>': '<version>'}

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.list_pkgs
        salt '*' pkg.list_pkgs versions_as_list=True
    """
    versions_as_list = salt.utils.is_true(versions_as_list)
    # not yet implemented or not applicable
    if any([salt.utils.is_true(kwargs.get(x)) for x in ("removed", "purge_desired")]):
        return {}

    ret = {}
    name_map = _get_name_map(saltenv)
    for pkg_name, val in six.iteritems(_get_reg_software()):
        if pkg_name in name_map:
            key = name_map[pkg_name]
            if val in ["(value not set)", "Not Found", None, False]:
                # Look up version from winrepo
                pkg_info = _get_package_info(key, saltenv=saltenv)
                if not pkg_info:
                    continue
                for pkg_ver in pkg_info.keys():
                    if pkg_info[pkg_ver]["full_name"] == pkg_name:
                        val = pkg_ver
        else:
            key = pkg_name
        __salt__["pkg_resource.add_pkg"](ret, key, val)

    __salt__["pkg_resource.sort_pkglist"](ret)
    if not versions_as_list:
        __salt__["pkg_resource.stringify"](ret)
    return ret