Skip to content

Commit 37d32f4

Browse files
committed
fix error handling for git dependencies
1 parent b516801 commit 37d32f4

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

src/poetry/installation/executor.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,18 @@ 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 = (
332+
pkg.to_dependency()
333+
.to_pep_508()
334+
.split(";")[0]
335+
.split("@")[1]
336+
.strip()
337+
)
338+
requirement = f"{git_url}#egg={pkg.name}"
339+
else:
340+
assert pkg.source_url
341+
requirement = pkg.source_url
331342
pip_command += " --editable"
332343
else:
333344
requirement = (
@@ -681,7 +692,12 @@ def _prepare_git_archive(self, operation: Install | Update) -> Path:
681692
package.source_subdirectory,
682693
)
683694

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

tests/installation/test_executor.py

+30-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"])
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,51 @@ 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+
if source_type == "directory":
1392+
source_url = fixture_dir("simple_project").resolve().as_posix()
1393+
source_resolved_reference = None
1394+
pip_url = path_to_url(source_url)
1395+
pip_editable_requirement = source_url
1396+
elif source_type == "git":
1397+
source_url = "https://github.com/demo/demo.git"
1398+
source_resolved_reference = "12345678"
1399+
pip_url = f"git+{source_url}"
1400+
pip_editable_requirement = f"{pip_url}#egg={package_name}"
1401+
else:
1402+
raise ValueError(f"Unknown source type: {source_type}")
1403+
package = Package(
13901404
package_name,
13911405
package_version,
1392-
source_type="directory",
1393-
source_url=fixture_dir("simple_project").resolve().as_posix(),
1406+
source_type=source_type,
1407+
source_url=source_url,
1408+
source_resolved_reference=source_resolved_reference,
13941409
develop=editable,
13951410
)
13961411
# must not be included in the error message
1397-
directory_package.python_versions = ">=3.7"
1412+
package.python_versions = ">=3.7"
13981413

1399-
return_code = executor.execute([Install(directory_package)])
1414+
return_code = executor.execute([Install(package)])
14001415

14011416
assert return_code == 1
14021417

1403-
package_url = directory_package.source_url
1404-
1405-
assert directory_package.source_url is not None
1418+
assert package.source_url is not None
14061419
if editable:
14071420
pip_command = "pip wheel --no-cache-dir --use-pep517 --editable"
1408-
requirement = directory_package.source_url
1409-
assert Path(requirement).exists()
1421+
requirement = pip_editable_requirement
1422+
if source_type == "directory":
1423+
assert Path(requirement).exists()
14101424
else:
14111425
pip_command = "pip wheel --no-cache-dir --use-pep517"
1412-
requirement = f"{package_name} @ {path_to_url(directory_package.source_url)}"
1426+
requirement = f"{package_name} @ {pip_url}"
14131427

1414-
expected_source_string = f"{package_name} ({package_version} {package_url})"
1428+
version_details = package.source_resolved_reference or package.source_url
1429+
expected_source_string = f"{package_name} ({package_version} {version_details})"
14151430
expected_pip_command = f'{pip_command} "{requirement}"'
14161431

14171432
expected_output = f"""
14181433
Package operations: 1 install, 0 updates, 0 removals
14191434
1420-
- Installing {package_name} ({package_version} {package_url})
1435+
- Installing {expected_source_string}
14211436
14221437
PEP517 build of a dependency failed
14231438
@@ -1433,7 +1448,8 @@ def test_build_backend_errors_are_reported_correctly_if_caused_by_subprocess(
14331448
)
14341449

14351450
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}
1451+
Note: This error originates from the build backend, and is likely not a problem \
1452+
with poetry but one of the following issues with {expected_source_string}
14371453
14381454
- not supporting PEP 517 builds
14391455
- not specifying PEP 517 build requirements correctly

0 commit comments

Comments
 (0)