diff --git a/src/hatch_nativelib/plugin.py b/src/hatch_nativelib/plugin.py index 3684e08..ae4948a 100644 --- a/src/hatch_nativelib/plugin.py +++ b/src/hatch_nativelib/plugin.py @@ -258,7 +258,8 @@ def _write_libinit_py( # TODO: should this be a fatal error if r.returncode == 0: module = r.stdout.decode("utf-8").strip() # type: ignore[arg-type, union-attr] - contents.append(f"import {module}") + if module: + contents.append(f"import {module}") if contents[-1] != "": contents.append("") diff --git a/tests/packages/demo-empty-init-dependency/pyproject.toml b/tests/packages/demo-empty-init-dependency/pyproject.toml new file mode 100644 index 0000000..6ad3ab0 --- /dev/null +++ b/tests/packages/demo-empty-init-dependency/pyproject.toml @@ -0,0 +1,22 @@ +[build-system] +requires = ["hatchling", "hatch-nativelib"] +build-backend = "hatchling.build" + +[project] +name = "demo-empty-init-dependency" +version = "1.2.3" +description = "Demo empty initializer dependency" + +[tool.hatch.build.targets.wheel] +packages = ["src/demo_pkg"] + +[[tool.hatch.build.hooks.nativelib.pcfile]] +pcfile = "src/demo_pkg/virtual.pc" +name = "virtual" + +[[tool.hatch.build.hooks.nativelib.pcfile]] +pcfile = "src/demo_pkg/consumer.pc" +name = "consumer" +libdir = "src/demo_pkg/lib" +shared_libraries = ["consumer"] +requires = ["virtual"] diff --git a/tests/packages/demo-empty-init-dependency/src/demo_pkg/__init__.py b/tests/packages/demo-empty-init-dependency/src/demo_pkg/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_integration_build_backend.py b/tests/test_integration_build_backend.py index cfd15bb..2f4687e 100644 --- a/tests/test_integration_build_backend.py +++ b/tests/test_integration_build_backend.py @@ -119,3 +119,17 @@ def test_build_resolves_intra_project_requires_on_first_run(project_factory) -> assert result.returncode == 0, result.stdout + result.stderr init_module = project_factory.read("src/demo_pkg/_init_consumer.py") assert "import demo_pkg._init_provider" in init_module + + +def test_build_skips_requirement_without_init_module(project_factory) -> None: + project_factory.copy_package_fixture("demo-empty-init-dependency") + project_factory.write( + f"src/demo_pkg/lib/{shared_library_filename('consumer')}", + "not-a-real-library", + ) + + result = project_factory.build_wheel() + + assert result.returncode == 0, result.stdout + result.stderr + init_module = project_factory.read("src/demo_pkg/_init_consumer.py") + compile(init_module, "_init_consumer.py", "exec")