Is there a dry-run mode for the Typst CLI?

The use-case would be for writing a Makefile for when the document depends on an externally-generated image.

main.pdf: main.typ $(shell typst --dry-run main.typ | stuff)
    typst main.typ

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:

typst compile main.typ -f pdf /dev/null

You don’t even need -f pdf as it is the default format! Simply typst compile main.typ /dev/null will do.

Hi, welcome to the forum!

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:

  1. Add the following code to the beginning of main.typ.
#let image(source, ..args) = if sys.inputs.at("dry-run", default: "") != "" {
  std.image(source, ..args)
} else {
  [#metadata(source)<image>]
}
  1. Execute the following command to get the list of images in main.typ.
typst eval --in main 'query(<image>).map(meta => meta.value)' --input dry-run=1 --format json

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.