# Buggy code
def to_requirement(self, dist):
    req = dist.as_requirement()

    markers = OrderedSet()

    # Here we map any wheel python requirement to the equivalent environment marker:
    # See:
    # + https://www.python.org/dev/peps/pep-0345/#requires-python
    # + https://www.python.org/dev/peps/pep-0508/#environment-markers
    python_requires = dist_metadata.requires_python(dist)
    if python_requires:
        markers.update(
            Marker(python_version)
            for python_version in sorted(
                "python_version {operator} {version!r}".format(
                    operator=specifier.operator, version=specifier.version
                )
                for specifier in python_requires
            )
        )

    markers.update(self._markers_by_requirement_key.get(req.key, ()))

    if not markers:
        return req

    if len(markers) == 1:
        marker = next(iter(markers))
        req.marker = marker
        return req

    # We may have resolved with multiple paths to the dependency represented by dist and at least
    # two of those paths had (different) conditional requirements for dist based on environment
    # marker predicates. In that case, since the pip resolve succeeded, the implication is that the
    # environment markers are compatible; i.e.: their intersection selects the target interpreter.
    # Here we make that intersection explicit.
    # See: https://www.python.org/dev/peps/pep-0508/#grammar
    marker = " and ".join("({})".format(marker) for marker in markers)
    return Requirement.parse("{}; {}".format(req, marker))


# Traceback (pex.vendor._vendored.setuptools.pkg_resources.RequirementParseError)
Traceback (most recent call last):
File "/app/file.pex/.bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/_vendor/packaging/requirements.py", line 106, in __init__
File "/app/file.pex/.bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/_vendor/pyparsing.py", line 1654, in parseString
File "/app/file.pex/.bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/_vendor/pyparsing.py", line 1644, in parseString
File "/app/file.pex/.bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/_vendor/pyparsing.py", line 1402, in _parseNoCache
File "/app/file.pex/.bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/_vendor/pyparsing.py", line 3417, in parseImpl
File "/app/file.pex/.bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/_vendor/pyparsing.py", line 1406, in _parseNoCache
File "/app/file.pex/.bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/_vendor/pyparsing.py", line 3205, in parseImpl
pex.vendor._vendored.setuptools.pkg_resources._vendor.pyparsing.ParseException: Expected stringEnd (at char 29), (line:1, col:30)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/app/file.pex/.bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/__init__.py", line 3133, in __init__
File "/app/file.pex/.bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/_vendor/packaging/requirements.py", line 110, in __init__
pex.vendor._vendored.setuptools.pkg_resources.extern.packaging.requirements.InvalidRequirement: Invalid requirement, parse error at "'python_v'"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/app/file.pex/.bootstrap/pex/pex.py", line 394, in execute
File "/app/file.pex/.bootstrap/pex/pex.py", line 92, in _activate
File "/app/file.pex/.bootstrap/pex/environment.py", line 239, in activate
File "/app/file.pex/.bootstrap/pex/environment.py", line 405, in _activate
File "/app/file.pex/.bootstrap/pex/environment.py", line 405, in <listcomp>
File "/app/file.pex/.bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/__init__.py", line 3180, in parse
File "/app/file.pex/.bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/__init__.py", line 3126, in parse_requirements
File "/app/file.pex/.bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/__init__.py", line 3135, in __init__
pex.vendor._vendored.setuptools.pkg_resources.RequirementParseError: Invalid requirement, parse error at "'python_v'"

# Fixed code
def to_requirement(self, dist):
    req = dist.as_requirement()

    # pkg_resources.Distribution.as_requirement returns requirements in one of two forms:
    # 1.) project_name==version
    # 2.) project_name===version
    # The latter form is used whenever the distribution's version is non-standard. In those
    # cases we cannot append environment markers since `===` indicates a raw version string to
    # the right that should not be parsed and instead should be compared literally in full.
    # See:
    # + https://www.python.org/dev/peps/pep-0440/#arbitrary-equality
    # + https://github.com/pantsbuild/pex/issues/940
    operator, _ = req.specs[0]
    if operator == "===":
        return req

    markers = OrderedSet()

    # Here we map any wheel python requirement to the equivalent environment marker:
    # See:
    # + https://www.python.org/dev/peps/pep-0345/#requires-python
    # + https://www.python.org/dev/peps/pep-0508/#environment-markers
    python_requires = dist_metadata.requires_python(dist)
    if python_requires:
        markers.update(
            Marker(python_version)
            for python_version in sorted(
                "python_version {operator} {version!r}".format(
                    operator=specifier.operator, version=specifier.version
                )
                for specifier in python_requires
            )
        )

    markers.update(self._markers_by_requirement_key.get(req.key, ()))

    if not markers:
        return req

    if len(markers) == 1:
        marker = next(iter(markers))
        req.marker = marker
        return req

    # We may have resolved with multiple paths to the dependency represented by dist and at least
    # two of those paths had (different) conditional requirements for dist based on environment
    # marker predicates. In that case, since the pip resolve succeeded, the implication is that the
    # environment markers are compatible; i.e.: their intersection selects the target interpreter.
    # Here we make that intersection explicit.
    # See: https://www.python.org/dev/peps/pep-0508/#grammar
    marker = " and ".join("({})".format(marker) for marker in markers)
    return Requirement.parse("{}; {}".format(req, marker))