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

Add more helpful error message when metadata is malformed #695

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions src/build/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def check_dependency(
# dependency is not installed in the environment.
yield (*ancestral_req_strings, normalised_req_string)
else:
if dist.version is None:
# Malformed dist-info or some other form of metadata corruption
# req.specifier.contains will raise TypeError, so let's do the same
# with a more helpful error message
msg = f'Package {req.name} has malformed metadata and no version information could be found'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: so that the name is quoted:

Suggested change
msg = f'Package {req.name} has malformed metadata and no version information could be found'
msg = f'Package {req.name!r} has malformed metadata and no version information could be found'

raise TypeError(msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we shouldn't make this a warning instead and return this package as an unmet dependency. That should give more information to the user. As it stands, they would only see an error message about a specific bad dependency, instead of getting a list of all bad ones with the extra context of why this one was considered bad.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me, sounds reasonable to want all issues at the same time instead of potentially having to address one-by-one

if req.specifier and not req.specifier.contains(dist.version, prereleases=True):
# the installed version is incompatible.
yield (*ancestral_req_strings, normalised_req_string)
Expand Down
15 changes: 14 additions & 1 deletion tests/test_projectbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

DEFAULT_BACKEND = {
'build-backend': 'setuptools.build_meta:__legacy__',
'requires': ['setuptools >= 40.8.0'],
'requires': ['setuptools >= 40.8.0', 'wheel'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a regression. Why is it needed? wheel was required on older setuptools versions, but shouldn't be anymore. If our minimum setuptools version does not yet include the fix to return wheel in get_requires_for_build_wheel, we should update it.

}


Expand All @@ -43,6 +43,8 @@ def from_name(cls, name):
return CircularMockDistribution()
elif name == 'nested_circular_dep':
return NestedCircularMockDistribution()
elif name == 'malformed_dep':
return MalformedMockDistribution()
raise _importlib.metadata.PackageNotFoundError


Expand Down Expand Up @@ -127,6 +129,11 @@ def read_text(self, filename):
"""
).strip()

class MalformedMockDistribution(MockDistribution):
def read_text(self, filename):
if filename == 'METADATA':
return ""


@pytest.mark.parametrize(
('requirement_string', 'expected'),
Expand Down Expand Up @@ -165,6 +172,12 @@ def test_check_dependency(monkeypatch, requirement_string, expected):
monkeypatch.setattr(_importlib.metadata, 'Distribution', MockDistribution)
assert next(build.check_dependency(requirement_string), None) == expected

def test_check_dependency_bad_metadata(monkeypatch):
monkeypatch.setattr(_importlib.metadata, 'Distribution', MockDistribution)
with pytest.raises(TypeError) as excinfo:
next(build.check_dependency("malformed_dep==1.0.0"), None)
assert "Package malformed_dep has malformed metadata and no version information could be found" in str(excinfo)


def test_bad_project(package_test_no_project):
# Passing a nonexistent project directory
Expand Down
Loading