Skip to content

Commit

Permalink
fix: ensure variants are used on first parsing pass (#5528)
Browse files Browse the repository at this point in the history
* fix: ensure variants are used on first parsing pass

* fix: do not use recipe that requires variant in main tests

* doc: add news

* fix: remove brackets to not trigger selector code
  • Loading branch information
beckermr authored Nov 7, 2024
1 parent bd94b7f commit d587da5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
17 changes: 16 additions & 1 deletion conda_build/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
find_used_variables_in_shell_script,
find_used_variables_in_text,
get_default_variant,
get_package_variants,
get_vars,
list_of_dicts_to_dict_of_lists,
)
Expand Down Expand Up @@ -1167,7 +1168,21 @@ def __init__(self, path, config=None, variant=None):
# Therefore, undefined jinja variables are permitted here
# In the second pass, we'll be more strict. See build.build()
# Primarily for debugging. Ensure that metadata is not altered after "finalizing"
self.parse_again(permit_undefined_jinja=True, allow_no_other_outputs=True)

try:
# For the first pass, we do a simple read of any variants in the recipe and
# use them. These are then discarded. Other operations on the metadata
# will restore versions of them as needed. This is done to preserve
# old behavior.
old_config = self.config

self.config = get_or_merge_config(config, variant=variant)
self.config.variants = get_package_variants(self)
self.config.variant = self.config.variants[0]
self.parse_again(permit_undefined_jinja=True, allow_no_other_outputs=True)
finally:
self.config = old_config

self.config.disable_pip = self.disable_pip
# establish whether this recipe should squish build and host together

Expand Down
19 changes: 19 additions & 0 deletions news/5528-jinja2-first-pass.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Fixed bug variant variables were not defined for the first parsing pass of a recipe. (#5528)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package:
name: noarch-test
version: 0.1

source:
path: ../../test-package

build:
number: 0
noarch: python

requirements:
host:
- python {{ python_min }}.*
run:
- python >={{ python_min }}
9 changes: 9 additions & 0 deletions tests/test_api_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2078,3 +2078,12 @@ def test_conda_build_script_errors_without_conda_info_handlers(tmp_path, recipe,
assert "Traceback" in all_output
assert "CalledProcessError" in all_output
assert "returned non-zero exit status 1" in all_output


def test_api_build_inject_jinja2_vars_on_first_pass(testing_config):
recipe_dir = os.path.join(metadata_dir, "_inject_jinja2_vars_on_first_pass")
with pytest.raises(RuntimeError):
api.build(recipe_dir, config=testing_config)

testing_config.variant = {"python_min": "3.12"}
api.build(recipe_dir, config=testing_config)
2 changes: 1 addition & 1 deletion tests/test_api_build_conda_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_conda_pkg_format(

# Recipe "entry_points" is used in other test -> add test-specific variant
# (change build hash) to avoid clashes in package cache from other tests.
variants = {"pytest_name": [request.node.name]}
variants = {"pytest_name": [request.node.name.replace("[", "").replace("]", "")]}
(output_file,) = api.get_output_file_paths(
recipe, config=testing_config, variants=variants
)
Expand Down

0 comments on commit d587da5

Please sign in to comment.