Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error when lockfile without metadata entry is encountered #8523

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ def _get_lock_data(self) -> dict[str, Any]:
except tomllib.TOMLDecodeError as e:
raise RuntimeError(f"Unable to read the lock file ({e}).")

# if the lockfile doesn't contain a metadata section at all,
# it probably needs to be rebuilt completely
if "metadata" not in lock_data:
raise RuntimeError(
"The lock file does not have a metadata entry.\n"
"Regenerate the lock file with the `poetry lock` command."
)

metadata = lock_data["metadata"]
lock_version = Version.parse(metadata.get("lock-version", "1.0"))
current_version = Version.parse(self._VERSION)
Expand Down
32 changes: 32 additions & 0 deletions tests/packages/test_locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,38 @@ def test_reading_lock_file_should_raise_an_error_on_invalid_data(
assert "Unable to read the lock file" in str(e.value)


def test_reading_lock_file_should_raise_an_error_on_missing_metadata(
locker: Locker,
) -> None:
content = f"""\
# {GENERATED_COMMENT}

[[package]]
name = "A"
version = "1.0.0"
description = ""
optional = false
python-versions = "*"
files = []

[package.source]
type = "legacy"
url = "https://foo.bar"
reference = "legacy"
"""
with locker.lock.open("w", encoding="utf-8") as f:
f.write(content)

with pytest.raises(RuntimeError) as e:
_ = locker.lock_data

assert (
radoering marked this conversation as resolved.
Show resolved Hide resolved
"The lock file does not have a metadata entry.\nRegenerate the lock file with"
" the `poetry lock` command."
in str(e.value)
)


def test_locking_legacy_repository_package_should_include_source_section(
root: ProjectPackage, locker: Locker
) -> None:
Expand Down