Skip to content

Fix require-runtime-dependencies being ignored for build hooks#2257

Open
filbranden wants to merge 3 commits into
pypa:masterfrom
filbranden:cacheinvalidate1
Open

Fix require-runtime-dependencies being ignored for build hooks#2257
filbranden wants to merge 3 commits into
pypa:masterfrom
filbranden:cacheinvalidate1

Conversation

@filbranden

Copy link
Copy Markdown

PR #2073 (workspace support) introduced a separate additional_dependencies list on EnvironmentInterface and changed prepare_build_environment to extend that list with the dynamic build-hook dependencies discovered by get_required_build_deps (e.g. project runtime deps when a build hook sets require-runtime-dependencies = true).

Bug: by the time prepare_build_environment extends additional_dependencies, the cached property chain that consumes it: dependencies_complex -> all_dependencies_complex -> missing_dependencies has already been populated by the preceding prepare_environment call (via dependency_hash() and dependencies_in_sync()) with the empty additional_dependencies snapshot.

The second sync_dependencies() call then early-returns because the cached missing_dependencies reflects the pre-mutation deps which have just been installed — so the runtime deps never get installed and the build hook fails with ModuleNotFoundError.

Before PR #2073, this worked because the old code mutated the cached dependencies list in place and the old sync_dependencies reinstalled the entire list via pip — there was no cached missing_dependencies short-circuit.

Fix: add EnvironmentInterface.add_additional_dependencies() which extends additional_dependencies and invalidates the dependent cached properties (dependencies_complex, dependencies, all_dependencies_complex, all_dependencies, missing_dependencies). Update prepare_build_environment to use it.

Add a regression test that primes the cache before extending and asserts the new dep is visible.

Fixes: #2110

@filbranden filbranden changed the title Fix require-runtime-dependencies being ignored for build hooks (#2110) Fix require-runtime-dependencies being ignored for build hooks May 6, 2026
@filbranden

Copy link
Copy Markdown
Author

Ah, I somehow missed that #2225 is posted that fixes issue #2110.

That's definitely a much less "brute force" way to solve this issue, certainly more elegant.

I'll close my PR then and pull that one into my local tree, until it gets merged and released officially.

Thanks!

@filbranden

filbranden commented May 13, 2026

Copy link
Copy Markdown
Author

Reopening after @cobaltt7's comment

@filbranden filbranden reopened this May 13, 2026
…2110)

PR pypa#2073 (workspace support) introduced a separate `additional_dependencies`
list on `EnvironmentInterface` and changed `prepare_build_environment` to
extend that list with the dynamic build-hook dependencies discovered by
`get_required_build_deps` (e.g. project runtime deps when a build hook sets
`require-runtime-dependencies = true`).

Bug: by the time `prepare_build_environment` extends
`additional_dependencies`, the cached property chain that consumes it:
`dependencies_complex` -> `all_dependencies_complex` -> `missing_dependencies`
has already been populated by the preceding `prepare_environment` call (via
`dependency_hash()` and `dependencies_in_sync()`) with the *empty*
`additional_dependencies` snapshot.

The second `sync_dependencies()` call then early-returns because the
cached `missing_dependencies` reflects the pre-mutation deps which have
just been installed — so the runtime deps never get installed and the
build hook fails with `ModuleNotFoundError`.

Before PR pypa#2073, this worked because the old code mutated the cached
`dependencies` list in place and the old `sync_dependencies` reinstalled
the entire list via pip — there was no cached `missing_dependencies`
short-circuit.

Fix: add `EnvironmentInterface.add_additional_dependencies()` which extends
`additional_dependencies` and invalidates the dependent cached properties
(`dependencies_complex`, `dependencies`, `all_dependencies_complex`,
`all_dependencies`, `missing_dependencies`). Update `prepare_build_environment`
to use it.

Add a regression test that primes the cache before extending and asserts
the new dep is visible.
@cobaltt7

Copy link
Copy Markdown

Still an issue with Hatch 1.17.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression: hatch fails on import in build hook with require-runtime-dependencies

3 participants