diff --git a/src/poetry/installation/executor.py b/src/poetry/installation/executor.py index af9cf784a1e..40edcc354c6 100644 --- a/src/poetry/installation/executor.py +++ b/src/poetry/installation/executor.py @@ -922,7 +922,7 @@ def _create_file_url_reference(self, package: Package) -> dict[str, Any]: assert package.source_url is not None return { - "url": Path(package.source_url).as_uri(), + "url": self._path_to_file_url(package.source_url), "archive_info": archive_info, } @@ -934,10 +934,18 @@ def _create_directory_url_reference(self, package: Package) -> dict[str, Any]: assert package.source_url is not None return { - "url": Path(package.source_url).as_uri(), + "url": self._path_to_file_url(package.source_url), "dir_info": dir_info, } + @staticmethod + def _path_to_file_url(path: str) -> str: + source = Path(path) + if not source.is_absolute(): + source = source.resolve() + + return source.as_uri() + def _get_archive_info(self, package: Package) -> dict[str, Any]: """ Create dictionary `archive_info` for file `direct_url.json`. diff --git a/tests/installation/test_executor.py b/tests/installation/test_executor.py index 08aa1bf294b..baeac76fca0 100644 --- a/tests/installation/test_executor.py +++ b/tests/installation/test_executor.py @@ -949,6 +949,41 @@ def test_executor_should_write_pep610_url_references_for_directories( assert not prepare_spy.spy_return.exists(), "archive not cleaned up" +def test_executor_should_create_pep610_url_reference_for_relative_directory( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + source = tmp_path / "source" + source.mkdir() + monkeypatch.chdir(tmp_path) + + package = Package("demo", "0.1.2", source_type="directory", source_url="source") + + executor = object.__new__(Executor) + + assert executor._create_directory_url_reference(package) == { + "dir_info": {}, + "url": source.as_uri(), + } + + +def test_executor_should_create_pep610_url_reference_for_relative_file( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + source = tmp_path / "source.tar.gz" + source.write_bytes(b"archive") + monkeypatch.chdir(tmp_path) + + package = Package("demo", "0.1.2", source_type="file", source_url="source.tar.gz") + + executor = object.__new__(Executor) + executor._hashes = {} + + assert executor._create_file_url_reference(package) == { + "archive_info": {}, + "url": source.as_uri(), + } + + def test_executor_should_write_pep610_url_references_for_editable_directories( tmp_venv: VirtualEnv, pool: RepositoryPool,