From 0ad4bb6829b440caae9ae8a4db14ab1ff3788ab7 Mon Sep 17 00:00:00 2001 From: "Matthew R. Becker" Date: Fri, 15 Nov 2024 12:53:56 -0600 Subject: [PATCH] fix: catch `CondaBuildUserError` w/ `SystemExit` (#5538) * fix: catch user errors in `distribute_variants` * fix: catch a few more SystemExits * test: add test to ensure undefine jinja2 is OK * doc: add news --- conda_build/metadata.py | 4 +-- conda_build/render.py | 4 +-- news/5538-sysexit-vs-cdusererror | 20 ++++++++++++++ tests/test_api_render.py | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 news/5538-sysexit-vs-cdusererror diff --git a/conda_build/metadata.py b/conda_build/metadata.py index 278d56eb1e..5cc5bc129a 100644 --- a/conda_build/metadata.py +++ b/conda_build/metadata.py @@ -2579,7 +2579,7 @@ def get_output_metadata_set( ref_metadata.parse_until_resolved( allow_no_other_outputs=True, bypass_env_check=True ) - except SystemExit: + except (SystemExit, CondaBuildUserError): pass outputs = get_output_dicts_from_metadata(ref_metadata) @@ -2611,7 +2611,7 @@ def get_output_metadata_set( ref_metadata.other_outputs = out_metadata.other_outputs = ( all_output_metadata ) - except SystemExit: + except (SystemExit, CondaBuildUserError): if not permit_undefined_jinja: raise output_tuples = [] diff --git a/conda_build/render.py b/conda_build/render.py index f6cfda491c..6292210301 100644 --- a/conda_build/render.py +++ b/conda_build/render.py @@ -34,7 +34,7 @@ from . import environ, exceptions, source, utils from .config import CondaPkgFormat -from .exceptions import DependencyNeedsBuildingError +from .exceptions import CondaBuildUserError, DependencyNeedsBuildingError from .index import get_build_index from .metadata import MetaData, MetaDataTuple, combine_top_level_metadata_with_output from .utils import ( @@ -894,7 +894,7 @@ def distribute_variants( allow_no_other_outputs=allow_no_other_outputs, bypass_env_check=bypass_env_check, ) - except SystemExit: + except (SystemExit, CondaBuildUserError): pass need_source_download = not mv.needs_source_for_render or not mv.source_provided diff --git a/news/5538-sysexit-vs-cdusererror b/news/5538-sysexit-vs-cdusererror new file mode 100644 index 0000000000..b01e596601 --- /dev/null +++ b/news/5538-sysexit-vs-cdusererror @@ -0,0 +1,20 @@ +### Enhancements + +* + +### Bug fixes + +* Fixed a bug where some ``CondaBuildUserError`` exceptions that were formally ``SystemExit`` exceptions + were not being caught properly. (#5538) + +### Deprecations + +* + +### Docs + +* + +### Other + +* diff --git a/tests/test_api_render.py b/tests/test_api_render.py index 0882de0df1..eb75a283d9 100644 --- a/tests/test_api_render.py +++ b/tests/test_api_render.py @@ -7,6 +7,7 @@ import os import re +import textwrap from itertools import count, islice import pytest @@ -15,6 +16,7 @@ from conda.common.compat import on_win from conda_build import api, render +from conda_build.exceptions import CondaBuildUserError from conda_build.variants import validate_spec from .utils import metadata_dir, variants_dir @@ -341,3 +343,46 @@ def create_variants(): recipe, config=testing_config, channels=[], variants=create_variants() ) assert len(metadata_tuples) == 11 - 3 # omits libarrow-all, pyarrow, pyarrow-tests + + +def test_api_render_missing_jinja2(testing_config, testing_workdir): + with open(os.path.join(testing_workdir, "meta.yaml"), "w") as f: + f.write( + textwrap.dedent( + """ + package: + name: blah-{{ foo }} + version: 0.1 + + build: + number: 0 + + requirements: + host: + - python {{ python_min }} + run: + - python + """ + ) + ) + + meta = api.render( + testing_workdir, + finalize=False, + bypass_env_check=True, + trim_skip=False, + ) + assert meta is not None + assert any("python" in val for val in meta[0][0].get_value("requirements/host")) + assert not any( + "{{ python_min }}" in val for val in meta[0][0].get_value("requirements/run") + ) + assert meta[0][0].get_value("package/name") == "blah-" + + with pytest.raises(CondaBuildUserError): + api.render( + testing_workdir, + finalize=True, + bypass_env_check=True, + trim_skip=False, + )