Skip to content

Commit 7bbe763

Browse files
authored
Fix require-runtime-dependencies being ignored for build hooks (#2257)
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`). `VirtualEnvironment` extends it to also invalidate its own `distributions` and `missing_dependencies` caches. Update `prepare_build_environment` to use it. Add a regression test that primes the caches before extending and asserts the new deps are visible and the virtual environment caches are cleared.
1 parent ac9c0c9 commit 7bbe763

4 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎src/hatch/env/plugin/interface.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,19 @@ def local_dependencies_complex(self) -> tuple[Dependency, ...]:
407407

408408
return tuple(local_dependencies_complex)
409409

410+
def add_additional_dependencies(self, dependencies: Sequence[Dependency]) -> None:
411+
"""
412+
Extend `additional_dependencies` and invalidate the cached dependency views.
413+
414+
Use this instead of mutating `additional_dependencies` directly when adding
415+
dependencies after the environment may have already computed
416+
`dependencies_complex` (e.g. after `prepare_environment`).
417+
Subclasses that cache values derived from the dependencies must extend this method.
418+
"""
419+
self.additional_dependencies.extend(dependencies)
420+
for attr in ("dependencies_complex", "dependencies", "all_dependencies_complex", "all_dependencies"):
421+
vars(self).pop(attr, None)
422+
410423
@cached_property
411424
def dependencies_complex(self) -> tuple[Dependency, ...]:
412425
"""Union of environment, project, and build dependencies before extras expansion on local projects."""

‎src/hatch/env/virtual.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
FREETHREADED_BUILD = bool(sysconfig.get_config_var("Py_GIL_DISABLED"))
2020

2121
if TYPE_CHECKING:
22-
from collections.abc import Callable, Iterable
22+
from collections.abc import Callable, Iterable, Sequence
2323

2424
from packaging.specifiers import SpecifierSet
2525
from python_discovery import PythonInfo
@@ -142,6 +142,12 @@ def distributions(self) -> InstalledDistributions:
142142
def missing_dependencies(self) -> list[Dependency]:
143143
return self.distributions.missing_dependencies(self.all_dependencies_complex)
144144

145+
def add_additional_dependencies(self, dependencies: Sequence[Dependency]) -> None:
146+
super().add_additional_dependencies(dependencies)
147+
# The distributions snapshot predates any packages installed since it was taken
148+
for attr in ("distributions", "missing_dependencies"):
149+
vars(self).pop(attr, None)
150+
145151
@staticmethod
146152
def get_option_types() -> dict:
147153
return {

‎src/hatch/project/core.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def prepare_build_environment(self, *, targets: list[str] | None = None, keep_en
294294
if additional_dependencies:
295295
from hatch.dep.core import Dependency
296296

297-
self.build_env.additional_dependencies.extend(map(Dependency, additional_dependencies))
297+
self.build_env.add_additional_dependencies([Dependency(dep) for dep in additional_dependencies])
298298
with self.build_env.app_status_dependency_synchronization():
299299
self.build_env.sync_dependencies()
300300

‎tests/env/plugin/test_interface.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import pytest
44

55
from hatch.config.constants import AppEnvVars
6+
from hatch.dep.core import Dependency
67
from hatch.env.plugin.interface import EnvironmentInterface
8+
from hatch.env.virtual import VirtualEnvironment
79
from hatch.project.core import Project
810
from hatch.utils.structures import EnvVars
911

@@ -2288,6 +2290,49 @@ def test_self_referencing_dependency_with_unknown_extra_warns(
22882290
assert not any("pytest" in str(d) for d in environment.all_dependencies_complex)
22892291
assert "refers to extra `tset`, which local project `my-app` does not define" in capsys.readouterr().err
22902292

2293+
def test_add_additional_dependencies_invalidates_caches(
2294+
self, temp_dir, isolated_data_dir, platform, temp_application
2295+
):
2296+
"""Extending additional_dependencies must invalidate the cached dependency views.
2297+
2298+
Otherwise the build env fails to install dynamic build-hook dependencies (e.g. those
2299+
pulled in by require-runtime-dependencies).
2300+
"""
2301+
pyproject = temp_dir / "pyproject.toml"
2302+
pyproject.write_text("""
2303+
[project]
2304+
name = "my-app"
2305+
version = "0.0.1"
2306+
""")
2307+
2308+
project = Project(temp_dir)
2309+
project.set_app(temp_application)
2310+
temp_application.project = project
2311+
environment = VirtualEnvironment(
2312+
temp_dir,
2313+
project.metadata,
2314+
"default",
2315+
project.config.envs["default"],
2316+
{},
2317+
isolated_data_dir,
2318+
isolated_data_dir,
2319+
platform,
2320+
0,
2321+
temp_application,
2322+
)
2323+
2324+
# Prime the caches with the empty additional_dependencies snapshot.
2325+
assert not any("extra-dep" in str(d) for d in environment.dependencies_complex)
2326+
assert not any("extra-dep" in str(d) for d in environment.all_dependencies_complex)
2327+
vars(environment)["distributions"] = vars(environment)["missing_dependencies"] = []
2328+
2329+
environment.add_additional_dependencies([Dependency("extra-dep"), Dependency("other-dep")])
2330+
2331+
assert {"extra-dep", "other-dep"} <= {d.name for d in environment.dependencies_complex}
2332+
assert {"extra-dep", "other-dep"} <= {d.name for d in environment.all_dependencies_complex}
2333+
assert "distributions" not in vars(environment)
2334+
assert "missing_dependencies" not in vars(environment)
2335+
22912336

22922337
class TestScripts:
22932338
@pytest.mark.parametrize("field", ["scripts", "extra-scripts"])

0 commit comments

Comments
 (0)