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
When opening files for your document, the system fails if the file does not exist, as expected. But when you only use the filename as metadata or inside the document, nothing is checked. It would be nice to have a function that checks if a file exists in the source tree.
Use Case
When using file names inside the document or in metadata, misspelled file names could be detected.
Reacted by Adrian Freund, Saverio, Luca Ciucci, Complex2-老爹, Tom Sydney Kerckhove, Kasi07, drskoolie, Adriano Fantini, Janosh Riebesell, Márcio Tavares and 39 more
I'm not sure, if we are talking of the same thing. My problem is not about loading a file; really just about checking for existance without the overhead of loading the file. The typst compile is intended to panic in case of the file missing. I understand your approach different: With an optinal you can react to a missing file that is loaded. Those use cases are different.
The question is. How do you respect other conditions like: Is not a file, but a dir; exists but is not accessable? Do you just return a bool; or some kind of enum?
I think if we are specifically talking about if-file-exists(), then we proooobably want to read it, so both "not a file, but a dir" and "exists but is not accessible" must return false.
Otherwise, we would have to implement a separate path type (for convenience) like Path class from pathlib in Python:
I think, most of the possible types are not suitable for typst. We might be interested in directories and anything that reads like a file. So maybe a path type is a little over the top, that's right. In my use case I would not read the file after checking directly, but the third party application would read the file based on metadata extracted with query. So I can live with a simple boolean.
This would be very handy for a project I'm working on as well, where I describe around 400 components in a manual. Each of those has a basic JSON file, but for some I want to add an optional .typ file to go into more detail.
Currently I do not think there's a way to do that: either I add a .typ file for all components, or I'm forced to use some workarounds using external tools (e.g. create a JSON with the list of files, import it, and for each component check if it's to be loaded or not).
I am prone to implement it, but we should agree upon a interface for this. My first simple solution would be a function pathExists or exists that reveices a string and returns a boolean if it exists. A path type or even Optionals would be better but, more work and over the top for a scripting language.
Reacted by Adrian Groh, Börge Kiss and Akshay Srivatsan
Currently, no path outputting functions are accepted due to the lack of path type or similar. But maybe a bool returning function that works with paths is OK. Also, Typst uses/advises kebab case in the Typst API, so camel case should be avoided. There is also no multi-word function name yet, I think, therefore the name is something that isn't so straightforward to decide on. And it's not important now anyway.
You can create a PR if you want to, but ultimately it's up to @laurmaedje to approve of it or not. What I'm trying to say is that there is a chance that it's too soon for any implementation of the path-related functions, or the implementation would have to be altered (or not).
I agree this would be very helpful. I am applying a template to a large set (>1000 elements) of data and for some elements an image exists, for some it doesn't. The data doesn't tell me if the image exists – it's really down to a filesystem check. I have no control over the input data. When working with research data, such things are not uncommon. It would be great if the compiler wouldn't nope out but let me handle these cases gracefully.
I have a similar case. Making a template for user-supplied content (according to a schema defined elsewhere) with 17 possible json files. Right now, that's 17 variables to type in if they all exist, but if Typst could check for existence or if json could return a default value, that would go to 0.
I simplified it as much as possible, found out that the label string can be anything, but must be unique. Extracted the context function into a variable. But still no idea how it could possibly work. It's definitely only possible to come up with if you know the internal logic of the compiler. I even tried the show: doc => doc to see the structure of the document, but nothing helpful.
One thing that is still happening with context is that #panic() near the #image() will be ignored (but also remove the image).
Oh... So the main trick is abusing the uncaught error where the #image() is (whether due to panic() or image not found etc.). But the queries still don't make any sense without knowing exactly what is happening under the hood. I'm guessing this is such a crazy hack, that it's probably too unstable to use, it can stop working in any of the future versions without any notice.
P.S. @SillyFreak, it's time to add prequery(experimental: true) (that can stop working with a new version)!
@Tetragramm do you happen to have a solution for your issue? I'm in a similar situation and I'd like to refrain from using pre-processing as much as possible.
@thekief Regrettably no. I just had a loading function with a set of variables. file_type_A: false, file_type_B: false and just set the ones I had to true. It'll work until a later version.
@laurmaedje I'd like to see, if I can get something done that is a long-term solution.
In a referenced PR you mentioned that short-term solutions aren't something you want to have added.
What would constitute a long-term solution for you? I would think of a file_exists as the starting point for a file/path package that encapsulates relates functionality
i stumbled on this where i am batch-processing a bunch of similar documents, some of which have images available and others don't. my first impulse was to reach for a try-catch - which doesn't exist either?
i'd assume that being able to decide how and whether to handle errors generally would probably solve more than one problem at the same time and isn't dependent on a specific path-type implementation?
for the moment i also work with the "dirty hack" from the typstonomicon
Error handling is a much bigger topic than a path type though. There isn't a clear vision for it in Typst and I've seen surprisingly little need for it (it's not none, but less than one might think). I wouldn't want to implement something quick and dirty. The PL landscape is sort of moving away from try/catch in favor of result types (for good reason), but those also don't immediately fit naturally into Typst.
I've seen surprisingly little need for it (it's not none, but less than one might think)
Document is not your typical program that needs to gracefully shut down, or show a popup with information about an error. So usually it's either going smooth, or something is not right, and the document either should look exactly like you'd expect/want, or not compile.
That is why panicking can take you quite far, if you are not really heavy into coding. But when you are, resorting to Go-like (data, err) should be good enough. The error can be whatever: str, none, bool.
Without thinking too much about it, I do think that throwing Option or Result into the mix can significantly shift how everything is done, and increase the learning steepness, etc.
I got a use case for this feature. I have been working on some tty-ish stuff that generates RUNME.sh (as PDF attachment) and expects to obtain script execution result from RUNME.sh.out.
So here is the paradox. Without the user getting RUNME.sh, the user cannot supply RUNME.sh.out to the document, blocking successful build; without a successful document build (file not found is critical error), the generated RUNME.sh cannot be given to the user.
The label-query workaround works for this case, but a native boolean existence check will surely be better.
Another use case, since there was doubt expressed on #7555 whether this is really needed:
In my package I'm adding support for external execution of code blocks found in a Typst document, so that the execution result is automatically included back in the document. A typical workflow is:
User changes code blocks in the Typst source
A typst query command is run to export all code blocks into a Jupyter notebook and execute this notebook
typst watch detects that the notebook file has changed, so the new execution results are read and shown in the preview
This works fine except that when the user starts writing a document (before the first notebook export) my package raises an error when trying to read the notebook. So the whole time the user writes until the first export the experience is bad: error in the IDE, no preview.
With this feature, I could wait until the notebook is created before trying to access it (e.g. showing placeholders if the file is not yet present).
The label-query workaround doesn't work for my use case: my package lets the user access notebooks as a source of data that can be further manipulated in Typst, while the workaround requires context so all return values would be opaque.
(And failing on path construction would not work because the user must be able to give the (future) notebook path to my package so it can read it. My package must do the check, but can't construct the path by itself since it's external to the package.)
Activity
owiecc commentedon Aug 27, 2023
Or perhaps we could get a maybe/optional type?
Beiri22 commentedon Aug 27, 2023
How would you solve the problem with those?
owiecc commentedon Aug 27, 2023
You can throw an error if file not found. Or use some defaults instead. Whatever you feel is best depending on your situation.
Beiri22 commentedon Aug 27, 2023
I'm not sure, if we are talking of the same thing. My problem is not about loading a file; really just about checking for existance without the overhead of loading the file. The typst compile is intended to panic in case of the file missing. I understand your approach different: With an optinal you can react to a missing file that is loaded. Those use cases are different.
laurmaedje commentedon Sep 4, 2023
Related: #1877
Beiri22 commentedon Sep 4, 2023
The question is. How do you respect other conditions like: Is not a file, but a dir; exists but is not accessable? Do you just return a bool; or some kind of enum?
Andrew15-5 commentedon Sep 26, 2023
I think if we are specifically talking about
if-file-exists(), then we proooobably want to read it, so both "not a file, but a dir" and "exists but is not accessible" must returnfalse.Otherwise, we would have to implement a separate
pathtype (for convenience) likePathclass frompathlibin Python:Beiri22 commentedon Sep 26, 2023
I think, most of the possible types are not suitable for typst. We might be interested in directories and anything that reads like a file. So maybe a path type is a little over the top, that's right. In my use case I would not read the file after checking directly, but the third party application would read the file based on metadata extracted with query. So I can live with a simple boolean.
adrfantini commentedon Apr 27, 2024
This would be very handy for a project I'm working on as well, where I describe around 400 components in a manual. Each of those has a basic JSON file, but for some I want to add an optional
.typfile to go into more detail.Currently I do not think there's a way to do that: either I add a .typ file for all components, or I'm forced to use some workarounds using external tools (e.g. create a JSON with the list of files, import it, and for each component check if it's to be loaded or not).
jonsch318 commentedon Jul 11, 2024
I am prone to implement it, but we should agree upon a interface for this. My first simple solution would be a function
pathExistsorexiststhat reveices a string and returns a boolean if it exists. A path type or even Optionals would be better but, more work and over the top for a scripting language.Andrew15-5 commentedon Jul 11, 2024
Currently, no path outputting functions are accepted due to the lack of
pathtype or similar. But maybe aboolreturning function that works with paths is OK. Also, Typst uses/advises kebab case in the Typst API, so camel case should be avoided. There is also no multi-word function name yet, I think, therefore the name is something that isn't so straightforward to decide on. And it's not important now anyway.You can create a PR if you want to, but ultimately it's up to @laurmaedje to approve of it or not. What I'm trying to say is that there is a chance that it's too soon for any implementation of the path-related functions, or the implementation would have to be altered (or not).
bkis commentedon Nov 5, 2024
I agree this would be very helpful. I am applying a template to a large set (>1000 elements) of data and for some elements an image exists, for some it doesn't. The data doesn't tell me if the image exists – it's really down to a filesystem check. I have no control over the input data. When working with research data, such things are not uncommon. It would be great if the compiler wouldn't nope out but let me handle these cases gracefully.
Tetragramm commentedon Nov 8, 2024
I have a similar case. Making a template for user-supplied content (according to a schema defined elsewhere) with 17 possible json files. Right now, that's 17 variables to type in if they all exist, but if Typst could check for existence or if
jsoncould return a default value, that would go to 0.lentilus commentedon Nov 26, 2024
I found this from @laurmaedje .
It is pretty hacky but gets the job done.
I am posting it here, so others don't have to search for as long as I did :)
Andrew15-5 commentedon Nov 26, 2024
I simplified it as much as possible, found out that the label string can be anything, but must be unique. Extracted the
contextfunction into a variable. But still no idea how it could possibly work. It's definitely only possible to come up with if you know the internal logic of the compiler. I even tried theshow: doc => docto see the structure of the document, but nothing helpful.One thing that is still happening with
contextis that#panic()near the#image()will be ignored (but also remove the image).Oh... So the main trick is abusing the uncaught error where the
#image()is (whether due topanic()or image not found etc.). But the queries still don't make any sense without knowing exactly what is happening under the hood. I'm guessing this is such a crazy hack, that it's probably too unstable to use, it can stop working in any of the future versions without any notice.P.S. @SillyFreak, it's time to add
prequery(experimental: true)(that can stop working with a new version)!thekief commentedon Feb 14, 2025
@Tetragramm do you happen to have a solution for your issue? I'm in a similar situation and I'd like to refrain from using pre-processing as much as possible.
Tetragramm commentedon Feb 14, 2025
@thekief Regrettably no. I just had a loading function with a set of variables.
file_type_A: false, file_type_B: falseand just set the ones I had to true. It'll work until a later version.thekief commentedon Feb 14, 2025
@laurmaedje I'd like to see, if I can get something done that is a long-term solution.
In a referenced PR you mentioned that short-term solutions aren't something you want to have added.
What would constitute a long-term solution for you? I would think of a
file_existsas the starting point for a file/path package that encapsulates relates functionalitylaurmaedje commentedon Feb 14, 2025
I'd like to add a proper path type first (see #971). Then we can see further.
The path type is something I'd like to give a shot for the 0.14 release cycle. We'll have to see whether it fits in.
johannesrave commentedon Jul 17, 2025
i stumbled on this where i am batch-processing a bunch of similar documents, some of which have images available and others don't. my first impulse was to reach for a try-catch - which doesn't exist either?
i'd assume that being able to decide how and whether to handle errors generally would probably solve more than one problem at the same time and isn't dependent on a specific path-type implementation?
for the moment i also work with the "dirty hack" from the typstonomicon
laurmaedje commentedon Jul 17, 2025
Error handling is a much bigger topic than a path type though. There isn't a clear vision for it in Typst and I've seen surprisingly little need for it (it's not none, but less than one might think). I wouldn't want to implement something quick and dirty. The PL landscape is sort of moving away from try/catch in favor of result types (for good reason), but those also don't immediately fit naturally into Typst.
Andrew15-5 commentedon Jul 17, 2025
Document is not your typical program that needs to gracefully shut down, or show a popup with information about an error. So usually it's either going smooth, or something is not right, and the document either should look exactly like you'd expect/want, or not compile.
That is why panicking can take you quite far, if you are not really heavy into coding. But when you are, resorting to Go-like
(data, err)should be good enough. The error can be whatever:str,none,bool.Without thinking too much about it, I do think that throwing
OptionorResultinto the mix can significantly shift how everything is done, and increase the learning steepness, etc.Trayshar commentedon Nov 24, 2025
This works on my machine™
You can call a function instead of
raw(x)if you need complex logic, and you can modify theelsebranch however you likeneruthes commentedon Apr 23, 2026
I got a use case for this feature. I have been working on some tty-ish stuff that generates
RUNME.sh(as PDF attachment) and expects to obtain script execution result fromRUNME.sh.out.So here is the paradox. Without the user getting
RUNME.sh, the user cannot supplyRUNME.sh.outto the document, blocking successful build; without a successful document build (file not found is critical error), the generatedRUNME.shcannot be given to the user.The label-query workaround works for this case, but a native boolean existence check will surely be better.
sijow commentedon May 18, 2026
Another use case, since there was doubt expressed on #7555 whether this is really needed:
In my package I'm adding support for external execution of code blocks found in a Typst document, so that the execution result is automatically included back in the document. A typical workflow is:
typst querycommand is run to export all code blocks into a Jupyter notebook and execute this notebooktypst watchdetects that the notebook file has changed, so the new execution results are read and shown in the previewThis works fine except that when the user starts writing a document (before the first notebook export) my package raises an error when trying to read the notebook. So the whole time the user writes until the first export the experience is bad: error in the IDE, no preview.
With this feature, I could wait until the notebook is created before trying to access it (e.g. showing placeholders if the file is not yet present).
The label-query workaround doesn't work for my use case: my package lets the user access notebooks as a source of data that can be further manipulated in Typst, while the workaround requires context so all return values would be opaque.
(And failing on path construction would not work because the user must be able to give the (future) notebook path to my package so it can read it. My package must do the check, but can't construct the path by itself since it's external to the package.)