Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repo: handle invalid distributions gracefully #5645

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/poetry/repositories/installed_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import itertools
import json
import logging

from pathlib import Path
from typing import TYPE_CHECKING
Expand All @@ -28,6 +29,9 @@
FileNotFoundError = OSError


logger = logging.getLogger(__name__)


class InstalledRepository(Repository):
@classmethod
def get_package_paths(cls, env: Env, name: str) -> set[Path]:
Expand Down Expand Up @@ -233,20 +237,41 @@ def load(cls, env: Env, with_dependencies: bool = False) -> InstalledRepository:

repo = cls()
seen = set()
skipped = set()

for entry in reversed(env.sys_path):
if not entry.strip():
logger.debug(
"Project environment contains an empty path in <c1>sys_path</>,"
" ignoring."
)
continue

for distribution in sorted(
metadata.distributions( # type: ignore[no-untyped-call]
path=[entry],
),
key=lambda d: str(d._path), # type: ignore[attr-defined]
):
name = canonicalize_name(distribution.metadata["name"])
path = Path(str(distribution._path)) # type: ignore[attr-defined]
abn marked this conversation as resolved.
Show resolved Hide resolved

if name in seen:
if path in skipped:
continue

path = Path(str(distribution._path)) # type: ignore[attr-defined]
try:
name = canonicalize_name(distribution.metadata["name"])
except TypeError:
logger.warning(
"Project environment contains an invalid distribution"
" (<c1>%s</>). Consider removing it manually or recreate the"
" environment.",
path,
)
skipped.add(path)
continue

if name in seen:
continue

try:
path.relative_to(_VENDORS)
Expand Down
46 changes: 38 additions & 8 deletions tests/repositories/test_installed_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@


if TYPE_CHECKING:
from _pytest.logging import LogCaptureFixture
from poetry.core.packages.package import Package
from pytest_mock.plugin import MockerFixture

Expand Down Expand Up @@ -56,29 +57,37 @@ def paths(self) -> dict[str, Path]:
}

@property
def sys_path(self) -> list[Path]:
return [ENV_DIR, SITE_PLATLIB, SITE_PURELIB]
def sys_path(self) -> list[str]:
return [str(path) for path in [ENV_DIR, SITE_PLATLIB, SITE_PURELIB]]


@pytest.fixture
def env() -> MockEnv:
return MockEnv(path=ENV_DIR)


@pytest.fixture
def repository(mocker: MockerFixture, env: MockEnv) -> InstalledRepository:
mocker.patch(
"poetry.utils._compat.metadata.Distribution.discover",
return_value=INSTALLED_RESULTS,
)
@pytest.fixture(autouse=True)
def mock_git_info(mocker: MockerFixture) -> None:
mocker.patch(
"poetry.vcs.git.Git.info",
return_value=namedtuple("GitRepoLocalInfo", "origin revision")(
origin="https://github.com/sdispater/pendulum.git",
revision="bb058f6b78b2d28ef5d9a5e759cfa179a1a713d6",
),
)


@pytest.fixture(autouse=True)
def mock_installed_repository_vendors(mocker: MockerFixture) -> None:
mocker.patch("poetry.repositories.installed_repository._VENDORS", str(VENDOR_DIR))


@pytest.fixture
def repository(mocker: MockerFixture, env: MockEnv) -> InstalledRepository:
mocker.patch(
"poetry.utils._compat.metadata.Distribution.discover",
return_value=INSTALLED_RESULTS,
)
return InstalledRepository.load(env)


Expand All @@ -95,6 +104,27 @@ def test_load_successful(repository: InstalledRepository):
assert len(repository.packages) == len(INSTALLED_RESULTS) - 1


def test_load_successful_with_invalid_distribution(
caplog: LogCaptureFixture, mocker: MockerFixture, env: MockEnv, tmp_dir: str
) -> None:
invalid_dist_info = Path(tmp_dir) / "site-packages" / "invalid-0.1.0.dist-info"
invalid_dist_info.mkdir(parents=True)
mocker.patch(
"poetry.utils._compat.metadata.Distribution.discover",
return_value=INSTALLED_RESULTS + [metadata.PathDistribution(invalid_dist_info)],
)
repository_with_invalid_distribution = InstalledRepository.load(env)

assert (
len(repository_with_invalid_distribution.packages) == len(INSTALLED_RESULTS) - 1
)
assert len(caplog.messages) == 1

message = caplog.messages[0]
assert message.startswith("Project environment contains an invalid distribution")
assert str(invalid_dist_info) in message


def test_load_ensure_isolation(repository: InstalledRepository):
package = get_package_from_repository("attrs", repository)
assert package is None
Expand Down