# Buggy code
def gen_python_files_in_dir(
    path: Path,
    root: Path,
    include: Pattern[str],
    exclude: Pattern[str],
    report: "Report",
) -> Iterator[Path]:
    """Generate all files under `path` whose paths are not excluded by the
    `exclude` regex, but are included by the `include` regex.

    `report` is where output about exclusions goes.
    """
    assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}"
    for child in path.iterdir():
        normalized_path = "/" + child.resolve().relative_to(root).as_posix()
        if child.is_dir():
            normalized_path += "/"
        exclude_match = exclude.search(normalized_path)
        if exclude_match and exclude_match.group(0):
            report.path_ignored(child, f"matches the --exclude regular expression")
            continue

        if child.is_dir():
            yield from gen_python_files_in_dir(child, root, include, exclude, report)

        elif child.is_file():
            include_match = include.search(normalized_path)
            if include_match:
                yield child


# Traceback (ValueError)
Traceback (most recent call last):
File "/home/neraste/.virtualenvs/test_black/bin/black", line 11, in <module>
sys.exit(main())
File "/home/neraste/.virtualenvs/test_black/lib/python3.6/site-packages/click/core.py", line 722, in __call__
return self.main(*args, **kwargs)
File "/home/neraste/.virtualenvs/test_black/lib/python3.6/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/home/neraste/.virtualenvs/test_black/lib/python3.6/site-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/neraste/.virtualenvs/test_black/lib/python3.6/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/home/neraste/.virtualenvs/test_black/lib/python3.6/site-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/neraste/.virtualenvs/test_black/lib/python3.6/site-packages/black.py", line 347, in main
gen_python_files_in_dir(p, root, include_regex, exclude_regex, report)
File "/home/neraste/.virtualenvs/test_black/lib/python3.6/site-packages/black.py", line 2942, in gen_python_files_in_dir
normalized_path = "/" + child.resolve().relative_to(root).as_posix()
File "/usr/lib64/python3.6/pathlib.py", line 872, in relative_to
.format(str(self), str(formatted)))
ValueError: '/data/test_black/base/resource' does not start with '/data/test_black/base/repo'

# Fixed code
def gen_python_files_in_dir(
    path: Path,
    root: Path,
    include: Pattern[str],
    exclude: Pattern[str],
    report: "Report",
) -> Iterator[Path]:
    """Generate all files under `path` whose paths are not excluded by the
    `exclude` regex, but are included by the `include` regex.

    Symbolic links pointing outside of the root directory are ignored.

    `report` is where output about exclusions goes.
    """
    assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}"
    for child in path.iterdir():
        try:
            normalized_path = "/" + child.resolve().relative_to(root).as_posix()
        except ValueError:
            if child.is_symlink():
                report.path_ignored(
                    child,
                    "is a symbolic link that points outside of the root directory",
                )
                continue

            raise

        if child.is_dir():
            normalized_path += "/"
        exclude_match = exclude.search(normalized_path)
        if exclude_match and exclude_match.group(0):
            report.path_ignored(child, f"matches the --exclude regular expression")
            continue

        if child.is_dir():
            yield from gen_python_files_in_dir(child, root, include, exclude, report)

        elif child.is_file():
            include_match = include.search(normalized_path)
            if include_match:
                yield child