From 367d4a37409963ca1f47139e77789aa552f13026 Mon Sep 17 00:00:00 2001 From: Ezra Brooks Date: Tue, 3 Nov 2026 13:56:07 -0700 Subject: [PATCH] conditional_python_dependency: guard Python-free outputs with a contents test A package classified Python-free is built once, against no particular Python. If the classification is wrong, its Python modules end up in the site-packages of the build-time interpreter only, and nothing notices. Give every Python-free output a package_contents test that fails the build on site-packages content or CPython extensions (byte-compiled launch files outside site-packages are fine); the fix is then a `needs_python: true` override. A per-package test file from tests/ used to replace the recipe's tests; it is now appended to them, so the guard stays. --- vinca/recipes.py | 24 ++++++++++++++++++++++++ vinca/template.py | 2 +- vinca/test_recipes.py | 19 +++++++++++++++++++ vinca/test_template.py | 16 ++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/vinca/recipes.py b/vinca/recipes.py index f326762..4d3e68d 100644 --- a/vinca/recipes.py +++ b/vinca/recipes.py @@ -94,6 +94,24 @@ def _conditional_python(vinca_conf: dict[str, Any]) -> bool: return bool(vinca_conf.get("conditional_python_dependency", False)) +# Installed into every Python-free output's tests (conditional_python_dependency): +# such an output is built once, against no particular Python, so Python modules or +# extensions in it would only be importable by the interpreter it happened to be +# built with. Byte-compiled launch files (share//launch/__pycache__) are fine. +_PYTHON_FREE_CONTENTS_TEST = { + "package_contents": { + "strict": False, + "files": { + "not_exists": [ + "**/site-packages/**", + "**/*.cpython-*.so", + "**/*.pyd", + ] + }, + } +} + + # Dependency names that indicate a package's own recipe genuinely needs a # Python interpreter/ABI in host/run (as opposed to Python merely being a # build-time tool for ament's own scripts, see _BUILD_TIME_PYTHON). @@ -592,6 +610,12 @@ def generate_output( removed, added = get_depmods(vinca_conf, package.name, distro) _apply_depmods(output["requirements"], removed, added) _adjust_requirements(output["requirements"], package_prefix) + if ( + conditional_python + and not is_dummy + and "python" not in output["requirements"]["host"] + ): + output["tests"] = [copy.deepcopy(_PYTHON_FREE_CONTENTS_TEST)] if dependencies_only: return output["requirements"] _add_metadata(output, package, shortname, distro) diff --git a/vinca/template.py b/vinca/template.py index 1a13046..48db64d 100644 --- a/vinca/template.py +++ b/vinca/template.py @@ -151,7 +151,7 @@ def write_recipe(source, outputs, vinca_conf, distro, single_file=True): test_content = evaluate_distro_selectors( yaml.safe_load(text), ros_distro=config.ros_distro ) - meta["tests"] = test_content["tests"] + meta["tests"] = list(meta.get("tests") or []) + test_content["tests"] recipe_dir = (Path("recipes") / package_name).absolute() os.makedirs(recipe_dir, exist_ok=True) diff --git a/vinca/test_recipes.py b/vinca/test_recipes.py index 81bdf3d..6b3275d 100644 --- a/vinca/test_recipes.py +++ b/vinca/test_recipes.py @@ -558,3 +558,22 @@ def test_dummy_constraint_uses_pin_depth_and_override_version(): def test_dummy_constraint_validates_required_settings(config, message): with pytest.raises(RuntimeError, match=message): _dummy_constraint(config, "1.0.0", "demo_vendor") + + +def test_python_free_output_gets_contents_guard(): + output = build_conditional("demo", depends=[("build_depend", "rclcpp")]) + + assert output["tests"] == [recipes._PYTHON_FREE_CONTENTS_TEST] + assert output["tests"][0] is not recipes._PYTHON_FREE_CONTENTS_TEST + + +def test_python_output_gets_no_contents_guard(): + output = build_conditional("demo", depends=[("exec_depend", "rclpy")]) + + assert "tests" not in output + + +def test_no_contents_guard_without_conditional_python(): + output = build("demo", depends=[("build_depend", "rclcpp")]) + + assert "tests" not in output diff --git a/vinca/test_template.py b/vinca/test_template.py index 651670f..7925529 100644 --- a/vinca/test_template.py +++ b/vinca/test_template.py @@ -44,6 +44,22 @@ def test_write_recipe_emits_pruned_variants_in_local_mode(tmp_path, monkeypatch) assert YAML(typ="safe").load(variants_path) == {"python": ["3.12"]} +def test_write_recipe_appends_test_file_to_generated_tests(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + test_file = tmp_path / "ros-jazzy-demo.yaml" + test_file.write_text("tests:\n - script:\n - demo --help\n") + conf = config("global") + conf["_tests"] = {"ros-jazzy-demo": test_file} + generated = {"package_contents": {"files": {"not_exists": ["**/*.pyd"]}}} + + write_recipe( + {}, [{**output(), "tests": [generated]}], conf, FakeDistro(), single_file=False + ) + + recipe = YAML(typ="safe").load(Path("recipes/ros-jazzy-demo/recipe.yaml")) + assert recipe["tests"] == [generated, {"script": ["demo --help"]}] + + def test_write_recipe_does_not_emit_variants_in_global_mode(tmp_path, monkeypatch): monkeypatch.chdir(tmp_path)