Skip to content

Commit c0cb1f9

Browse files
committed
fix error handling for git dependencies
1 parent 13e78ba commit c0cb1f9

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

src/poetry/installation/executor.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,20 @@ def _execute_operation(self, operation: Operation) -> None:
327327
with_trace = False
328328
pip_command = "pip wheel --no-cache-dir --use-pep517"
329329
if pkg.develop:
330-
requirement = pkg.source_url
330+
if pkg.source_type == "git":
331+
git_url_parts = (
332+
pkg.to_dependency()
333+
.to_pep_508()
334+
.split(";", 1)[0]
335+
.split("@", 1)[-1]
336+
.strip()
337+
).split("#", 1)
338+
requirement = f"{git_url_parts[0]}#egg={pkg.name}"
339+
if len(git_url_parts) > 1:
340+
requirement += f"&{git_url_parts[1]}"
341+
else:
342+
assert pkg.source_url
343+
requirement = pkg.source_url
331344
pip_command += " --editable"
332345
else:
333346
requirement = (
@@ -681,7 +694,12 @@ def _prepare_git_archive(self, operation: Install | Update) -> Path:
681694
package.source_subdirectory,
682695
)
683696

684-
archive = self._prepare_archive(operation, output_dir=output_dir)
697+
try:
698+
archive = self._prepare_archive(operation, output_dir=output_dir)
699+
except Exception:
700+
# always reset source_url in case of an error for correct output
701+
package._source_url = original_url
702+
raise
685703
if not package.develop:
686704
package._source_url = original_url
687705

tests/installation/test_executor.py

+46-14
Original file line numberDiff line numberDiff line change
@@ -1367,10 +1367,12 @@ def test_executor_should_be_initialized_with_correct_workers(
13671367
],
13681368
)
13691369
@pytest.mark.parametrize("editable", [False, True])
1370+
@pytest.mark.parametrize("source_type", ["directory", "git", "git subdirectory"])
13701371
def test_build_backend_errors_are_reported_correctly_if_caused_by_subprocess(
13711372
failing_method: str,
13721373
exception: Exception,
13731374
editable: bool,
1375+
source_type: str,
13741376
mocker: MockerFixture,
13751377
config: Config,
13761378
pool: RepositoryPool,
@@ -1386,38 +1388,67 @@ def test_build_backend_errors_are_reported_correctly_if_caused_by_subprocess(
13861388

13871389
package_name = "simple-project"
13881390
package_version = "1.2.3"
1389-
directory_package = Package(
1391+
source_reference: str | None = None
1392+
source_sub_directory: str | None = None
1393+
if source_type == "directory":
1394+
source_url = fixture_dir("simple_project").resolve().as_posix()
1395+
source_resolved_reference = None
1396+
pip_url = path_to_url(source_url)
1397+
pip_editable_requirement = source_url
1398+
elif source_type == "git":
1399+
source_url = "https://github.com/demo/demo.git"
1400+
source_reference = "v2.0"
1401+
source_resolved_reference = "12345678"
1402+
pip_url = f"git+{source_url}@{source_reference}"
1403+
pip_editable_requirement = f"{pip_url}#egg={package_name}"
1404+
elif source_type == "git subdirectory":
1405+
source_type = "git"
1406+
source_sub_directory = "one"
1407+
source_url = "https://github.com/demo/subdirectories.git"
1408+
source_reference = "v2.0"
1409+
source_resolved_reference = "12345678"
1410+
pip_base_url = f"git+{source_url}@{source_reference}"
1411+
pip_url = f"{pip_base_url}#subdirectory={source_sub_directory}"
1412+
pip_editable_requirement = (
1413+
f"{pip_base_url}#egg={package_name}&subdirectory={source_sub_directory}"
1414+
)
1415+
else:
1416+
raise ValueError(f"Unknown source type: {source_type}")
1417+
package = Package(
13901418
package_name,
13911419
package_version,
1392-
source_type="directory",
1393-
source_url=fixture_dir("simple_project").resolve().as_posix(),
1420+
source_type=source_type,
1421+
source_url=source_url,
1422+
source_reference=source_reference,
1423+
source_resolved_reference=source_resolved_reference,
1424+
source_subdirectory=source_sub_directory,
13941425
develop=editable,
13951426
)
13961427
# must not be included in the error message
1397-
directory_package.python_versions = ">=3.7"
1428+
package.python_versions = ">=3.7"
13981429

1399-
return_code = executor.execute([Install(directory_package)])
1430+
return_code = executor.execute([Install(package)])
14001431

14011432
assert return_code == 1
14021433

1403-
package_url = directory_package.source_url
1404-
1405-
assert directory_package.source_url is not None
1434+
assert package.source_url is not None
14061435
if editable:
14071436
pip_command = "pip wheel --no-cache-dir --use-pep517 --editable"
1408-
requirement = directory_package.source_url
1409-
assert Path(requirement).exists()
1437+
requirement = pip_editable_requirement
1438+
if source_type == "directory":
1439+
assert Path(requirement).exists()
14101440
else:
14111441
pip_command = "pip wheel --no-cache-dir --use-pep517"
1412-
requirement = f"{package_name} @ {path_to_url(directory_package.source_url)}"
1442+
requirement = f"{package_name} @ {pip_url}"
14131443

1414-
expected_source_string = f"{package_name} ({package_version} {package_url})"
1444+
version_details = package.source_resolved_reference or package.source_url
1445+
expected_source_string = f"{package_name} ({package_version} {version_details})"
14151446
expected_pip_command = f'{pip_command} "{requirement}"'
14161447

14171448
expected_output = f"""
14181449
Package operations: 1 install, 0 updates, 0 removals
14191450
1420-
- Installing {package_name} ({package_version} {package_url})
1451+
- Installing {expected_source_string}
14211452
14221453
PEP517 build of a dependency failed
14231454
@@ -1433,7 +1464,8 @@ def test_build_backend_errors_are_reported_correctly_if_caused_by_subprocess(
14331464
)
14341465

14351466
expected_output += f"""
1436-
Note: This error originates from the build backend, and is likely not a problem with poetry but one of the following issues with {expected_source_string}
1467+
Note: This error originates from the build backend, and is likely not a problem \
1468+
with poetry but one of the following issues with {expected_source_string}
14371469
14381470
- not supporting PEP 517 builds
14391471
- not specifying PEP 517 build requirements correctly

0 commit comments

Comments
 (0)