diff --git a/news/2178.fixed.md b/news/2178.fixed.md new file mode 100644 index 0000000000..2c62c88c85 --- /dev/null +++ b/news/2178.fixed.md @@ -0,0 +1,3 @@ +(pypi) Preserve bytecode package initializers explicitly included as +`__init__.pyc` in wheel data +([#2178](https://github.com/bazel-contrib/rules_python/issues/2178)). diff --git a/python/private/pypi/attrs.bzl b/python/private/pypi/attrs.bzl index 271c17e35c..0cd1064c8d 100644 --- a/python/private/pypi/attrs.bzl +++ b/python/private/pypi/attrs.bzl @@ -39,6 +39,11 @@ and py_test targets must specify either `legacy_create_init=False` or the global `--incompatible_default_to_explicit_init_py` to prevent `__init__.py` being automatically generated in every directory. This option is required to support some packages which cannot handle the conversion to pkg-util style. + +:::{versionchanged} VERSION_NEXT_PATCH +Namespace conversion recognizes `__init__.pyc` as the package initializer +when it is explicitly included in wheel data. +::: """, ), "environment": attr.string_dict( diff --git a/python/private/pypi/namespace_pkgs.bzl b/python/private/pypi/namespace_pkgs.bzl index d58d1f2094..deda922e23 100644 --- a/python/private/pypi/namespace_pkgs.bzl +++ b/python/private/pypi/namespace_pkgs.bzl @@ -36,9 +36,12 @@ def get_files(*, srcs, ignored_dirnames = [], root = None): _add_all(root, ignored) for file in srcs: - dirname, _, filename = file.rpartition("/") + # The wheel BUILD file is at the repository root. Compare both label + # forms and glob results as paths relative to that root. + path = file.removeprefix("//:").removeprefix(":") + dirname, _, filename = path.rpartition("/") - if filename == "__init__.py": + if filename in ("__init__.py", "__init__.pyc"): ignored[dirname] = None dirname, _, _ = dirname.rpartition("/") elif filename.endswith(_ext.py): diff --git a/tests/pypi/namespace_pkgs/namespace_pkgs_tests.bzl b/tests/pypi/namespace_pkgs/namespace_pkgs_tests.bzl index 391b13fd66..5b77d158d0 100644 --- a/tests/pypi/namespace_pkgs/namespace_pkgs_tests.bzl +++ b/tests/pypi/namespace_pkgs/namespace_pkgs_tests.bzl @@ -124,6 +124,8 @@ _tests.append(test_parent_child_relationship_of_namespace_and_nested_standard_pk def test_recognized_all_nonstandard_module_types(env): srcs = [ "ayy/my_module.pyc", + "ayy/regular/__init__.pyc", + "ayy/regular/other.pyc", "bee/ccc/dee/eee.so", "eff/jee/aych.pyd", ] @@ -141,6 +143,19 @@ def test_recognized_all_nonstandard_module_types(env): _tests.append(test_recognized_all_nonstandard_module_types) +def test_bytecode_initializer_file_labels(env): + srcs = [ + "site-packages/relative/module.py", + ":site-packages/relative/__init__.pyc", + "site-packages/absolute/module.py", + "//:site-packages/absolute/__init__.pyc", + "site-packages/namespace/module.py", + ] + got = get_files(srcs = srcs, root = "site-packages") + env.expect.that_collection(got).contains_exactly(["site-packages/namespace"]) + +_tests.append(test_bytecode_initializer_file_labels) + def test_skips_ignored_directories(env): srcs = [ "root/foo/boo/my_module.py",