Repository: pypa/twine

Issue:
check: Error message No METADATA in archive when it's there
### Is there an existing issue for this?

- [X] I have searched the existing issues (open and closed), and could not find an existing issue

### What keywords did you use to search existing issues?

No Metadata in archive

### What operating system are you using?

macOS

### If you selected 'Other', describe your Operating System here

_No response_

### What version of Python are you running?

```console
3.12.3
```


### How did you install twine? Did you use your operating system's package manager or pip or something else?

```console
pipx install twine
```


### What version of twine do you have installed (include the complete output)

```console
5.0.0
```


### Which package repository are you using?

pypi.org

### Please describe the issue that you are experiencing

When running twine check on the attached wheel, it reports No METADATA in archive, even though it does have a METADATA file. Probably the issue is that the METADATA file is missing the requisite Metadata-Version field.

[coherent_build-0.0.1.dev0-py3-none-any.whl.zip](https://github.com/pypa/twine/files/15190464/coherent_build-0.0.1.dev0-py3-none-any.whl.zip)

Better would be to report an error message that indicates the true cause of the problem.


### Please list the steps required to reproduce this behaviour

Build an invalid wheel without minimal metadata. Run `twine check` on it.

### Anything else you'd like to mention?

_No response_


Discussion:
pkginfo has the [same issue](https://bugs.launchpad.net/pkginfo/+bug/2064652).
This needs to be fixed in pkginfo. I don't believe we can fix it ourselves 
The logic also exists in twine:

https://github.com/pypa/twine/blob/5d17a43dec622d6f4fc490937baad3db4b9a8e29/twine/wheel.py#L76-L85



Patch:
diff --git a/twine/wheel.py b/twine/wheel.py
index 7af1d804..a2a8ba8a 100644
--- a/twine/wheel.py
+++ b/twine/wheel.py
@@ -73,16 +73,21 @@ def read_file(name: str) -> bytes:
                 "Not a known archive format for file: %s" % fqn
             )
 
+        searched_files: List[str] = []
         try:
             for path in self.find_candidate_metadata_files(names):
                 candidate = "/".join(path)
                 data = read_file(candidate)
                 if b"Metadata-Version" in data:
                     return data
+                searched_files.append(candidate)
         finally:
             archive.close()
 
-        raise exceptions.InvalidDistribution("No METADATA in archive: %s" % fqn)
+        raise exceptions.InvalidDistribution(
+            "No METADATA in archive or METADATA missing 'Metadata-Version': "
+            "%s (searched %s)" % (fqn, ",".join(searched_files))
+        )
 
     def parse(self, data: bytes) -> None:
         super().parse(data)


Test patch:
diff --git a/tests/test_wheel.py b/tests/test_wheel.py
index 485c89f0..eae3bd95 100644
--- a/tests/test_wheel.py
+++ b/tests/test_wheel.py
@@ -96,6 +96,8 @@ def test_read_wheel_empty_metadata(tmpdir):
 
     with pytest.raises(
         exceptions.InvalidDistribution,
-        match=re.escape(f"No METADATA in archive: {whl_file}"),
+        match=re.escape(
+            f"No METADATA in archive or METADATA missing 'Metadata-Version': {whl_file}"
+        ),
     ):
         wheel.Wheel(whl_file)