Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure variants are used on first parsing pass #5528

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading