From 4275a551989c6780f74557540d6704f2437a1576 Mon Sep 17 00:00:00 2001 From: Matthieu Devlin Date: Fri, 2 Jul 2021 10:46:46 -0700 Subject: [PATCH 1/3] fix: create path dependencies relative to package rather than lockfile (#4245) --- poetry/packages/locker.py | 18 +++++++++----- tests/packages/test_locker.py | 44 ++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/poetry/packages/locker.py b/poetry/packages/locker.py index ad61421a93e..84bf2ce242b 100644 --- a/poetry/packages/locker.py +++ b/poetry/packages/locker.py @@ -176,20 +176,26 @@ def locked_repository( package.marker = parse_marker(split_dep[1].strip()) for dep_name, constraint in info.get("dependencies", {}).items(): + + root_dir = self._lock.path.parent + if package.source_type == "directory": + # root dir should be the source of the package relative to the lock path + root_dir = Path( + os.path.relpath( + Path(package.source_url), self._lock.path.parent + ) + ).resolve() + if isinstance(constraint, list): for c in constraint: package.add_dependency( - Factory.create_dependency( - dep_name, c, root_dir=self._lock.path.parent - ) + Factory.create_dependency(dep_name, c, root_dir=root_dir) ) continue package.add_dependency( - Factory.create_dependency( - dep_name, constraint, root_dir=self._lock.path.parent - ) + Factory.create_dependency(dep_name, constraint, root_dir=root_dir) ) if "develop" in info: diff --git a/tests/packages/test_locker.py b/tests/packages/test_locker.py index ed3c9af411d..09d51f62da9 100644 --- a/tests/packages/test_locker.py +++ b/tests/packages/test_locker.py @@ -348,7 +348,7 @@ def test_lock_file_should_not_have_mixed_types(locker, root): def test_reading_lock_file_should_raise_an_error_on_invalid_data(locker): - content = u"""[[package]] + content = """[[package]] name = "A" version = "1.0.0" description = "" @@ -598,3 +598,45 @@ def test_locker_dumps_dependency_information_correctly(locker, root): """ assert expected == content + + +def test_locked_repository_uses_root_dir_of_package(locker, mocker): + content = """\ +[[package]] +name = "lib-a" +version = "0.1.0" +description = "" +category = "main" +optional = false +python-versions = "^2.7.9" +develop = true + +[package.dependencies] +lib-b = {path = "../libB", develop = true} + +[package.source] +type = "directory" +url = "lib/libA" + +[metadata] +lock-version = "1.1" +python-versions = "*" +content-hash = "115cf985d932e9bf5f540555bbdd75decbb62cac81e399375fc19f6277f8c1d8" + +[metadata.files] +lib-a = [] +lib-b = [] +""" + + locker.lock.write(tomlkit.parse(content)) + create_dependency_patch = mocker.patch( + "poetry.factory.Factory.create_dependency", autospec=True + ) + locker.locked_repository() + + create_dependency_patch.assert_called_once_with( + "lib-b", {"develop": True, "path": "../libB"}, root_dir=mocker.ANY + ) + root_dir = create_dependency_patch.call_args.kwargs["root_dir"] + assert root_dir.match("*/lib/libA") + assert root_dir.is_relative_to(locker.lock.path.parent.resolve()) From f0487f6ced32c55fae9439e2aca74ab975408aa1 Mon Sep 17 00:00:00 2001 From: Matthieu Devlin Date: Fri, 2 Jul 2021 11:37:06 -0700 Subject: [PATCH 2/3] test: fix tests for for older versions of python --- tests/packages/test_locker.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/packages/test_locker.py b/tests/packages/test_locker.py index 09d51f62da9..11f1155a162 100644 --- a/tests/packages/test_locker.py +++ b/tests/packages/test_locker.py @@ -637,6 +637,8 @@ def test_locked_repository_uses_root_dir_of_package(locker, mocker): create_dependency_patch.assert_called_once_with( "lib-b", {"develop": True, "path": "../libB"}, root_dir=mocker.ANY ) - root_dir = create_dependency_patch.call_args.kwargs["root_dir"] + call_kwargs = create_dependency_patch.call_args[1] + root_dir = call_kwargs["root_dir"] assert root_dir.match("*/lib/libA") - assert root_dir.is_relative_to(locker.lock.path.parent.resolve()) + # relative_to raises an exception if not relative - is_relative_to comes in py3.9 + assert root_dir.relative_to(locker.lock.path.parent.resolve()) is not None From 97e63f629b9e669740603754f39a67ff1fa166dc Mon Sep 17 00:00:00 2001 From: Matthieu Devlin Date: Tue, 6 Jul 2021 13:18:50 -0700 Subject: [PATCH 3/3] fix: use already calculated relative url --- poetry/packages/locker.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/poetry/packages/locker.py b/poetry/packages/locker.py index 84bf2ce242b..623e2d7745e 100644 --- a/poetry/packages/locker.py +++ b/poetry/packages/locker.py @@ -180,11 +180,7 @@ def locked_repository( root_dir = self._lock.path.parent if package.source_type == "directory": # root dir should be the source of the package relative to the lock path - root_dir = Path( - os.path.relpath( - Path(package.source_url), self._lock.path.parent - ) - ).resolve() + root_dir = Path(package.source_url) if isinstance(constraint, list): for c in constraint: