Regression: hatch fails on import in build hook with require-runtime-dependencies
I have a project that runs a simple python script as part of the wheel build, that started to fail after updating to hatch 1.16.0.
I created a simple project to easily reproduce the problem with a dummy src folder and two relevant files:
pyproject.toml
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "foobar"
description = ""
requires-python = ">=3.13"
keywords = []
version = "0.0.1"
dependencies = [
"django"
]
[tool.hatch.build.targets.wheel.hooks.custom]
require-runtime-dependencies = true
path = "build.py"
build.py
from typing import Any
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
class CustomBuildHook(BuildHookInterface):
PLUGIN_NAME = "CustomBuild"
def initialize(self, version: str, build_data: dict[str, Any]) -> None:
import django
print(f"{django=}")
when I run this with hatch 1.15.1, I get:
──────────────────────────────────── sdist ─────────────────────────────────────
dist/foobar-0.0.1.tar.gz
──────────────────────────────────── wheel ─────────────────────────────────────
django=<module 'django' from '/home/olivier/.local/share/hatch/env/virtual/foobar/3gFOuOmh/foobar-build/lib/python3.13/site-packages/django/__init__.py'>
dist/foobar-0.0.1-py3-none-any.whl
when I run this with hatch 1.16.0, I get :
──────────────────────────────────── sdist ─────────────────────────────────────
dist/foobar-0.0.1.tar.gz
──────────────────────────────────── wheel ─────────────────────────────────────
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/root/.local/share/hatch/env/virtual/foobar/6gE1vKXj/hatch-build/lib/python3.14/site-packages/hatchling/__main__.py", line 6, in <module>
sys.exit(hatchling())
~~~~~~~~~^^
File "/root/.local/share/hatch/env/virtual/foobar/6gE1vKXj/hatch-build/lib/python3.14/site-packages/hatchling/cli/__init__.py", line 26, in hatchling
command(**kwargs)
~~~~~~~^^^^^^^^^^
File "/root/.local/share/hatch/env/virtual/foobar/6gE1vKXj/hatch-build/lib/python3.14/site-packages/hatchling/cli/build/__init__.py", line 82, in build_impl
for artifact in builder.build(
~~~~~~~~~~~~~^
directory=directory,
^^^^^^^^^^^^^^^^^^^^
...<4 lines>...
clean_only=clean_only,
^^^^^^^^^^^^^^^^^^^^^^
):
^
File "/root/.local/share/hatch/env/virtual/foobar/6gE1vKXj/hatch-build/lib/python3.14/site-packages/hatchling/builders/plugin/interface.py", line 149, in build
build_hook.initialize(version, build_data)
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
File "/project/build.py", line 10, in initialize
import django
ModuleNotFoundError: No module named 'django'
It looks like the require-runtime-dependencies = true flag is not taken into account.
Does the issue still exist if you flag the environment with builder = true?
I had a bit of try/error session to test various combinations. I tested following variations :
[tool.hatch.envs.default]
builder = true
> hatch build
...
ModuleNotFoundError: No module named 'django'
[tool.hatch.envs.hatch-build]
builder = true
> hatch build
...
ModuleNotFoundError: No module named 'django'
[tool.hatch.envs.foobar]
> hatch -e foobar build
Environment `foobar` is not a builder environment
[tool.hatch.envs.foobar]
builder = true
> hatch -e foobar build
──────────────────────────────────── sdist ─────────────────────────────────────
dist/foobar-0.0.1.tar.gz
──────────────────────────────────── wheel ─────────────────────────────────────
django=<module 'django' from '/root/.local/share/hatch/env/virtual/foobar/6gE1vKXj/foobar/lib/python3.14/site-packages/django/__init__.py'>
dist/foobar-0.0.1-py3-none-any.whl
Looks like the default hatch-build environment is considered as a build one, but ignore the require-runtime-dependencies flag.
I tried a git bisect today. Looks like the commit that introduced the issue is the one introducing workspace support #2073 If I find a bit of time later this week, I'll try to find the source of the issue.
For reference this seems to affect https://github.com/jupyterlab/hatch-jupyter-builder, as discussed in https://github.com/jupyterlab/hatch-jupyter-builder/issues/146.
Making the hatch build command fail with the following error:
hatchling.plugin.exceptions.UnknownPluginError: Unknown build hook: jupyter-builder
Is there something that needs to be updated in the hatch-jupyter-builder plugin? Or should we expect all users of hatch-jupyter-builder to now specify the plugin in their build environment?
[build-system]
requires = [
"hatchling>=1.11",
+ "hatch-jupyter-builder>=0.5",
]
build-backend = "hatchling.build"