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

jinja2: improve error context information #4842

Merged
merged 3 commits into from
May 12, 2022

Conversation

oliver-sanders
Copy link
Member

@oliver-sanders oliver-sanders commented Apr 28, 2022

Our Jinja2 error context reporting gets stuck on include statements.

A couple of our Cylc 8 trialers have hit Python 2->3 issues (e.g. dict.keys()) that have been obscured by this. I suspect a few more will run into similar issues. It's quite hard to debug these issues when all you see is TypeError and the line the {% include statement is on.

Took a look, seems an easy fix.

Example

Should validate with Cylc 7 but fail with Cylc 8 due to Python the 2->3 change in dict.keys return type:

(flow.cylc)

#!Jinja2

{% include "foo.cylc" %}

(foo.cylc)

{% set foo = {"a": 1} %}
{% for key in foo.keys() + ["b"] %}
#    {{ key }}
{% endfor %}

Before this change only the {% include line would be highlighted, after this change the real error should also appear in the error.

Requirements check-list

  • I have read CONTRIBUTING.md and added my name as a Code Contributor.
  • Contains logically grouped changes (else tidy your branch by rebase).
  • Does not contain off-topic changes (use other PRs for other changes).
  • Applied any dependency changes to both setup.cfg and conda-environment.yml.
  • Appropriate tests are included (unit and/or functional).
  • Appropriate change log entry included.
  • No documentation update required.

@oliver-sanders oliver-sanders self-assigned this Apr 28, 2022
@oliver-sanders oliver-sanders added this to the cylc-8.0rc4 milestone May 3, 2022
@oliver-sanders oliver-sanders added small could be better Not exactly a bug, but not ideal. labels May 3, 2022
@oliver-sanders oliver-sanders force-pushed the jinja2-error-context branch 3 times, most recently from 1105aa4 to 6f5b8e6 Compare May 4, 2022 10:17
@oliver-sanders oliver-sanders marked this pull request as ready for review May 4, 2022 10:17
* When `{% include %}` statements are involved, the error context we
  extract highlights the include statement as the error *not* the actual
  line where the error happened.
* This expands the context extraction to pull out the relevant lines
  from include files where this happens.
@@ -80,7 +111,7 @@ def __init__(self, reason, index=None, line=None, lines=None,
self.fpath = fpath
self.help_lines = help_lines or []

def __str__(self):
def __str__(self) -> str:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obvious but mypy needs this to work.

Copy link
Member

@MetRonnie MetRonnie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested out. Before:

Jinja2Error: Jinja2 Error: some error
Context lines:

# line before include
{% include "foo.cylc" %}	<-- Exception

After:

Jinja2Error: Jinja2 Error: some error
File ~/cylc-run/GAH/foo.cylc
  # line before error
  {{ raise('some error') }}
  # line after error	<-- Exception
File ~/cylc-run/GAH/flow.cylc
  #!Jinja2
  
  # line before include
  {% include "foo.cylc" %}	<-- Exception

Minor niggle is that the traceback is reversed compared to what you would expect i.e. foo.cylc comes before flow.cylc

cylc/flow/parsec/fileparse.py Show resolved Hide resolved
Copy link
Member

@hjoliver hjoliver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Just needs a change log entry?

@oliver-sanders oliver-sanders merged commit 4b7862f into cylc:master May 12, 2022
@oliver-sanders oliver-sanders deleted the jinja2-error-context branch May 12, 2022 11:58
This was referenced May 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
could be better Not exactly a bug, but not ideal. small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants