You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Ease debugging of "layout did not converge within 5 attempts" issues #4528
Add a way to reduce the number of layout attempts, to be able to output 5 pdfs (one for each iteration), allowing to better understand the lack of convergence.
When I fist encountered this error (see Use Case), my first thought was to augment the number of layout attempts (since it surely must converge at some point, right?), however after more thoughts, this would likely be bad idea, since people (like me) would just be lazy and increase this number without consideration.
Hence the opposite proposal: allow the number of attempts to be decreased.
This would allow me to see 5 PDFs and get a better feeling, why my layout is not converging.
Use Case
I am trying to implement an autobreak feature (for a songbook system, see #993 (comment)), were I get layout did not converge within 5 attempts and no idea how to solve it:
If I execute the logic in my head, the layout should converge immediately, since at the beginning of every autobreak block, I can compute where the current block should go. So I really don't understand how Typst is computing the layout and why it is taking so many steps. Having a way to see the step 1-4 should help me get a better understanding (and eventually find a suitable fix).
BTW Typst looks awesome, thank your for this hard work!
I am creating a songbook-system to replace patacrep (which is based on LaTeX + Songs). The webapp looks really neat and I think that multiple people I know would love to use it to create their next songbook collaboratively (I know 3 songbook-projects which could benefit from it ;)
Reacted by Malo, +merlan #flirora, Void, bluss and Stefan Fast
I had the same problem when trying to reach a specific number of pages. Usecase: i'm making booklets so i want to add pagebreaks so that the number of pages is a multiple of 4. It's unclear to me when the total page count is updated (on every layout iteration?) and how that affects dynamic page break insertion.
EDIT: This is not a blocker for me. I simply moved the page break logic to after typst compilation.
I suggest adding a CLI argument under the name max-layout-iterations or similar that restricts the number of layout iterations (maximum 5). This way a document can be compiled to see how it looks after a certain number of iterations.
Note that this version is comparably straight-forward and has far less implementation overhead (and potential discussions) than the originally proposed solution of outputting 5 pdf for each iteration. Still, the same is basically achieved.
For debugging purposes, it would of course be more direct to be able to change this setting right in the document, but I can also see some reasons against that.
Not my favorite either, though I'm glad it helps you @zinifier.
Makes me wonder... why couldn't Typst just do the same thing, add extra pages, see if it converges, repeat a few times, then if converges just delete the extra pages before finalizing the PDF output?
It's all hacks, I agree... but this is a problem experienced by many Typstists. Any solution, temporary or otherwise, would be great.
Barring that, let us users decide on max iterations. I get that if it's not converging then some theoretical thing must be wrong. But that doesn't help me finish my already overdue papers. I, and others at my company, use Typst for real work now; and I'm quickly finding that I strongly depend on it.
@laurmaedje - how can I help here? I'm happy to legitimately make fixing this my top non-work tech priority.
Was just about to create a feature request for this exact feature. I agree that allowing any value will just lead to me being lazy and increasing it to make it converge, which is a bad idea. But allowing me to set the number of iterations up to a maximum of 5 would REALLY help debugging, and seems like a fairly easy thing to implement (though I obviously don't know).
Here are my initial collected thoughts on how we could improve the debugging situation. In part, this summarizes existing ideas, but there are also a few new ones.
Locating the problem
Ideally, we'd have a span / error range for what specific introspection actually didn't converge (e.g. which query). However, it's really quite difficult to implement with how memoization works. Still, somehow we need to solve this.
Visualizing how the document evolves
It would be quite useful, as a user, to have a way to step through the different iterations to understand what's happening where (I think this should primarily be an IDE feature of the web app and tinymist). This should be fairly easy to implement and it's something that's on our web app roadmap.
Analysing patterns in the state
We could try to analyse how the state evolves to give the user a bit more insight into what's going wrong.
I think the common cases of how the state evolves are
A, B, C, D, E, F, G, G (converges, but too late)
A, B, C, B, C, B, C, ... (doesn't coverge, but oscillates)
A, B, C, D, E, F, G, H, I, ... (doesn't converge, also doesn't oscillate, may still exhibit patterns)
To help the user, we could try to
run a fixed number of additional iterations for diagnostic purposes (to understand whether it would converge but just not quickly enough), but still output the document from iteration 5. That would help us understand if the code does in principle converge, but too slowly (case 1). Might be frustrating though if Typst tells you "hey, it converged after 7 iterations, but I'm not gonna give you the final document".
detect state oscillations to know for sure that it cannot converge (case 2).
detect more patterns in the state (case 3) to predict whether it is likely to converge. Much fuzzier than the first two proposals. But if e.g. we have a state that evolves in a monotonic fashion, perhaps it's possible to detect that.
Also possible and much easier than providing a span is to provide just some more information about the query that didn't converge and possibly how it evolved over the iterations. Implementation-wise that's fairly easy, the challenge is more one of UX, i.e. how we would present different queries. The actual queries can be quite a bit more low-level than what the user wrote.
Sometimes, the link is still fairly clear, e.g. a non-converging state("hi") might fail to converge on a query(state-update.where(key: "hi"))). We could try to detect common cases and display a nice message instead of the raw query. Similarly e.g. for location.position()). The raw query is quite useless (a location is really just a 128-bit unique ID, so it would be something like introspector.position(120938120958102958)), but with a bit of work, we could resolve the location to the element and say "the position of this heading didn't stabilize". We could even provide the source location of the heading.
What we couldn't easily tell you is who asked for the location of the heading. That's where having the spans of the queries themselves would come in. This is a lot harder to implement, and not always so clear-cut because the compiler itself might run queries which don't always have a associated source location (though it might be possible to derive something that's close enough, e.g. the span of a set heading(numbering) when a heading counter doesn't converge).
I'm imaging something like this (just a first draft):
warning: the position of this heading did not stabilize in 5 attempts
┌─ hi.typ:3:0
│
3 │ = Heading
│ ^^^^^^^^^
hint: the position of the heading evolved as follows:
- try 1: page 1 at (47pt, 30pt)
- try 2: page 1 at (47pt, 40pt)
- try 3: page 1 at (47pt, 50pt)
- try 4: page 1 at (47pt, 60pt)
- try 5: page 1 at (47pt, 70pt)
Reacted by oliverpool, +merlan #flirora, Daniel Hauck, Rasmus Mecklenburg and moinsr
Having the compiler tell me where things went wrong would be great. However, given the complexity of most of these options, simply giving me the option to specify the max number of iterations (up to 5) seems like an important first step. I can then produce five PDFs and check where changes happen. It's not pretty but it seems like it should be simple to implement and it'll never have any uncommon cases where it won't work. It should also be useful long term since it would be easier to detect and report edge cases for better compiler hints.
@davystrong I agree that this feature should exist, but it feels more like an IDE feature for tinymist and the web app to me than a CLI flag. There is some boundary as to what features should only be exposed via IDE versus also via CLI. To the one extreme, autocompletion is obviously one that is clearly in the IDE camp. For some of the debugging tools, I'm not sure whether in the range they should land.
I created the issue in tinymist. I agree it would be perfect as an IDE feature but I'm not sure I agree it would be out of place in the CLI. Unlike autocompletion (which I know was an extreme example), this isn't something only related to writing the document. It is to do with compiling the document, and therefore including the flag in the compiler seems reasonable. I may be biased though because, while I use an IDE basically all the time, I don't like it when I HAVE to use the IDE. I like that all my tools are available as CLIs. So I'll not press it any further, it's up to you.
@laurmaedje is this effectively the same thing (with the exception of the document now having an "__iterations" counter and the fact that the document is now inside a context environment)?
Or is there something I'm missing that also affects the number of iterations? This certainly acts as I'd expect it to when included at the start of the document, but I'm not sure if imports, for example, are imported regardless.
This is cursed, I love it. I think it will probably do what you want. Imports will happen regardless, but that can't have any effect on the layout if you don't put anything into the document in your show: rule.
I was considering creating a package but thought it might fall foul of the "Packages must not attempt to exploit the compiler" requirement. If not, I'll try to create a package at some stage when I have the time.
Also, credit where it's due, it was @Myriad-Dreamin who suggested something like this might be possible.
Nice! Next logical step.. script generating output with this package for iterations in [1..=5], then create a progressive diff of the images from each iteration to the next.
ETA: Or, although it probably just makes the problem worse and may not help with debugging, query some set of known labels, headings, etc after compiler() finishes its N iterations and record that in a summary. This could help guide users to what part of their document is moving the most each iteration.
Regardless of whether something is exposed as a CLI feature, the maximum iterations being somehow configurable is a good idea, I think. And a first step would be to expose it somewhere in the library, instead of talking constants. Is there any reason not to do this? My main question would be, which object should hold the configuration for this? World or the Document?
Right now, in order to debug things, I'm just editing the source code like the following (I'm not sure about the String::leak, but I don't think it will cause big issues).
Code diff
diff --git a/crates/typst/src/lib.rs b/crates/typst/src/lib.rs
index 580ba9e8..9f755af6 100644
--- a/crates/typst/src/lib.rs+++ b/crates/typst/src/lib.rs@@ -33,6 +33,8 @@
pub extern crate comemo;
pub extern crate ecow;
+const MAX_ITER: usize = 10;+
pub use typst_library::*;
#[doc(inline)]
pub use typst_syntax as syntax;
@@ -129,9 +131,7 @@ fn compile_impl<D: Document>(
// If that doesn't happen within five attempts, we give up.
loop {
// The name of the iterations for timing scopes.
- const ITER_NAMES: &[&str] =- &["layout (1)", "layout (2)", "layout (3)", "layout (4)", "layout (5)"];- let _scope = TimingScope::new(ITER_NAMES[iter]);+ let _scope = TimingScope::new(format!("layout ({})", iter + 1).leak());
subsink = Sink::new();
@@ -154,9 +154,9 @@ fn compile_impl<D: Document>(
break;
}
- if iter >= 5 {+ if iter >= MAX_ITER {
subsink.warn(warning!(
- Span::detached(), "layout did not converge within 5 attempts";+ Span::detached(), "layout did not converge within {} attempts", MAX_ITER;
hint: "check if any states or queries are updating themselves"
));
break;
no comment on this issue but, @SolidTuxString::leak is fine there because the strings are saved for the entire program runtime anyway so nothing is really leaked compared to String.
Activity
laurmaedje commentedon Jul 9, 2024
Yeah, some way to debug this error would certainly be nice. It's a difficult problem, but at some time we'll need to figure something out.
zinifier commentedon Jul 9, 2024
I had the same problem when trying to reach a specific number of pages. Usecase: i'm making booklets so i want to add pagebreaks so that the number of pages is a multiple of 4. It's unclear to me when the total page count is updated (on every layout iteration?) and how that affects dynamic page break insertion.
EDIT: This is not a blocker for me. I simply moved the page break logic to after typst compilation.
oliverpool commentedon Jul 10, 2024
(Note: to not pollute this feature request, I opened a discussion dedicated to my specific problem and eventual solutions: #4530)
Mc-Zen commentedon Aug 6, 2024
I suggest adding a CLI argument under the name
max-layout-iterationsor similar that restricts the number of layout iterations (maximum 5). This way a document can be compiled to see how it looks after a certain number of iterations.Note that this version is comparably straight-forward and has far less implementation overhead (and potential discussions) than the originally proposed solution of outputting 5 pdf for each iteration. Still, the same is basically achieved.
For debugging purposes, it would of course be more direct to be able to change this setting right in the document, but I can also see some reasons against that.
swaits commentedon Oct 22, 2024
@zinifier how did you accomplish this? I'm having a similar problem now and seeking solutions.
zinifier commentedon Dec 24, 2024
@swaits I add blank pages to the PDF after typst compilation. Not my favorite solution but it does the trick!
swaits commentedon Dec 24, 2024
Not my favorite either, though I'm glad it helps you @zinifier.
Makes me wonder... why couldn't Typst just do the same thing, add extra pages, see if it converges, repeat a few times, then if converges just delete the extra pages before finalizing the PDF output?
It's all hacks, I agree... but this is a problem experienced by many Typstists. Any solution, temporary or otherwise, would be great.
Barring that, let us users decide on max iterations. I get that if it's not converging then some theoretical thing must be wrong. But that doesn't help me finish my already overdue papers. I, and others at my company, use Typst for real work now; and I'm quickly finding that I strongly depend on it.
@laurmaedje - how can I help here? I'm happy to legitimately make fixing this my top non-work tech priority.
davystrong commentedon Jan 29, 2025
Was just about to create a feature request for this exact feature. I agree that allowing any value will just lead to me being lazy and increasing it to make it converge, which is a bad idea. But allowing me to set the number of iterations up to a maximum of 5 would REALLY help debugging, and seems like a fairly easy thing to implement (though I obviously don't know).
laurmaedje commentedon Feb 15, 2025
Here are my initial collected thoughts on how we could improve the debugging situation. In part, this summarizes existing ideas, but there are also a few new ones.
Locating the problem
Ideally, we'd have a span / error range for what specific introspection actually didn't converge (e.g. which
query). However, it's really quite difficult to implement with how memoization works. Still, somehow we need to solve this.Visualizing how the document evolves
It would be quite useful, as a user, to have a way to step through the different iterations to understand what's happening where (I think this should primarily be an IDE feature of the web app and tinymist). This should be fairly easy to implement and it's something that's on our web app roadmap.
Analysing patterns in the state
We could try to analyse how the state evolves to give the user a bit more insight into what's going wrong.
I think the common cases of how the state evolves are
To help the user, we could try to
run a fixed number of additional iterations for diagnostic purposes (to understand whether it would converge but just not quickly enough), but still output the document from iteration 5. That would help us understand if the code does in principle converge, but too slowly (case 1). Might be frustrating though if Typst tells you "hey, it converged after 7 iterations, but I'm not gonna give you the final document".
detect state oscillations to know for sure that it cannot converge (case 2).
detect more patterns in the state (case 3) to predict whether it is likely to converge. Much fuzzier than the first two proposals. But if e.g. we have a state that evolves in a monotonic fashion, perhaps it's possible to detect that.
laurmaedje commentedon Feb 15, 2025
Providing details about the non-converging query
Also possible and much easier than providing a span is to provide just some more information about the query that didn't converge and possibly how it evolved over the iterations. Implementation-wise that's fairly easy, the challenge is more one of UX, i.e. how we would present different queries. The actual queries can be quite a bit more low-level than what the user wrote.
Sometimes, the link is still fairly clear, e.g. a non-converging
state("hi")might fail to converge on aquery(state-update.where(key: "hi"))). We could try to detect common cases and display a nice message instead of the raw query. Similarly e.g. forlocation.position()). The raw query is quite useless (a location is really just a 128-bit unique ID, so it would be something likeintrospector.position(120938120958102958)), but with a bit of work, we could resolve the location to the element and say "the position of this heading didn't stabilize". We could even provide the source location of the heading.What we couldn't easily tell you is who asked for the location of the heading. That's where having the spans of the queries themselves would come in. This is a lot harder to implement, and not always so clear-cut because the compiler itself might run queries which don't always have a associated source location (though it might be possible to derive something that's close enough, e.g. the span of a
set heading(numbering)when a heading counter doesn't converge).I'm imaging something like this (just a first draft):
davystrong commentedon Feb 17, 2025
Having the compiler tell me where things went wrong would be great. However, given the complexity of most of these options, simply giving me the option to specify the max number of iterations (up to 5) seems like an important first step. I can then produce five PDFs and check where changes happen. It's not pretty but it seems like it should be simple to implement and it'll never have any uncommon cases where it won't work. It should also be useful long term since it would be easier to detect and report edge cases for better compiler hints.
laurmaedje commentedon Feb 17, 2025
@davystrong I agree that this feature should exist, but it feels more like an IDE feature for tinymist and the web app to me than a CLI flag. There is some boundary as to what features should only be exposed via IDE versus also via CLI. To the one extreme, autocompletion is obviously one that is clearly in the IDE camp. For some of the debugging tools, I'm not sure whether in the range they should land.
davystrong commentedon Feb 17, 2025
I created the issue in tinymist. I agree it would be perfect as an IDE feature but I'm not sure I agree it would be out of place in the CLI. Unlike autocompletion (which I know was an extreme example), this isn't something only related to writing the document. It is to do with compiling the document, and therefore including the flag in the compiler seems reasonable. I may be biased though because, while I use an IDE basically all the time, I don't like it when I HAVE to use the IDE. I like that all my tools are available as CLIs. So I'll not press it any further, it's up to you.
laurmaedje commentedon Feb 17, 2025
Yeah, as said, it's not that I'm expressly against it. I just haven't made up my mind entirely.
davystrong commentedon Feb 17, 2025
@laurmaedje is this effectively the same thing (with the exception of the document now having an "__iterations" counter and the fact that the document is now inside a
contextenvironment)?Or is there something I'm missing that also affects the number of iterations? This certainly acts as I'd expect it to when included at the start of the document, but I'm not sure if imports, for example, are imported regardless.
laurmaedje commentedon Feb 17, 2025
This is cursed, I love it. I think it will probably do what you want. Imports will happen regardless, but that can't have any effect on the layout if you don't put anything into the document in your
show:rule.laurmaedje commentedon Feb 17, 2025
You could make a package out of this :)
davystrong commentedon Feb 17, 2025
I was considering creating a package but thought it might fall foul of the "Packages must not attempt to exploit the compiler" requirement. If not, I'll try to create a package at some stage when I have the time.
Also, credit where it's due, it was @Myriad-Dreamin who suggested something like this might be possible.
laurmaedje commentedon Feb 17, 2025
I wouldn't count that as an exploit. Exploit would be more in the direction of trying to break the sandbox or something like that.
davystrong commentedon Feb 18, 2025
My package to limit the number of compiler iterations is now available on typst Universe.
swaits commentedon Feb 18, 2025
Nice! Next logical step.. script generating output with this package
for iterations in [1..=5], then create a progressive diff of the images from each iteration to the next.ETA: Or, although it probably just makes the problem worse and may not help with debugging, query some set of known labels, headings, etc after
compiler()finishes its N iterations and record that in a summary. This could help guide users to what part of their document is moving the most each iteration.SolidTux commentedon Feb 20, 2025
Regardless of whether something is exposed as a CLI feature, the maximum iterations being somehow configurable is a good idea, I think. And a first step would be to expose it somewhere in the library, instead of talking constants. Is there any reason not to do this? My main question would be, which object should hold the configuration for this?
Worldor theDocument?Right now, in order to debug things, I'm just editing the source code like the following (I'm not sure about the
String::leak, but I don't think it will cause big issues).Code diff
mattfbacon commentedon Aug 25, 2025
no comment on this issue but, @SolidTux
String::leakis fine there because the strings are saved for the entire program runtime anyway so nothing is really leaked compared toString.laurmaedje commentedon Nov 12, 2025
Just opened #7364, which implements detailed non-convergence diagnostics.