If the document contained #image("build/diagram.svg"), typst --dry-run --deps would show that the compilation depends on build/diagram.svg without attempting to actually build the file. Make would then build all of the dependencies before returning for a non-dry-run invocation of typst.
The current behavior fails on the first missing file which causes two problems: if you had two diagrams, only the first is written to the deps file; you can’t tell from the exit-code if there was a syntax-error or a missing file. Keeping around last-run’s diagram.svg is inelegant, because compiled outputs shouldn’t be kept in the source tree, and the compilation should work from a freshly-cloned source tree.
But I am unable to find a --dry-run mode or equivalent flag for typst. Is this a problem that other people run in to?
Not sure if it’s applicable for you, but when I don’t want any artifacts, just to compile and see that no errors occured, I write directly to /dev/null:
I’m afraid that your original idea is theoretically impossible. For example, one can create a Typst document that loads 1.png, 2.png, …, n.png, where n is determined by the aspect ratio of 1.png. The dependencies of such document cannot be determined in advance.
However, it’s possible to achieve something similar with sys.inputs:
Add the following code to the beginning of main.typ.
By the way, there exist high-level frameworks for building images on demand when compiling a Typst document. The prequery typst package/CLI is an example.
However, I have never actually used prequery. I usually declare the images manually in Makefile (or equivalents), and use a script to check which images are not included in my document. I find that workflow more natural.