Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(packaging): Format METADATA correctly if given empty requires_file #2771

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

Merged
merged 12 commits into from
Apr 15, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Unreleased changes template.
* (toolchains) Run the check on the Python interpreter in isolated mode, to ensure it's not affected by userland environment variables, such as `PYTHONPATH`.
* (toolchains) Ensure temporary `.pyc` and `.pyo` files are also excluded from the interpreters repository files.
* (pypi) Run interpreter version call in isolated mode, to ensure it's not affected by userland environment variables, such as `PYTHONPATH`.
* (packaging) An empty `requires_file` is treated as if it were omitted, resulting in a valid `METADATA` file.

{#v0-0-0-added}
### Added
Expand Down
16 changes: 16 additions & 0 deletions examples/wheel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ starlark # Example comment
""".splitlines(),
)

write_file(
name = "empty_requires_file",
out = "empty_requires.txt",
content = [""],
)

write_file(
name = "extra_requires_file",
out = "extra_requires.txt",
Expand Down Expand Up @@ -324,6 +330,15 @@ py_wheel(
deps = [":example_pkg"],
)

py_wheel(
name = "empty_requires_files",
distribution = "empty_requires_files",
python_tag = "py3",
requires_file = ":empty_requires.txt",
version = "0.0.1",
deps = [":example_pkg"],
)

# Package just a specific py_libraries, without their dependencies
py_wheel(
name = "minimal_data_files",
Expand Down Expand Up @@ -367,6 +382,7 @@ py_test(
":custom_package_root_multi_prefix",
":custom_package_root_multi_prefix_reverse_order",
":customized",
":empty_requires_files",
":extra_requires",
":filename_escaping",
":minimal_data_files",
Expand Down
24 changes: 23 additions & 1 deletion examples/wheel/wheel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,6 @@ def test_requires_file_and_extra_requires_files(self):
if line.startswith(b"Requires-Dist:"):
requires.append(line.decode("utf-8").strip())

print(requires)
self.assertEqual(
[
"Requires-Dist: tomli>=2.0.0",
Expand All @@ -495,6 +494,29 @@ def test_requires_file_and_extra_requires_files(self):
requires,
)

def test_empty_requires_file(self):
filename = self._get_path("empty_requires_files-0.0.1-py3-none-any.whl")

with zipfile.ZipFile(filename) as zf:
self.assertAllEntriesHasReproducibleMetadata(zf)
metadata_file = None
for f in zf.namelist():
if os.path.basename(f) == "METADATA":
metadata_file = f
self.assertIsNotNone(metadata_file)

metadata = zf.read(metadata_file).decode("utf-8")
metadata_lines = metadata.splitlines()

requires = []
for i, line in enumerate(metadata_lines):
if line.startswith("Name:"):
self.assertTrue(metadata_lines[i + 1].startswith("Version:"))
if line.startswith("Requires-Dist:"):
requires.append(line.strip())

self.assertEqual([], requires)

def test_minimal_data_files(self):
filename = self._get_path("minimal_data_files-0.0.1-py3-none-any.whl")

Expand Down
5 changes: 5 additions & 0 deletions python/packaging.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ def py_wheel(

Currently only pure-python wheels are supported.

:::{versionchanged} VERSION_NEXT_FEATURE
From now on, an empty `requires_file` is treated as if it were omitted, resulting in a valid
`METADATA` file.
:::

Examples:

```python
Expand Down
7 changes: 6 additions & 1 deletion tools/wheelmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,12 @@ def get_new_requirement_line(reqs_text, extra):

reqs.append(get_new_requirement_line(reqs_text, extra))

metadata = metadata.replace(meta_line, "\n".join(reqs))
if reqs:
metadata = metadata.replace(meta_line, "\n".join(reqs))
# File is empty
# So replace the meta_line entirely, including removing newline chars
else:
metadata = re.sub(re.escape(meta_line) + r"(?:\r?\n)?", "", metadata, count=1)

maker.add_metadata(
metadata=metadata,
Expand Down