From d2ffd7a4cafca944febcb2f68cc58c9960c8e9f2 Mon Sep 17 00:00:00 2001 From: Ezra Brooks Date: Wed, 4 Nov 2026 15:50:12 -0700 Subject: [PATCH] conditional_python_dependency: pin the build python, cross-compilation included With conditional_python_dependency, recipes without Python content get a build python pinned to python_min, so they aren't built per Python. The cross-compilation block's bare python escaped that pin and put them back into the Python variant matrix when cross-compiling. Packages built per Python, on the other hand, got the pin too: cross-compiling, their build environment also gets the variant's python (for cross-python), so a cp313 build asked for python 3.12.* and 3.13 at once. Pin both build pythons in one place, after dependencies.yaml (which can add python to a package without Python dependencies, e.g. one that embeds the interpreter), and only when host ends up without python. --- vinca/recipes.py | 24 +++++++++++++++++++----- vinca/test_recipes.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/vinca/recipes.py b/vinca/recipes.py index f326762..0812d5b 100644 --- a/vinca/recipes.py +++ b/vinca/recipes.py @@ -83,7 +83,9 @@ # pinned to python_min rather than a bare "python": a fully resolved version # constraint is invisible to rattler-build's variant scan (confirmed with # `rattler-build build --render-only`: one variant instead of one per pinned Python), -# so a package without Python content is not rebuilt per Python version. +# so a package without Python content is not rebuilt per Python version. A package +# built per Python keeps the bare one: cross-compiling, its build environment also +# gets the variant's python for cross-python, which the pin would contradict. # `default('3.11')` covers a conda_build_config.yaml without python_min, which would # otherwise render the invalid spec "python .*". _BUILD_TIME_PYTHON = "python ${{ python_min | default('3.11') }}.*" @@ -236,10 +238,6 @@ def _initial_output( if not is_dummy_metapackage(shortname, vinca_conf): requirements = copy.deepcopy(_BASE_REQUIREMENTS) if _conditional_python(vinca_conf): - requirements["build"] = [ - _BUILD_TIME_PYTHON if r == "python" else r - for r in requirements["build"] - ] requirements["host"] = [ r for r in requirements["host"] if r not in _HOST_PYTHON ] @@ -325,6 +323,18 @@ def _replace_with_selector( ) +def _pin_build_python(requirements: list[Any]) -> None: + """Replace the bare build ``python``, including the cross-compilation one, + with _BUILD_TIME_PYTHON.""" + for i, requirement in enumerate(requirements): + if requirement == "python": + requirements[i] = _BUILD_TIME_PYTHON + elif isinstance(requirement, dict) and "python" in requirement["then"]: + requirement["then"] = [ + _BUILD_TIME_PYTHON if r == "python" else r for r in requirement["then"] + ] + + def _adjust_requirements(requirements: Requirements, package_prefix: str) -> None: """Apply platform and cross-compilation fixups, then sort and deduplicate. @@ -591,6 +601,10 @@ def generate_output( removed, added = get_depmods(vinca_conf, package.name, distro) _apply_depmods(output["requirements"], removed, added) + # after dependencies.yaml, which can add python to a package without Python + # dependencies (e.g. one that embeds the interpreter) + if conditional_python and "python" not in output["requirements"]["host"]: + _pin_build_python(output["requirements"]["build"]) _adjust_requirements(output["requirements"], package_prefix) if dependencies_only: return output["requirements"] diff --git a/vinca/test_recipes.py b/vinca/test_recipes.py index 81bdf3d..de2f779 100644 --- a/vinca/test_recipes.py +++ b/vinca/test_recipes.py @@ -270,6 +270,43 @@ def test_needs_python_plain_cpp_package_gets_no_python_dependency(): assert "python" not in output["requirements"]["build"] +def _cross_python_block(output): + return next( + r["then"] + for r in output["requirements"]["build"] + if isinstance(r, dict) and any("cross-python" in d for d in r["then"]) + ) + + +def test_python_free_output_pins_cross_compilation_python(): + output = build_conditional("demo", depends=[("build_depend", "rclcpp")]) + + block = _cross_python_block(output) + assert "python" not in block + assert recipes._BUILD_TIME_PYTHON in block + + +def test_python_output_keeps_cross_compilation_python(): + output = build_conditional("demo", depends=[("exec_depend", "rclpy")]) + + assert "python" in _cross_python_block(output) + # the cross-compilation python is the variant's, so the build one must be too + assert "python" in output["requirements"]["build"] + assert recipes._BUILD_TIME_PYTHON not in output["requirements"]["build"] + + +def test_python_added_in_dependencies_yaml_keeps_cross_compilation_python(): + # e.g. plotjuggler: embeds libpython without a Python-flavored dependency + output = build_conditional( + "demo", + depends=[("build_depend", "rclcpp")], + depmods={"demo": {"add_host": ["python"]}}, + ) + + assert "python" in _cross_python_block(output) + assert recipes._BUILD_TIME_PYTHON not in output["requirements"]["build"] + + def test_needs_python_rosidl_interface_package_gets_python_dependency(): # msg/srv/action packages declare membership in rosidl_interface_packages; # rosidl_generator_py always compiles Python bindings for these,