# Buggy code
def all_commands(self):
    paths = builtins.__xonsh_env__.get("PATH", [])
    pathset = frozenset(x for x in paths if os.path.isdir(x))
    # did PATH change?
    path_hash = hash(pathset)
    cache_valid = path_hash == self._path_checksum
    self._path_checksum = path_hash
    # did aliases change?
    alss = getattr(builtins, "aliases", dict())
    al_hash = hash(frozenset(alss))
    cache_valid = cache_valid and al_hash == self._alias_checksum
    self._alias_checksum = al_hash
    # did the contents of any directory in PATH change?
    max_mtime = 0
    for path in pathset:
        mtime = os.stat(path).st_mtime
        if mtime > max_mtime:
            max_mtime = mtime
    cache_valid = cache_valid and (max_mtime <= self._path_mtime)
    self._path_mtime = max_mtime
    if cache_valid:
        return self._cmds_cache
    allcmds = {}
    for path in reversed(paths):
        # iterate backwards so that entries at the front of PATH overwrite
        # entries at the back.
        for cmd in executables_in(path):
            key = cmd.upper() if ON_WINDOWS else cmd
            allcmds[key] = (os.path.join(path, cmd), alss.get(key, None))
    for cmd in alss:
        if cmd not in allcmds:
            key = cmd.upper() if ON_WINDOWS else cmd
            allcmds[key] = (cmd, True)
    self._cmds_cache = allcmds
    return allcmds


# Traceback (NotADirectoryError)
~  xonsh
prompt: error: on field 'curr_branch'
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
File "/usr/local/Cellar/xonsh/0.5.8/libexec/lib/python3.6/site-packages/xonsh/prompt/__amalgam__.py", line 671, in _get_field_value
value = field_value() if callable(field_value) else field_value
File "/usr/local/Cellar/xonsh/0.5.8/libexec/lib/python3.6/site-packages/xonsh/prompt/__amalgam__.py", line 486, in current_branch
has_git = bool(cmds.locate_binary('git', ignore_alias=True))
File "/usr/local/Cellar/xonsh/0.5.8/libexec/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 7790, in locate_binary
_ = self.all_commands
File "/usr/local/Cellar/xonsh/0.5.8/libexec/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 7733, in all_commands
for cmd in executables_in(path):
File "/usr/local/Cellar/xonsh/0.5.8/libexec/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 5881, in executables_in
yield from func(path)
File "/usr/local/Cellar/xonsh/0.5.8/libexec/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 5849, in _executables_in_posix
yield from _yield_accessible_unix_file_names(path)
File "/usr/local/Cellar/xonsh/0.5.8/libexec/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 5829, in _yield_accessible_unix_file_names
for file_ in scandir(path):
NotADirectoryError: [Errno 20] Not a directory: '/usr/local/bin/jamf'

# Fixed code
def all_commands(self):
    paths = builtins.__xonsh_env__.get("PATH", [])
    paths = CommandsCache.remove_dups(paths)
    path_immut = tuple(x for x in paths if os.path.isdir(x))
    # did PATH change?
    path_hash = hash(path_immut)
    cache_valid = path_hash == self._path_checksum
    self._path_checksum = path_hash
    # did aliases change?
    alss = getattr(builtins, "aliases", dict())
    al_hash = hash(frozenset(alss))
    cache_valid = cache_valid and al_hash == self._alias_checksum
    self._alias_checksum = al_hash
    # did the contents of any directory in PATH change?
    max_mtime = 0
    for path in path_immut:
        mtime = os.stat(path).st_mtime
        if mtime > max_mtime:
            max_mtime = mtime
    cache_valid = cache_valid and (max_mtime <= self._path_mtime)
    self._path_mtime = max_mtime
    if cache_valid:
        return self._cmds_cache
    allcmds = {}
    for path in reversed(path_immut):
        # iterate backwards so that entries at the front of PATH overwrite
        # entries at the back.
        for cmd in executables_in(path):
            key = cmd.upper() if ON_WINDOWS else cmd
            allcmds[key] = (os.path.join(path, cmd), alss.get(key, None))
    for cmd in alss:
        if cmd not in allcmds:
            key = cmd.upper() if ON_WINDOWS else cmd
            allcmds[key] = (cmd, True)
    self._cmds_cache = allcmds
    return allcmds