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
Downgrade missing references from errors to warning #4035
Coming from LaTeX, you could compile a document even if a reference is not defined. This would trigger a warning and a "??" would be printed on the resulting file.
Currently, Typst just errs and exits the whole compiler process (e.g. if using typst watch).
Use Case
A similar functionality where the error is ignored and a warning is emitted instead would help creative flow while writing so that one does not have to immediately go back to a missing reference and try to address it.
Reacted by Lukas Juhrich, Zhuoren Ye, j3m, Joshua Himmens, Henk Bierlee, Matey Krastev, Eîar Abramski-Kronenberg, Felipe Raimann, Matt Piekenbrock, salman km and 25 more
I think instead of having a specific flags just for refs we should instead think about how to implement a more generalized system for enabling/disabling warnings, then this could be extended to this specific use case as well.
Reacted by Laurenz, Matey Krastev, Derived Cat, ᡥᠠᡳᡤᡳᠶᠠ ᡥᠠᠯᠠ·ᠨᡝᡴᠣ 猫 and Eîar Abramski-Kronenberg
Yes! You want to be able to just put the references in as you're writing and add the actual reference targets at the end. An --ignore-refs flag or something would be ideal. The current behavior encourages putting in a bunch of dummy referands just to get the compiler off your back, which is worse since you might forget them.
This at the beginning of the document makes a good workaround:
#show ref: it => {
it.target
}
However, this doesn't solve the problem because now all refs will be replaced by a placeholder, even ones that work, so you can't gradually fix broken refs and have them fill in. This will do both:
#show ref: it => {
if query(it.target).len() > 0 {
it
} else {
it.target
}
}
To be honest, I'm wondering if a compiler flag would be the best solution. I think a standard (built in) package you could import that would do this for you might be better.
I would also be in favor of warnings for missing refs. Developing and debugging is easier when you can remove large parts of the document, and now everything is very strongly connected.
#show ref: it => {
if query(it.target).len() > 0 {
it
} else {
it.target
}
}
This is a great help, but unfortunately does not seem to work for citations which return () when queried. I've opened a separate issue. If someone has a workaround there, let me know.
The whole thing is that people want to just keep writing before filling in their references.
I'd argue that the compiler is right to be loud about this by default: the main use case is when the references do, in fact, exist, to the point that we have autocomplete for existing references, and I'd certainly not want to accidentally share a document with missing references at some spots (can be easy to miss when the document is large) - warnings are good, but an error is even better and ensures you won't miss it. I feel like blocking compilation unless you're certain this is what you want is a good default, and you only have to change it once per module. Warning by default feels like a step backwards on reliability, imo.
I don't think I would mind as long as the compiler tells me how to switch it to a warning in the error message
I'd argue that warning by default is better for usability. Otherwise you'll have to add preamble to every single subfile (chapters e.g.) to compile them stand-alone.
Otherwise you'll have to add preamble to every single subfile (chapters e.g.) to compile them stand-alone.
I'm fine with that tradeoff. Using subfiles standalone is already problematic for any kind of introspection, not just references, so I'm not sure this would bring enough of a gain compared to having a more general solution to provide more context to subfiles.
Besides, we could add compiler flags that permit controlling warning levels externally regardless of the file being compiled, or even project-wide settings in a future typst.toml.
Edit: This ties into what was being discussed previously in this issue regarding compiler flags for controlling warnings (regardless of their default levels), and it's worth emphasizing how warnings could be less effective as a default for the more abundantly common use case (expect all references to exist), given the following comment:
I have the exact opposite impression. The majority would rather have references be a warning, without blocking compilation.
I think arguing in terms of amount of users isn't productive in this discussion (and was a mistake in my part) because there is actually overlap between the groups "users who would like missing references to not block compilation" and "users who would like missing references to block compilation". Rather, the same user may want both at different moments.
Let's consider the two "purposes" for triggering a Typst compilation below (which would influence what would block it or not), in a usual workflow for writing a document:
Preview: when you just want to preview your document, missing references are usually OK, since you're just looking to have an idea of how it looks.
Although a missing reference might still change how the document looks due to show rules on #ref, so not all documents would produce accurate preview compilations if missing references are allowed, so it should still be toggleable somehow (see one potential proposal below).
Export: when you want to share your document to others (e.g. turn in your thesis, present your slides and so on), you certainly do not want to allow missing references for obvious reasons. For example, you wouldn't want a reviewer to ask what is this red @typstlaur text. A warning would help in this case, but it is often the case that people export with warnings anyway because they're satisfied with how everything looks and don't want to spend more precious time satisfying the Typst compiler (and thus may miss this warning which is clearly different from something like redundant **, which can be safely ignored).
Therefore, I'd argue that
Being stricter by default is better, as I'd rather have the compiler, by default, reject otherwise valid preview compilations than accept one invalid export compilation. Typst is here to ensure quality in your documents, and detecting this kind of problem (at the right time) is part of what makes the Typst compiler so useful and helpful.
But it should still be possible to make the compiler more flexible. One potential proposal here is to differentiate preview and export compilations somehow. This could be implemented by marking some errors as dismissible for preview compilations (/ "deny-by-default warnings"), and:
The web app and LSPs would compile in "preview mode" / downgrade those warnings by default when displaying the document preview, allowing the user to still preview their documents, whereas clicking the export button would either fail or at least prompt the user for confirmation, listing problems which should be given attention;
The Typst CLI is more often than not used for final exports, so it should fail compilation by default, but could simulate the same behavior through a --preview flag which downgrades dismissible errors.
Hopefully this clarifies my position better; I think this isn't a matter of catering to a certain group users or another, but rather (since those groups do have an intersection) appealing to different needs they may have in their workflow with Typst.
Reacted by Laurenz, Nikolai Neff-Sarnow, Luca Scherzer, Bastien Voirin, schmop, Philipp Niedermayer and Johannes van den Berg
Martin and I had thoughts in a similar direction. I don't like --allow-missing-refs at all, but a generic preview/draft compile that demotes certain errors to warnings and is enabled by default for previews sounds like a good idea.
Reacted by PgBiel, Matey Krastev, Matt Piekenbrock, Nikolai Neff-Sarnow, Bastien Voirin and Daniel Cook
Yes! You want to be able to just put the references in as you're writing and add the actual reference targets at the end. An --ignore-refs flag or something would be ideal. The current behavior encourages putting in a bunch of dummy referands just to get the compiler off your back, which is worse since you might forget them.
This at the beginning of the document makes a good workaround:
#show ref: it => {
it.target
}
However, this doesn't solve the problem because now all refs will be replaced by a placeholder, even ones that work, so you can't gradually fix broken refs and have them fill in. This will do both:
#show ref: it => {
if query(it.target).len() > 0 {
it
} else {
it.target
}
}
This did not work for me. Still hitting compiler errors (typst 0.11.1)
Edit: I encountered this in trying to convert a large Latex source not correctly translated from pandoc to typst, wherein I'm constantly commenting out large portions of the source and manually translate the transpiled-typst to manually written typst. Because I want instant preview while doing this, raising errors on missing label or redundant label is cumbersome.
One workaround I found was as follows (not the best, but workable).
Via regex, select all the labels in the doc source of the form eq:my_equation
All labels now exist and the document should now compile. As you proceed with uncommenting large portions of code, you can remove the labels at the end as you manually add them.
But it should still be possible to make the compiler more flexible
As I independently came to a similar conclusion (see #5710), I'd like to interject here in this conversation as well.
Differentiating preview and release is, at least in my opinion a powerful and useful feature.
I'm using typst right now for a university project, and this feature would significantly improve my workflow.
Regarding the implementation details, I think we can take inspiration from the rust compiler (specifically with cargo). Firstly, a --release flag or something similar is an easy and quick option that does 99% of what 90% of users want (I assume).
To cater to the rest, a declarative config file (like a config.toml) could expose more options.
In my case I have a very large set of notes, essentially a large survey, which I've split into several volumes. The volumes cross-reference each other, but I also like to compile and open them separately because the entire group is rather large. I am now trying to migrate from LaTeX and I've hit this stumbling block =/
It seems that from a certain version (I think typst v0.14.0), the method metioned above (using query(it.target).len() > 0) does not work for bibliography entries. My alternative solution is
Activity
Enivex commentedon Apr 29, 2024
See #1276 and #1322
m-krastev commentedon Apr 30, 2024
I would love for the functionality of #1276 to be included in the base compiler, as this should not be considered as a critical compiler failure.
laurmaedje commentedon Apr 30, 2024
If we can come up with a good design, I'm open to it.
Coekjan commentedon May 8, 2024
Maybe we can add a cli argument (like
--allow-missing-ref, or something else) to toggle the compile/watch behavior:--allow-missing-refbehavior: warn on missing ref, and print?(or something else, or something configurable by cli args) in the resultLaurenzV commentedon May 8, 2024
I think instead of having a specific flags just for refs we should instead think about how to implement a more generalized system for enabling/disabling warnings, then this could be extended to this specific use case as well.
jack-jjm commentedon May 16, 2024
Yes! You want to be able to just put the references in as you're writing and add the actual reference targets at the end. An
--ignore-refsflag or something would be ideal. The current behavior encourages putting in a bunch of dummy referands just to get the compiler off your back, which is worse since you might forget them.This at the beginning of the document makes a good workaround:
However, this doesn't solve the problem because now all refs will be replaced by a placeholder, even ones that work, so you can't gradually fix broken refs and have them fill in. This will do both:
Eismandel commentedon Jul 3, 2024
Thank you for your solution. As I tend to overlook such messages, I added some additional code
With the #show rule
With this set, I can create a new outline only displayed when there are some missing refs
This is even cooler as compiler warnings, as I tend to ignore them and with this outline, I get informed within the document directly
jack-jjm commentedon Jul 6, 2024
To be honest, I'm wondering if a compiler flag would be the best solution. I think a standard (built in) package you could import that would do this for you might be better.
laurmaedje commentedon Jul 6, 2024
At this point, I think the best solution is to just downgrade it to a warning.
hbierlee commentedon Jul 9, 2024
I would also be in favor of warnings for missing refs. Developing and debugging is easier when you can remove large parts of the document, and now everything is very strongly connected.
This is a great help, but unfortunately does not seem to work for citations which return
()when queried. I've opened a separate issue. If someone has a workaround there, let me know.[-]Add support for missing references (w/ warnings)[/-][+]Downgrade missing references from errors to warning[/+]Andrew15-5 commentedon Jul 29, 2024
Is this the line?
typst/crates/typst/src/introspection/introspector.rs
Line 246 in c4dd6fa
2 remaining items
jack-jjm commentedon Aug 4, 2024
I don't think I would mind as long as the compiler tells me how to switch it to a warning in the error message
PgBiel commentedon Aug 4, 2024
I'd argue that the compiler is right to be loud about this by default: the main use case is when the references do, in fact, exist, to the point that we have autocomplete for existing references, and I'd certainly not want to accidentally share a document with missing references at some spots (can be easy to miss when the document is large) - warnings are good, but an error is even better and ensures you won't miss it. I feel like blocking compilation unless you're certain this is what you want is a good default, and you only have to change it once per module. Warning by default feels like a step backwards on reliability, imo.
Sounds like a good idea to me!
Enivex commentedon Aug 4, 2024
I'd argue that warning by default is better for usability. Otherwise you'll have to add preamble to every single subfile (chapters e.g.) to compile them stand-alone.
PgBiel commentedon Aug 4, 2024
I'm fine with that tradeoff. Using subfiles standalone is already problematic for any kind of introspection, not just references, so I'm not sure this would bring enough of a gain compared to having a more general solution to provide more context to subfiles.
Besides, we could add compiler flags that permit controlling warning levels externally regardless of the file being compiled, or even project-wide settings in a future
typst.toml.Edit: This ties into what was being discussed previously in this issue regarding compiler flags for controlling warnings (regardless of their default levels), and it's worth emphasizing how warnings could be less effective as a default for the more abundantly common use case (expect all references to exist), given the following comment:
PgBiel commentedon Sep 17, 2024
Re: #4974 (comment)
Cc @Enivex
I think arguing in terms of amount of users isn't productive in this discussion (and was a mistake in my part) because there is actually overlap between the groups "users who would like missing references to not block compilation" and "users who would like missing references to block compilation". Rather, the same user may want both at different moments.
Let's consider the two "purposes" for triggering a Typst compilation below (which would influence what would block it or not), in a usual workflow for writing a document:
#ref, so not all documents would produce accurate preview compilations if missing references are allowed, so it should still be toggleable somehow (see one potential proposal below).@typstlaurtext. A warning would help in this case, but it is often the case that people export with warnings anyway because they're satisfied with how everything looks and don't want to spend more precious time satisfying the Typst compiler (and thus may miss this warning which is clearly different from something like redundant**, which can be safely ignored).Therefore, I'd argue that
--previewflag which downgrades dismissible errors.Hopefully this clarifies my position better; I think this isn't a matter of catering to a certain group users or another, but rather (since those groups do have an intersection) appealing to different needs they may have in their workflow with Typst.
laurmaedje commentedon Sep 17, 2024
Martin and I had thoughts in a similar direction. I don't like
--allow-missing-refsat all, but a generic preview/draft compile that demotes certain errors to warnings and is enabled by default for previews sounds like a good idea.peekxc commentedon Sep 30, 2024
This did not work for me. Still hitting compiler errors (typst 0.11.1)
Edit: I encountered this in trying to convert a large Latex source not correctly translated from pandoc to typst, wherein I'm constantly commenting out large portions of the source and manually translate the transpiled-typst to manually written typst. Because I want instant preview while doing this, raising errors on missing label or redundant label is cumbersome.
One workaround I found was as follows (not the best, but workable).
$ 1 $ eq:my_equation
on each line.
lucascherzer commentedon Jan 20, 2025
@PgBiel
As I independently came to a similar conclusion (see #5710), I'd like to interject here in this conversation as well.
Differentiating preview and release is, at least in my opinion a powerful and useful feature.
I'm using typst right now for a university project, and this feature would significantly improve my workflow.
Regarding the implementation details, I think we can take inspiration from the rust compiler (specifically with cargo). Firstly, a
--releaseflag or something similar is an easy and quick option that does 99% of what 90% of users want (I assume).To cater to the rest, a declarative config file (like a config.toml) could expose more options.
laurmaedje commentedon Jan 20, 2025
I would definitely prefer this to be opt-in instead opt out, i.e.
--draftinstead of--releaselucascherzer commentedon Feb 4, 2025
Another thing to consider is how this could be exposed to package authors.
Consider for example the fletcher package. It has the option to display grid lines for debugging purposes. You would not want to keep this on for the final version.
Having an option as a plugin author to mark certain features as "draft-only" would be great!
ukoethe commentedon Mar 2, 2025
In a draft document, I'd also like the option to render missing references as placeholders. I tried the proposed solution
It works nicely on equation references, but seems to prevent citations from being rendered at all. Can the above code be improved for this use case?
Edit: The above refers to citations in the form
@einstein1905relativity, not to#cite(<einstein1905relativity>).azuline commentedon Oct 29, 2025
In my case I have a very large set of notes, essentially a large survey, which I've split into several volumes. The volumes cross-reference each other, but I also like to compile and open them separately because the entire group is rather large. I am now trying to migrate from LaTeX and I've hit this stumbling block =/
rijuyuezhu commentedon Nov 5, 2025
It seems that from a certain version (I think typst
v0.14.0), the method metioned above (usingquery(it.target).len() > 0) does not work for bibliography entries. My alternative solution isfeat: remove option to print bibliography standalone chapter