Skip to content

Commit

Permalink
fix: use a more specific regex for jinja2 set/for statements (#5514)
Browse files Browse the repository at this point in the history
  • Loading branch information
beckermr authored Oct 28, 2024
1 parent e69f7cb commit 639be98
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
12 changes: 10 additions & 2 deletions conda_build/variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,16 @@ def find_used_variables_in_text(variant, recipe_text, selectors_only=False):
)
# TODO: this `for` regex won't catch some common cases like lists of vars, multiline
# jinja2 blocks, if filters on the for loop, etc.
for_regex = r"(?:^|[^\{])\{%\s*for\s*.*\s*in\s*" + v_regex + r"(?:[^%]*?)?%\}"
set_regex = r"(?:^|[^\{])\{%\s*set\s*.*\s*=\s*.*" + v_regex + r"(?:[^%]*?)?%\}"
for_regex = (
r"(?:^|[^\{])\{%\s*for\s*.*\s*in\s*"
+ v_regex
+ r"(?![a-zA-Z_0-9])(?:[^%]*?)?%\}"
)
set_regex = (
r"(?:^|[^\{])\{%\s*set\s*.*\s*=\s*.*"
+ v_regex
+ r"(?![a-zA-Z_0-9])(?:[^%]*?)?%\}"
)
# plain req name, no version spec. Look for end of line after name, or comment or selector
requirement_regex = rf"^\s+\-\s+{v_req_regex}\s*(?:\s[\[#]|$)"
if selectors_only:
Expand Down
19 changes: 19 additions & 0 deletions news/5514-better-jinja2-regex.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Fixed regex for Jinja2 `set` / `for` statements to be more specific. (#5514)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>

0 comments on commit 639be98

Please sign in to comment.