Skip to content

hatch build no longer finds dependencies for build hook #2140

@unflxw

Description

@unflxw

We have a custom build hook defined as:

[tool.hatch.build.targets.wheel.hooks.custom]
path = "src/scripts/build_hook.py"
dependencies = ["requests"]

The build_hook.py file imports requests as part of its behaviour.

This worked fine when running hatch build -t wheel on Hatch 1.15.1 and Hatchling 1.27.0 (CI link) but fails on Hatch 1.16.2 and Hatchling 1.28.0 (CI link) with the following error:

Full traceback from CI job
app-1    | Traceback (most recent call last):
app-1    |   File "<frozen runpy>", line 198, in _run_module_as_main
app-1    |   File "<frozen runpy>", line 88, in _run_code
app-1    |   File "/root/.local/share/hatch/env/virtual/appsignal/6Y_oALkB/hatch-build/lib/python3.11/site-packages/hatchling/__main__.py", line 6, in <module>
app-1    |     sys.exit(hatchling())
app-1    |              ^^^^^^^^^^^
app-1    |   File "/root/.local/share/hatch/env/virtual/appsignal/6Y_oALkB/hatch-build/lib/python3.11/site-packages/hatchling/cli/__init__.py", line 26, in hatchling
app-1    |     command(**kwargs)
app-1    |   File "/root/.local/share/hatch/env/virtual/appsignal/6Y_oALkB/hatch-build/lib/python3.11/site-packages/hatchling/cli/build/__init__.py", line 82, in build_impl
app-1    |     for artifact in builder.build(
app-1    |   File "/root/.local/share/hatch/env/virtual/appsignal/6Y_oALkB/hatch-build/lib/python3.11/site-packages/hatchling/builders/plugin/interface.py", line 118, in build
app-1    |     configured_build_hooks = self.get_build_hooks(directory)
app-1    |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app-1    |   File "/root/.local/share/hatch/env/virtual/appsignal/6Y_oALkB/hatch-build/lib/python3.11/site-packages/hatchling/builders/plugin/interface.py", line 390, in get_build_hooks
app-1    |     configured_build_hooks[hook_name] = build_hook(
app-1    |                                         ^^^^^^^^^^^
app-1    |   File "/root/.local/share/hatch/env/virtual/appsignal/6Y_oALkB/hatch-build/lib/python3.11/site-packages/hatchling/builders/hooks/custom.py", line 35, in __new__
app-1    |     hook_class = load_plugin_from_script(path, build_script, BuildHookInterface, "build_hook")
app-1    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app-1    |   File "/root/.local/share/hatch/env/virtual/appsignal/6Y_oALkB/hatch-build/lib/python3.11/site-packages/hatchling/plugin/utils.py", line 18, in load_plugin_from_script
app-1    |     spec.loader.exec_module(module)  # type: ignore[union-attr]
app-1    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app-1    |   File "<frozen importlib._bootstrap_external>", line 940, in exec_module
app-1    |   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
app-1    |   File "/integration/src/scripts/build_hook.py", line 13, in <module>
app-1    |     import requests
app-1    | ModuleNotFoundError: No module named 'requests'

Comparing the two CI runs, it seems that it now creates an environment named hatch-build to run the build hook in, rather than the build environment it used before. Adding the following (as suggested in #2113 (comment)) fixed the issue locally:

[tool.hatch.envs.build]
builder = true

However, on our CI environments, we also needed to, in addition to specifying requests as a dependency for the build hook, do so for the hatch-build environment as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions