# Buggy code
def color_file(file_path: str, path_stat: os.stat_result) -> (Color, str):
    """Determine color to use for file as ls --color would, given lstat() results and its name.

    Parameters
    ----------
    file_path:
        relative path of file (as user typed it).
    path_stat:
        stat() results for file_path.

    Returns
    -------
        color token, color_key

    Notes
    -----

    * implementation slavishly follows one authority:
      https://github.com/coreutils/coreutils/blob/master/src/ls.c#L4879

    """

    lsc = builtins.__xonsh__.env["LS_COLORS"]
    symlink_target = "ln_target" in lsc  # if ln=target in LS_COLORS
    color_key = "fi"

    # if ln=target, get name of target (next link only) and its lstat_result
    if symlink_target and stat.S_ISLNK(path_stat.st_mode):
        try:
            file_path = os.readlink(
                file_path
            )  # next hop in symlink chain, just like ls
            path_stat = os.lstat(file_path)  # and work with its properties
        except FileNotFoundError:
            color_key = "mi"  # early exit
            ret_color_token = file_color_tokens.get(color_key, Text)
            return ret_color_token, color_key

    mode = path_stat.st_mode

    if stat.S_ISREG(mode):
        if mode & stat.S_ISUID:
            color_key = "su"
        elif mode & stat.S_ISGID:
            color_key = "sg"
        else:
            cap = os_listxattr(file_path, follow_symlinks=False)
            if cap and "security.capability" in cap:  # protect None return on some OS?
                color_key = "ca"
            elif stat.S_IMODE(mode) & (stat.S_IXUSR + stat.S_IXGRP + stat.S_IXOTH):
                color_key = "ex"
            elif path_stat.st_nlink > 1:
                color_key = "mh"
            else:
                color_key = "fi"
    elif stat.S_ISDIR(mode):  # ls --color doesn't colorize sticky or ow if not dirs...
        color_key = "di"
        if (mode & stat.S_ISVTX) and (mode & stat.S_IWOTH):
            color_key = "tw"
        elif mode & stat.S_IWOTH:
            color_key = "ow"
        elif mode & stat.S_ISVTX:
            color_key = "st"
    elif stat.S_ISLNK(mode):
        color_key = "ln"
    elif stat.S_ISFIFO(mode):
        color_key = "pi"
    elif stat.S_ISSOCK(mode):
        color_key = "so"
    elif stat.S_ISBLK(mode):
        color_key = "bd"
    elif stat.S_ISCHR(mode):
        color_key = "cd"
    elif stat.S_ISDOOR(mode):
        color_key = "do"
    else:
        color_key = "or"  # any other type --> orphan

    if color_key == "fi":  # if still normal file -- try color by file extension.
        match = color_file_extension_RE.match(file_path)
        if match:
            ext = "*" + match.group(1)  # look for *.<fileExtension> coloring
            if ext in lsc:
                color_key = ext

    ret_color_token = file_color_tokens.get(color_key, Text)

    return ret_color_token, color_key


# Traceback (ValueError)
Traceback (most recent call last):
File /home/tim/.xonsh/lib64/python3.7/site-packages/xonsh/__amalgam__.py, line 24210, in main
return main_xonsh(args)
File /home/tim/.xonsh/lib64/python3.7/site-packages/xonsh/__amalgam__.py, line 24239, in main_xonsh
shell.shell.cmdloop()
File /home/tim/.xonsh/lib64/python3.7/site-packages/xonsh/ptk_shell/shell.py, line 197, in cmdloop
line = self.singleline(auto_suggest=auto_suggest)
File /home/tim/.xonsh/lib64/python3.7/site-packages/xonsh/ptk_shell/shell.py, line 101, in singleline
self.styler.style_name = env.get(XONSH_COLOR_STYLE)
File /home/tim/.xonsh/lib64/python3.7/site-packages/xonsh/__amalgam__.py, line 17732, in styler
self._styler = XonshStyle(env.get(XONSH_COLOR_STYLE))
File /home/tim/.xonsh/lib64/python3.7/site-packages/xonsh/pyghooks.py, line 345, in __init__
self.style_name = style_name
File /home/tim/.xonsh/lib64/python3.7/site-packages/xonsh/pyghooks.py, line 384, in style_name
color_token = color_token_by_name(xonsh_color, self.styles)
File /home/tim/.xonsh/lib64/python3.7/site-packages/xonsh/pyghooks.py, line 223, in color_token_by_name
pc = color_name_to_pygments_code(xc[0], styles)
File /home/tim/.xonsh/lib64/python3.7/site-packages/xonsh/pyghooks.py, line 143, in color_name_to_pygments_code
if token in styles:
File /usr/lib64/python3.7/collections/__init__.py, line 931, in __contains__
return any(key in m for m in self.maps)
File /usr/lib64/python3.7/collections/__init__.py, line 931, in <genexpr>
return any(key in m for m in self.maps)
File /usr/lib64/python3.7/_collections_abc.py, line 666, in __contains__
self[key]
File /home/tim/.xonsh/lib64/python3.7/site-packages/xonsh/pyghooks.py, line 312, in __getitem__
value = code_by_name(name, self.styles)
File /home/tim/.xonsh/lib64/python3.7/site-packages/xonsh/pyghooks.py, line 205, in code_by_name
code = color_name_to_pygments_code(fg, styles)
File /home/tim/.xonsh/lib64/python3.7/site-packages/xonsh/pyghooks.py, line 147, in color_name_to_pygments_code
raise ValueError(!r is not a color!.format(name))
ValueError: 'TARGET' is not a color!
Xonsh encountered an issue during launch
Failback to /bin/sh

# Fixed code
def color_file(file_path: str, path_stat: os.stat_result) -> (Color, str):
    """Determine color to use for file *approximately* as ls --color would, given lstat() results and its name.

    Parameters
    ----------
    file_path:
        relative path of file (as user typed it).
    path_stat:
        lstat() results for file_path.

    Returns
    -------
        color token, color_key

    Notes
    -----

    * implementation follows one authority:
      https://github.com/coreutils/coreutils/blob/master/src/ls.c#L4879
    * except:
    1. does not return 'mi' symlink to missing file unless ln=target,
      because caller provides an existing file.
    2. in dircolors, setting type code to '0 or '00' bypasses that test and proceeds to others.
       In our implementation, setting code to '00' paints the file with no color.
       This is arguably a bug.
    3. dircolors with ln=target only colors links to fundamental types, not to *.zoo
       (file extension patterns). This function will paint the link with the ultimate file type color.
       This is arguably better for the user typing a filename on the command line.
    """

    lsc = builtins.__xonsh__.env["LS_COLORS"]
    color_key = "fi"

    # if ln=target, get name of target (next link only) and its lstat_result
    if lsc.is_target("ln") and stat.S_ISLNK(path_stat.st_mode):
        try:
            file_path = os.readlink(
                file_path
            )  # next hop in symlink chain, just like ls
            path_stat = os.lstat(file_path)  # and work with its properties
        except FileNotFoundError:
            color_key = "mi"  # early exit
            ret_color_token = file_color_tokens.get(color_key, Text)
            return ret_color_token, color_key

    mode = path_stat.st_mode

    if stat.S_ISREG(mode):
        if mode & stat.S_ISUID:
            color_key = "su"
        elif mode & stat.S_ISGID:
            color_key = "sg"
        else:
            cap = os_listxattr(file_path, follow_symlinks=False)
            if cap and "security.capability" in cap:  # protect None return on some OS?
                color_key = "ca"
            elif stat.S_IMODE(mode) & (stat.S_IXUSR + stat.S_IXGRP + stat.S_IXOTH):
                color_key = "ex"
            elif path_stat.st_nlink > 1:
                color_key = "mh"
            else:
                color_key = "fi"
    elif stat.S_ISDIR(mode):  # ls --color doesn't colorize sticky or ow if not dirs...
        color_key = "di"
        if (mode & stat.S_ISVTX) and (mode & stat.S_IWOTH):
            color_key = "tw"
        elif mode & stat.S_IWOTH:
            color_key = "ow"
        elif mode & stat.S_ISVTX:
            color_key = "st"
    elif stat.S_ISLNK(mode):
        color_key = "ln"
    elif stat.S_ISFIFO(mode):
        color_key = "pi"
    elif stat.S_ISSOCK(mode):
        color_key = "so"
    elif stat.S_ISBLK(mode):
        color_key = "bd"
    elif stat.S_ISCHR(mode):
        color_key = "cd"
    elif stat.S_ISDOOR(mode):
        color_key = "do"
    else:
        color_key = "or"  # any other type --> orphan

    if color_key == "fi":  # if still normal file -- try color by file extension.
        match = color_file_extension_RE.match(file_path)
        if match:
            ext = "*" + match.group(1)  # look for *.<fileExtension> coloring
            if ext in lsc:
                color_key = ext

    ret_color_token = file_color_tokens.get(color_key, Text)

    return ret_color_token, color_key