# Buggy code
def vendor_runtime(chroot, dest_basedir, label, root_module_names):
    """Includes portions of vendored distributions in a chroot.

    The portion to include is selected by root module name. If the module is a file, just it is
    included. If the module represents a package, the package and all its sub-packages are added
    recursively.

    :param chroot: The chroot to add vendored code to.
    :type chroot: :class:`pex.common.Chroot`
    :param str dest_basedir: The prefix to store the vendored code under in the ``chroot``.
    :param str label: The chroot label for the vendored code fileset.
    :param root_module_names: The names of the root vendored modules to include in the chroot.
    :type root_module_names: :class:`collections.Iterable` of str
    :raise: :class:`ValueError` if any of the given ``root_module_names`` could not be found amongst
            the vendored code and added to the chroot.
    """
    vendor_module_names = {
        root_module_name: False for root_module_name in root_module_names
    }
    for spec in iter_vendor_specs():
        for root, dirs, files in os.walk(spec.target_dir):
            if root == spec.target_dir:
                dirs[:] = [
                    pkg_name for pkg_name in dirs if pkg_name in vendor_module_names
                ]
                files[:] = [
                    mod_name
                    for mod_name in files
                    if mod_name[:-3] in vendor_module_names
                ]
                vendored_names = dirs + [filename[:-3] for filename in files]
                if vendored_names:
                    pkg_path = ""
                    for pkg in spec.relpath.split(os.sep):
                        pkg_path = os.path.join(pkg_path, pkg)
                        pkg_file = os.path.join(pkg_path, "__init__.py")
                        src = os.path.join(VendorSpec.ROOT, pkg_file)
                        dest = os.path.join(dest_basedir, pkg_file)
                        if os.path.exists(src):
                            chroot.copy(src, dest, label)
                        else:
                            # We delete `pex/vendor/_vendored/<dist>/__init__.py` when isolating third_party.
                            chroot.touch(dest, label)
                    for name in vendored_names:
                        vendor_module_names[name] = True
                        TRACER.log(
                            "Vendoring {} from {} @ {}".format(
                                name, spec, spec.target_dir
                            ),
                            V=3,
                        )

            for filename in files:
                if not filename.endswith(".pyc"):  # Sources and data only.
                    src = os.path.join(root, filename)
                    dest = os.path.join(
                        dest_basedir,
                        spec.relpath,
                        os.path.relpath(src, spec.target_dir),
                    )
                    chroot.copy(src, dest, label)

    if not all(vendor_module_names.values()):
        raise ValueError(
            "Failed to extract {module_names} from:\n\t{specs}".format(
                module_names=", ".join(
                    module
                    for module, written in vendor_module_names.items()
                    if not written
                ),
                specs="\n\t".join(
                    "{} @ {}".format(spec, spec.target_dir)
                    for spec in iter_vendor_specs()
                ),
            )
        )


# Traceback (FileNotFoundError)
Engine traceback:
in select
in pants.core.goals.typecheck.typecheck
in pants.backend.python.typecheck.mypy.rules.mypy_typecheck
in pants.backend.python.typecheck.mypy.rules.mypy_typecheck_partition
in pants.backend.python.util_rules.pex.create_pex
in pants.backend.python.util_rules.pex.find_interpreter (CPython<3.9,<4,>=3.7,>=3.8)
in pants.engine.process.fallible_to_exec_result_or_raise
Traceback (most recent call last):
File "/home/toolchain/.*****/pants/setup/bootstrap-Linux-x86_64/2.1.0.dev0_py38/lib/python3.8/site-packages/pants/engine/process.py", line 241, in fallible_to_exec_result_or_raise
raise ProcessExecutionFailure(
pants.engine.process.ProcessExecutionFailure: Process 'Find interpreter for constraints: CPython<3.9,<4,>=3.7,>=3.8' failed with exit code 1.
stdout:

stderr:
Traceback (most recent call last):
File "/home/toolchain/.*****/pants/named_*****s/pex_root/unzipped_pexes/d7ed954089c1232b9c1a001845a5a384366d3669/.bootstrap/pex/pex.py", line 443, in execute
exit_code = self._wrap_coverage(self._wrap_profiling, self._execute)
File "/home/toolchain/.*****/pants/named_*****s/pex_root/unzipped_pexes/d7ed954089c1232b9c1a001845a5a384366d3669/.bootstrap/pex/pex.py", line 375, in _wrap_coverage
return runner(*args)
File "/home/toolchain/.*****/pants/named_*****s/pex_root/unzipped_pexes/d7ed954089c1232b9c1a001845a5a384366d3669/.bootstrap/pex/pex.py", line 406, in _wrap_profiling
return runner(*args)
File "/home/toolchain/.*****/pants/named_*****s/pex_root/unzipped_pexes/d7ed954089c1232b9c1a001845a5a384366d3669/.bootstrap/pex/pex.py", line 494, in _execute
return self.execute_entry(self._pex_info.entry_point)
File "/home/toolchain/.*****/pants/named_*****s/pex_root/unzipped_pexes/d7ed954089c1232b9c1a001845a5a384366d3669/.bootstrap/pex/pex.py", line 596, in execute_entry
return runner(entry_point)
File "/home/toolchain/.*****/pants/named_*****s/pex_root/unzipped_pexes/d7ed954089c1232b9c1a001845a5a384366d3669/.bootstrap/pex/pex.py", line 612, in execute_pkg_resources
return runner()
File "/home/toolchain/.*****/pants/named_*****s/pex_root/unzipped_pexes/d7ed954089c1232b9c1a001845a5a384366d3669/.deps/pex-2.1.20-py2.py3-none-any.whl/pex/bin/pex.py", line 994, in main
pex_builder.freeze(bytecode_compile=options.compile)
File "/home/toolchain/.*****/pants/named_*****s/pex_root/unzipped_pexes/d7ed954089c1232b9c1a001845a5a384366d3669/.deps/pex-2.1.20-py2.py3-none-any.whl/pex/pex_builder.py", line 505, in freeze
self._prepare_bootstrap()
File "/home/toolchain/.*****/pants/named_*****s/pex_root/unzipped_pexes/d7ed954089c1232b9c1a001845a5a384366d3669/.deps/pex-2.1.20-py2.py3-none-any.whl/pex/pex_builder.py", line 463, in _prepare_bootstrap
vendor.vendor_runtime(
File "/home/toolchain/.*****/pants/named_*****s/pex_root/unzipped_pexes/d7ed954089c1232b9c1a001845a5a384366d3669/.deps/pex-2.1.20-py2.py3-none-any.whl/pex/vendor/__init__.py", line 166, in vendor_runtime
chroot.copy(src, dest, label)
File "/home/toolchain/.*****/pants/named_*****s/pex_root/unzipped_pexes/d7ed954089c1232b9c1a001845a5a384366d3669/.deps/pex-2.1.20-py2.py3-none-any.whl/pex/common.py", line 512, in copy
shutil.copy(src, os.path.join(self.chroot, dst))
File "/usr/local/lib/python3.8/shutil.py", line 415, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/usr/local/lib/python3.8/shutil.py", line 261, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: '/home/toolchain/.*****/pants/named_*****s/pex_root/unzipped_pexes/d7ed954089c1232b9c1a001845a5a384366d3669/.deps/pex-2.1.20-py2.py3-none-any.whl/pex/vendor/_vendored/setuptools/pkg_resources/_vendor/__py*****__/pyparsing.cpython-37.pyc.140210639452464'

# Fixed code
def vendor_runtime(chroot, dest_basedir, label, root_module_names):
    """Includes portions of vendored distributions in a chroot.

    The portion to include is selected by root module name. If the module is a file, just it is
    included. If the module represents a package, the package and all its sub-packages are added
    recursively.

    :param chroot: The chroot to add vendored code to.
    :type chroot: :class:`pex.common.Chroot`
    :param str dest_basedir: The prefix to store the vendored code under in the ``chroot``.
    :param str label: The chroot label for the vendored code fileset.
    :param root_module_names: The names of the root vendored modules to include in the chroot.
    :type root_module_names: :class:`collections.Iterable` of str
    :raise: :class:`ValueError` if any of the given ``root_module_names`` could not be found amongst
            the vendored code and added to the chroot.
    """
    vendor_module_names = {
        root_module_name: False for root_module_name in root_module_names
    }
    for spec in iter_vendor_specs():
        for root, dirs, files in os.walk(spec.target_dir):
            if root == spec.target_dir:
                dirs[:] = [
                    pkg_name for pkg_name in dirs if pkg_name in vendor_module_names
                ]
                files[:] = [
                    mod_name
                    for mod_name in files
                    if mod_name[:-3] in vendor_module_names
                ]
                vendored_names = dirs + [filename[:-3] for filename in files]
                if vendored_names:
                    pkg_path = ""
                    for pkg in spec.relpath.split(os.sep):
                        pkg_path = os.path.join(pkg_path, pkg)
                        pkg_file = os.path.join(pkg_path, "__init__.py")
                        src = os.path.join(VendorSpec.ROOT, pkg_file)
                        dest = os.path.join(dest_basedir, pkg_file)
                        if os.path.exists(src):
                            chroot.copy(src, dest, label)
                        else:
                            # We delete `pex/vendor/_vendored/<dist>/__init__.py` when isolating third_party.
                            chroot.touch(dest, label)
                    for name in vendored_names:
                        vendor_module_names[name] = True
                        TRACER.log(
                            "Vendoring {} from {} @ {}".format(
                                name, spec, spec.target_dir
                            ),
                            V=3,
                        )

            # We copy over sources and data only; no pyc files.
            dirs[:] = filter_pyc_dirs(dirs)
            for filename in filter_pyc_files(files):
                src = os.path.join(root, filename)
                dest = os.path.join(
                    dest_basedir, spec.relpath, os.path.relpath(src, spec.target_dir)
                )
                chroot.copy(src, dest, label)

    if not all(vendor_module_names.values()):
        raise ValueError(
            "Failed to extract {module_names} from:\n\t{specs}".format(
                module_names=", ".join(
                    module
                    for module, written in vendor_module_names.items()
                    if not written
                ),
                specs="\n\t".join(
                    "{} @ {}".format(spec, spec.target_dir)
                    for spec in iter_vendor_specs()
                ),
            )
        )