fix(pypi): parse the whl METADATA with CRLF line endings (#4491) Some wheels ship the `METADATA` file with CRLF line endings. The parser split the contents on `\n` only, so the `\r` was kept at the end of every line. As a result the empty line that ends the headers was not detected and the trailing `\r` ended up in the `Requires-Dist` and `Provides-Extra` values, e.g. the requested extras did not match the extras provided by the package. With this change the lines are split with `splitlines()` and all of the surrounding whitespace is stripped from the values.
diff --git a/news/4491.fixed.md b/news/4491.fixed.md new file mode 100644 index 0000000..713e4d1 --- /dev/null +++ b/news/4491.fixed.md
@@ -0,0 +1,2 @@ +(pypi) The wheel `METADATA` files with CRLF line endings are now parsed +correctly.
diff --git a/python/private/pypi/whl_metadata.bzl b/python/private/pypi/whl_metadata.bzl index 2981a5d..203caaf 100644 --- a/python/private/pypi/whl_metadata.bzl +++ b/python/private/pypi/whl_metadata.bzl
@@ -56,7 +56,7 @@ "requires_dist": [], "version": "", } - for line in contents.strip().split("\n"): + for line in contents.strip().splitlines(): if not line: # Stop parsing on first empty line, which marks the end of the # headers containing the metadata. @@ -70,10 +70,10 @@ parsed["version"] = value.strip() elif line.startswith(_REQUIRES_DIST): _, _, value = line.partition(_REQUIRES_DIST) - parsed["requires_dist"].append(value.strip(" ")) + parsed["requires_dist"].append(value.strip()) elif line.startswith(_PROVIDES_EXTRA): _, _, value = line.partition(_PROVIDES_EXTRA) - parsed["provides_extra"].append(value.strip(" ")) + parsed["provides_extra"].append(value.strip()) return struct( name = parsed["name"],
diff --git a/tests/pypi/whl_metadata/whl_metadata_tests.bzl b/tests/pypi/whl_metadata/whl_metadata_tests.bzl index 8131b0f..8aca204 100644 --- a/tests/pypi/whl_metadata/whl_metadata_tests.bzl +++ b/tests/pypi/whl_metadata/whl_metadata_tests.bzl
@@ -246,6 +246,27 @@ _tests.append(_test_parse_entry_points_deduplicate) +def _test_parse_metadata_crlf(env): + got = _parse_whl_metadata( + env, + contents = "\r\n".join([ + "Name: foo", + "Version: 0.0.1", + "Requires-Dist: bar; extra == \"all\"", + "Provides-Extra: all", + "", + "Requires-Dist: this will be ignored", + ]), + ) + got.name().equals("foo") + got.version().equals("0.0.1") + got.requires_dist().contains_exactly([ + "bar; extra == \"all\"", + ]) + got.provides_extra().contains_exactly(["all"]) + +_tests.append(_test_parse_metadata_crlf) + def whl_metadata_test_suite(name): # buildifier: disable=function-docstring test_suite( name = name,