From c729f367e6a9ad779dbe4ccc5aeaacdd5327947b Mon Sep 17 00:00:00 2001 From: Mike DePaulo Date: Tue, 29 Sep 2026 19:01:26 -0400 Subject: [PATCH] fix(dependencies): use the sdist requirements hook Call get_requires_for_build_sdist when discovering sdist dependencies instead of invoking the wheel requirements hook. Correct the matching docstring. Cover distinct wheel and sdist requirements, the empty default for an absent sdist hook, and a failing wheel hook using real backend subprocess calls. Closes: #1351 Co-Authored-By: OpenAI Codex Signed-off-by: Mike DePaulo --- src/fromager/dependencies.py | 4 +-- tests/test_dependencies.py | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/fromager/dependencies.py b/src/fromager/dependencies.py index 18dcdbef..9a756b1d 100644 --- a/src/fromager/dependencies.py +++ b/src/fromager/dependencies.py @@ -236,7 +236,7 @@ def default_get_build_sdist_dependencies( """Get build sdist dependencies Defaults to result of hook call - :meth:`~pyproject_hooks.BuildBackendHookCaller.get_requires_for_build_wheel` + :meth:`~pyproject_hooks.BuildBackendHookCaller.get_requires_for_build_sdist` """ logger.info(f"looking for build sdist dependencies in {build_dir}") pbi = ctx.package_build_info(req) @@ -247,7 +247,7 @@ def default_get_build_sdist_dependencies( override_environ=extra_environ, build_env=build_env, ) - return hook_caller.get_requires_for_build_wheel( + return hook_caller.get_requires_for_build_sdist( config_settings=pbi.config_settings, ) diff --git a/tests/test_dependencies.py b/tests/test_dependencies.py index 34a68ee8..ed986003 100644 --- a/tests/test_dependencies.py +++ b/tests/test_dependencies.py @@ -3,12 +3,14 @@ import os import pathlib import shutil +import sys import textwrap import typing import zipfile from unittest.mock import Mock, patch import hatchling.build +import pyproject_hooks import pytest from packaging.metadata import Metadata from packaging.requirements import Requirement @@ -294,6 +296,60 @@ def test_get_build_sdist_dependencies( assert names == set() +@pytest.mark.parametrize( + "backend_source,expected", + [ + ( + "def get_requires_for_build_wheel(config_settings=None):\n" + " return ['wheel-only>=1']\n" + "def get_requires_for_build_sdist(config_settings=None):\n" + " return ['sdist-only>=2']\n", + ["sdist-only>=2"], + ), + ( + "def get_requires_for_build_wheel(config_settings=None):\n" + " return ['wheel-only>=1']\n", + [], + ), + ( + "def get_requires_for_build_wheel(config_settings=None):\n" + " raise RuntimeError('wheel hook must not run')\n" + "def get_requires_for_build_sdist(config_settings=None):\n" + " return ['sdist-only>=2']\n", + ["sdist-only>=2"], + ), + ], + ids=["distinct-requirements", "optional-sdist-hook", "wheel-hook-fails"], +) +def test_default_get_build_sdist_dependencies_uses_sdist_hook( + backend_source: str, + expected: list[str], + tmp_context: context.WorkContext, + tmp_path: pathlib.Path, +) -> None: + """Discover sdist requirements independently of the wheel hook.""" + tmp_path.joinpath("backend.py").write_text(backend_source) + hook_caller = pyproject_hooks.BuildBackendHookCaller( + source_dir=str(tmp_path), + build_backend="backend", + backend_path=["."], + python_executable=sys.executable, + ) + with patch( + "fromager.dependencies.get_build_backend_hook_caller", + return_value=hook_caller, + ): + results = dependencies.default_get_build_sdist_dependencies( + ctx=tmp_context, + req=Requirement("example"), + sdist_root_dir=tmp_path, + build_dir=tmp_path, + extra_environ={}, + build_env=Mock(spec=build_environment.BuildEnvironment), + ) + assert list(results) == expected + + def test_get_build_sdist_dependencies_cached( tmp_context: context.WorkContext, tmp_path: pathlib.Path ) -> None: