Skip to content

Commit

Permalink
Fix jinja2.exceptions.TemplateSyntaxError
Browse files Browse the repository at this point in the history
The filter loader is not working for meta.find_undeclared_variables and
templates using custom filters will error. Fall back to basic file
string matching when this happens.
  • Loading branch information
hoo29 committed Oct 13, 2024
1 parent 82bd8ad commit d3ac4f9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Change log for the little-timmy python module.

## [1.1.2] - 2024/10/13

- Fix `jinja2.exceptions.TemplateSyntaxError` when loading templates with custom filters by failing back to basic searching.
- Add `-v` version output.

## [1.1.1] - 2024/10/05

- Fix erroneous new line in `-g` output.
Expand Down
2 changes: 1 addition & 1 deletion github_action/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LABEL com.github.actions.name="little-timmy-action" \
org.opencontainers.image.vendor="@hoo29" \
org.opencontainers.image.description="GHA for Little Timmy, an unused Ansible variable finder."

RUN pip3 install little-timmy==1.1.1
RUN pip3 install little-timmy==1.1.2

COPY entrypoint.sh /entrypoint.sh

Expand Down
8 changes: 6 additions & 2 deletions little_timmy/var_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ansible.template import JinjaPluginIntercept
from dataclasses import dataclass
from glob import iglob
from jinja2 import Environment, meta
from jinja2 import Environment, exceptions, meta
from typing import Any

from .config_loader import Config
Expand Down Expand Up @@ -227,7 +227,11 @@ def find_unused_vars(directory: str, config: Config) -> dict[str, set[str]]:
for path in get_files_in_folder(directory, "**/templates", config, include_ext=True):
LOGGER.debug(f"template file {path}")
with open(path, "r") as f:
parse_jinja(f.read(), all_referenced_vars, path)
contents = f.read()
try:
parse_jinja(contents, all_referenced_vars, path)
except exceptions.TemplateSyntaxError:
check_raw_file_for_variables(contents, path, context)

# playbooks
for path in get_files_in_folder(directory, ".", config, f"*playbook*{YAML_FILE_EXTENSION_GLOB}"):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "little-timmy"
version = "1.1.1"
version = "1.1.2"
description = "Little Timmy will try their best to find those unused Ansible variables."
readme = "README.md"
authors = [{ name = "Huw" }]
Expand Down

0 comments on commit d3ac4f9

Please sign in to comment.