SilentVoid13 / Templater

A template plugin for obsidian
https://silentvoid13.github.io/Templater
GNU Affero General Public License v3.0
2.95k stars 169 forks source link

depth limit is not really a depth limit #1174

Open SylvainGuieu opened 10 months ago

SylvainGuieu commented 10 months ago

Plugin information (please complete the following information):

Describe the bug

It seems that the depth limit is not really a depth limit.

I have 5 files (a, b, c, d, e) , each includes one other single file (foo) in their body.
In a master file I include the 5 files (a, b, c, d, e). This lead to a total of 10 includes calls. It works with 5. If I add a sixth file The error "inclusion depth limit" is trow, however the real inclusion depth is still 2.

It seems that the depth counter is just counting the occurrence of inclusions but not in depth.

Expected behavior Should work as the real inclusion depth is 2

Zachatoo commented 10 months ago

Can reproduce, you can have a depth of 1 and still encounter this issue. Assuming all the notes you're including are notes that don't include any other notes, you'll reach a depth of 11 with the following example, which is beyond the max of 10.

<% tp.file.include("[[Note1]]") %>
<% tp.file.include("[[Note2]]") %>
<% tp.file.include("[[Note3]]") %>
<% tp.file.include("[[Note4]]") %>
<% tp.file.include("[[Note5]]") %>
<% tp.file.include("[[Note6]]") %>
<% tp.file.include("[[Note7]]") %>
<% tp.file.include("[[Note8]]") %>
<% tp.file.include("[[Note9]]") %>
<% tp.file.include("[[Note10]]") %>
<% tp.file.include("[[Note11]]") %>
SylvainGuieu commented 10 months ago

@Zachatoo indeed. I should have presented this way. Thanks.

Zachatoo commented 9 months ago

I did some digging, and this will be harder to fix than I thought. It's a race condition, and the plugin author has noted that this is an issue here.

However, in my digging, I did find a workaround! You can add a delay in between each tp.file.include() to avoid the race condition. I don't know if it's entirely necessary to do it in between every one, but it won't hurt, and 1ms is fast enough that it shouldn't be noticeable.

<%*
const sleep = ms => new Promise(r => setTimeout(r, ms));
-%>
<% tp.file.include("[[Note1]]") %>
<% await sleep(1) -%>
<% tp.file.include("[[Note2]]") %>
<% await sleep(1) -%>
<% tp.file.include("[[Note3]]") %>
<% await sleep(1) -%>
<% tp.file.include("[[Note4]]") %>
<% await sleep(1) -%>
<% tp.file.include("[[Note5]]") %>
<% await sleep(1) -%>
<% tp.file.include("[[Note6]]") %>
<% await sleep(1) -%>
<% tp.file.include("[[Note7]]") %>
<% await sleep(1) -%>
<% tp.file.include("[[Note8]]") %>
<% await sleep(1) -%>
<% tp.file.include("[[Note9]]") %>
<% await sleep(1) -%>
<% tp.file.include("[[Note10]]") %>
<% await sleep(1) -%>
<% tp.file.include("[[Note11]]") %>

I'll try to come back to this at some point. I believe we'll need to update the parser (rusty_engine) to pass along some sort of unique identifier or mutex for each tp.file.include() in the template to track depth.

TheBrightSoul commented 1 month ago

But Shouldn't we Increase the Depth limit if that's bugged now. So, basically I was trying to use Suggester with the following code but It count all of the entries as included files and exceeds the limit If I were to select the last one

<% tp.system.suggester([
"CompTIA A+",
"Python Programming",
"Web Development",
"Computer Science Crash Course",
"Windows Fundamentals",
"Artificial intelligence",
"Branch Education",
"No study today" ],
[tp.file.include(
"[[CompTIA A+ Study template]]"),
tp.file.include("[[Python Study template]]"),
tp.file.include("[[Web Development Study template]]"),
tp.file.include("[[CS Crash course Study template]]"),
tp.file.include("[[Windows Fundamentals]]"),
tp.file.include("[[AI Study template]]"),
tp.file.include("[[Branch Education Study Template]]"),
tp.file.include("[[No Study Template]]")],
true,
"Hallo! Mr. Bright. What you want to Study Today?",) %>
Zachatoo commented 1 month ago

That's a slightly different issue, you're executing the code in each of those templates and then throwing away the result of each template except for one. This would be a version of that template that only executes the selected template.

<% await tp.file.include(await tp.system.suggester([
"CompTIA A+",
"Python Programming",
"Web Development",
"Computer Science Crash Course",
"Windows Fundamentals",
"Artificial intelligence",
"Branch Education",
"No study today" ],
["[[CompTIA A+ Study template]]",
"[[Python Study template]]",
"[[Web Development Study template]]",
"[[CS Crash course Study template]]",
"[[Windows Fundamentals]]",
"[[AI Study template]]",
"[[Branch Education Study Template]]",
"[[No Study Template]]"],
true,
"Hallo! Mr. Bright. What you want to Study Today?",) %>

I'm not opposed to upping the limit, but in this case the current state could potentially cause unwanted side effects.

TheBrightSoul commented 1 month ago

That's a slightly different issue, you're executing the code in each of those templates and then throwing away the result of each template except for one. This would be a version of that template that only executes the selected template.

<% await tp.file.include(await tp.system.suggester([
"CompTIA A+",
"Python Programming",
"Web Development",
"Computer Science Crash Course",
"Windows Fundamentals",
"Artificial intelligence",
"Branch Education",
"No study today" ],
["[[CompTIA A+ Study template]]",
"[[Python Study template]]",
"[[Web Development Study template]]",
"[[CS Crash course Study template]]",
"[[Windows Fundamentals]]",
"[[AI Study template]]",
"[[Branch Education Study Template]]",
"[[No Study Template]]"],
true,
"Hallo! Mr. Bright. What you want to Study Today?",) %>

I'm not opposed to upping the limit, but in this case the current state could potentially cause unwanted side effects.

Dude, It worked but took me an hour to find out that you've haven't Included an closing bracket at the end in the last line. Correction:

What you want to Study Today?",)) %>

I've spent all this time trying to debug why this script is giving error. :(

But Thanks I've learnt new things while i was debugging.