Skip to content
Open
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
20 changes: 19 additions & 1 deletion conda_build/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2094,9 +2094,27 @@ def _get_contents(
except jinja2.TemplateError as ex:
if "'None' has not attribute" in str(ex):
ex = "Failed to run jinja context function"
raise CondaBuildUserError(
f"Failed to render jinja template in {self.meta_path}:\n{str(ex)}"
)
if not isinstance(ex, jinja2.TemplateSyntaxError):
# TODO handle other error types?
raise CondaBuildUserError(
f"Failed to render jinja template in {self.meta_path}:\n"
f"{type(ex).__name__}: {str(ex)}\n"
)
error_line = ex.source.splitlines()[ex.lineno - 1]
source_lines = list(enumerate(ex.source.splitlines()))
context_size = 3
error_context_lines = source_lines[(ex.lineno - 1 - context_size):(ex.lineno + context_size)]
lineno_width = len(str(error_context_lines[-1][0] + 1))
error_context_str = "\n".join(map(lambda L: f"{str(L[0] + 1).zfill(lineno_width)}: {L[1]}", error_context_lines))
raise CondaBuildUserError(
f"Failed to render jinja template in {self.meta_path}:\n{str(ex)}"
f"Failed to render jinja template in {self.meta_path}:\n"
f"{type(ex).__name__}: {str(ex)} in line {ex.lineno}\n"
+ error_context_str
)

finally:
if "CONDA_BUILD_STATE" in os.environ:
del os.environ["CONDA_BUILD_STATE"]
Expand Down
3 changes: 3 additions & 0 deletions news/pull-5985.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Enhancements

* Improve Jinja template error reporting by including the line number and context of the error
Loading