Replies: 2 comments
-
I've tried creating a minimal example, but it looks like Jinja always produces correct tracebacks when there's an error in a template. However during debug sessions the lines do not match. I suppose Jinja doesn't mess too much with the frames and rather corrects tracebacks when an exception is raised? That would explain why tracebacks are correct, while line numbers in debug session frames are not, and frames reported by |
Beta Was this translation helpful? Give feedback.
-
Huh, MkDocs catches warnings anyway to re-emit them as info logs... I guess I can just log them as warnings instead then. |
Beta Was this translation helpful? Give feedback.
-
Hello, I'm maintaining mkdocstrings, and I'm currently trying to rename my templates from
*.html
to*.html.jinja
, for proper syntax highlighting (and other tooling support). Since it's a breaking change (I allow users to override templates with custom ones), I want to prepare a deprecation period where overriding templates with a filename ending with.html
, or inheriting from a.html
base template will emit a deprecation warning. For this I add partials to the environmentglobals
:I can then warn like this:
{# in _base/attribute.html #} {{ warn.deprecation("this template is deprecated blabla") }}
However, running MkDocs, here's the output I get:
Note the file path
_base/children.html
and the line number97
. The actual call towarn
is in_base/attribute.html
at line3
.First, I have to let you know that this is not the first time I'm noticing wrong line numbers: when debugging mkdocstrings, everytime I'm in a Jinja frame, I'm on the wrong line. So maybe there's a more general issue here. I don't use particularly fancy templating stuff, apart maybe from multiline opening tags such as:
...(and similar ones with
filter
,set
, etc.), as well as empty{% with %}
tags (with bodies) for namespacing.My (rather cheap) intuition is that maybe these multiline opening tags are shifting the line numbers? Therefore line 97 above would actually be 97 + S, where S is the number of continuation lines in multiline opening tags? But that doesn't make sense when I look at my templates: the line that includes the
attribute.html
template, itself extending the_base/attribute.html
template, is line 35. There's another line, 125, which includes the same template, but the condition to reach it isn't set. Weirdly, this line and the calculated offset would match!{%
(excluded) up to the closing%}
(included): 28!Click to see the children.html template
When running the code,
config.group_by_category
is true, but somehow the line number reported by Jinja plus the offset computed from continuation lines matches line 125, which would be reached if the condition was false. Weird.I have added trailing comments on continuation lines to increment a counter 🙂
If my intuition is right, that's one issue out of three that is elucidated.
warnings.warn
reporting frames starting at the frame above: no idea 🤷To backup my second intuition about line numbers of the wrong conditional branch, here's a screenshot of me debugging execution of mkdocstrings:
I set a breakpoint in the
do_get_template
function (you can see the call stack in the left sidebar), that is called as a filter within the instruction thatinclude
s theattribute.html
template (editor panel, middle, though the frame reports line 124 and not 125).You can see that the frame info reports we're in the
else
clause of the{$ if config.group_by_category %}
condition (editor panel, top), whileconfig.group_by_category
is actually true (watched variables panel, bottom right).Sorry, this post is a bit messy. I guess I could summarize my questions as:
Happy to try and provide more minimal examples if needed 🙂
Beta Was this translation helpful? Give feedback.
All reactions