From 9eedb4dc58fba71091625c15e6fe481df49bbde0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sat, 13 Jul 2024 23:30:07 +0200 Subject: [PATCH] installed repository: improve recognition of installed packages' types (especially relevant for installing project plugins if Poetry has been installed via pipx) (#9547) --- .../repositories/installed_repository.py | 4 +-- .../repositories/test_installed_repository.py | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/poetry/repositories/installed_repository.py b/src/poetry/repositories/installed_repository.py index 73231d9d9bf..eca051bd251 100644 --- a/src/poetry/repositories/installed_repository.py +++ b/src/poetry/repositories/installed_repository.py @@ -9,6 +9,7 @@ from packaging.utils import canonicalize_name from poetry.core.packages.package import Package +from poetry.core.packages.utils.utils import is_python_project from poetry.core.packages.utils.utils import url_to_path from poetry.core.utils.helpers import module_name @@ -150,8 +151,7 @@ def create_package_from_distribution( ) = cls.get_package_vcs_properties_from_path( env.path / "src" / canonicalize_name(distribution.metadata["name"]) ) - else: - # If not, it's a path dependency + elif is_python_project(path.parent): source_type = "directory" source_url = str(path.parent) diff --git a/tests/repositories/test_installed_repository.py b/tests/repositories/test_installed_repository.py index aa083eb82a6..cb05ac85786 100644 --- a/tests/repositories/test_installed_repository.py +++ b/tests/repositories/test_installed_repository.py @@ -423,3 +423,30 @@ def test_system_site_packages_source_type( package.name: package.source_type for package in installed_repository.packages } assert source_types == {"cleo": None, "directory-pep-610": "directory"} + + +def test_pipx_shared_lib_site_packages( + tmp_path: Path, + poetry: Poetry, + site_purelib: Path, + caplog: LogCaptureFixture, +) -> None: + """ + Simulate pipx shared/lib/site-packages which is not relative to the venv path. + """ + venv_path = tmp_path / "venv" + shared_lib_site_path = tmp_path / "site" + env = MockEnv( + path=venv_path, sys_path=[str(venv_path / "purelib"), str(shared_lib_site_path)] + ) + dist_info = "cleo-0.7.6.dist-info" + shutil.copytree(site_purelib / dist_info, shared_lib_site_path / dist_info) + installed_repository = InstalledRepository.load(env) + + assert len(installed_repository.packages) == 1 + cleo_package = installed_repository.packages[0] + cleo_package.to_dependency() + # There must not be a warning + # that the package does not seem to be a valid Python package. + assert caplog.messages == [] + assert cleo_package.source_type is None