Skip to content

Allow loading of files outside the project folder (in addition to --root) #5382

Open

Description

@woerndl

Description

Assume you have a file structure that shares a template or other common parts:

project/
  - common/
  - thesis-1/
  - thesis-2/

You can run typst compile thesis-1/main.typ --root ~/project/ or symlink parts (= PITA). But you can't import common files in a way that gets rid of the error message (in this case Tinymist). Because the live syntax check (be it Tinymist or any other future solution) can't know what the scope of the project is, as it is not a permanent setting.

Searching in Discord I found a couple of people with the same issue, be it for books, a thesis (split chapters) etc.

Use Case

Having --root as the only option to include files outside the current directory is, to quote another user, a "too basic of a thing not to have". I would agree with that assessment and propose considering a way for permanent relative imports outside the project root.

Activity

  1. added
    feature request
    on Nov 5, 2024
  2. added
    cli
    on Nov 7, 2024
  3. trombonehero commented on May 6, 2025

    @trombonehero

    Yes please! For out-of-tree builds that integrate with other tools (e.g., something builds an SVG file that is then incorporated into the Typst->PDF build, all within a build directory), a -I option would be invaluable.

  4. terefang commented on May 13, 2025

    @terefang

    for some usecases it might even be more beneficial to have a search path similar to #6190

  5. changed the title [-]Allow the import of files outside the project folder (in addition to --root)[/-] [+]Allow loading of files outside the project folder (in addition to --root)[/+] on Aug 2, 2025
  6. marked Global asset directory #6690 as a duplicate of this issue on Aug 2, 2025
  7. laurmaedje commented on Aug 4, 2025

    @laurmaedje
    Member

    I thought a bit more about this while looking at the --input-file PR and had the following idea: We could have something like --input-path mykey=path/to/file which will add sys.inputs.mykey of type path (which is to be added soon; this would block the design). Then you could use this path like any other. In particular, you could read from it, import/include it, or join strings to it to perform operations relative to a directory that was provided like this. In the directory case, the path would form its own detached root, and the imported Typst file could perform reads relatively & absolutely to that root, but not escape it.

    Some examples of what this would enable:

    • typst c file.typ --input-path config=config.toml -> toml(sys.inputs.config)
    • typst c file.typ --input-path img=tiger.png -> image(sys.inputs.img)
    • typst c file.typ --input-path assets=~/assets -> image(sys.inputs.assets + "path/in/asset/dir.svg")
    • typst c file.typ --input-path graph=~/graph.typ -> include sys.inputs.graph
    • wget https://data.org/data.json | typst c file.typ --input-path data=- (stdin) -> json(sys.inputs.data)
  8. terefang commented on Aug 5, 2025

    @terefang

    wow!

    could --input-path var=... --input-path var=... be used to make an array ?

  9. laurmaedje commented on Aug 5, 2025

    @laurmaedje
    Member

    That feels a bit implicit to me, but it'd be easy to write Typst code that turns --input-path var1=... --input-path var2=... into an array.

  10. terefang commented on Aug 6, 2025

    @terefang

    That feels a bit implicit to me, but it'd be easy to write Typst code that turns --input-path var1=... --input-path var2=... into an array.

    maybe --input-path var[]=... --input-path var[]=... ?

  11. woerndl commented on Aug 6, 2025

    @woerndl
    Author

    In the directory case, the path would form its own detached root, and the imported Typst file could perform reads relatively & absolutely to that root, but not escape it.

    The general approach sounds really flexible. My first thought or consideration would be whether a somewhat "separate" method of "adding" to the root would truly be equally handled in detail aspects like error handling or code suggestions (such as typing #import "…" where the file name is suggested). My second consideration would be whether the approach could also facilitate adding an entire directory, for example, including the complete subfolder thesis-1/* with chapter-1.typ, chapter-2.typ, etc. My need for this feature didn't primarily arise from importing or adding single external files, as demonstrated in examples like tiger.png or config.toml (although I understand these examples are meant to highlight the approach's flexibility). Instead, it mostly stemmed from managing documents or projects that share common parts or are split up but are not located in a single folder.

  12. laurmaedje commented on Aug 6, 2025

    @laurmaedje
    Member

    My first thought or consideration would be whether a somewhat "separate" method of "adding" to the root would truly be equally handled in detail aspects like error handling or code suggestions (such as typing #import "…" where the file name is suggested).

    It could be if the inputs were correctly configured in your language server. It wouldn't be so much adding to the root, but adding a new root. (Packages are already separate roots.)

    My second consideration would be whether the approach could also facilitate adding an entire directory, for example, including the complete subfolder thesis-1/* with chapter-1.typ, chapter-2.typ, etc. My need for this feature didn't primarily arise from importing or adding single external files, as demonstrated in examples like tiger.png or config.toml (although I understand these examples are meant to highlight the approach's flexibility). Instead, it mostly stemmed from managing documents or projects that share common parts or are split up but are not located in a single folder.

    That would be possible. If you don't know ahead of times, which files are in the folder, you would combine this feature with #2123 (which is planned).

  13. Xiretza commented on May 11, 2026

    @Xiretza
    Contributor

    We could have something like --input-path mykey=path/to/file

    (#5382 (comment))

    Is there implementation work for this already? If not, would it make sense for me to start working on it?

  14. laurmaedje commented on May 12, 2026

    @laurmaedje
    Member

    @Xiretza There isn't active work, but this would be too large for a first contribution. If you'd like to contribute, I suggest starting with something small and building up trust with the maintainers and other contributors.

  15. toine512 commented on Jul 13, 2026

    @toine512

    Hi,

    In the directory case, the path would form its own detached root, and the imported Typst file could perform reads relatively & absolutely to that root, but not escape it.

    Some examples of what this would enable:

    • typst c file.typ --input-path config=config.toml -> toml(sys.inputs.config)
    • typst c file.typ --input-path img=tiger.png -> image(sys.inputs.img)
    • typst c file.typ --input-path assets=~/assets -> image(sys.inputs.assets + "path/in/asset/dir.svg")
    • typst c file.typ --input-path graph=~/graph.typ -> include sys.inputs.graph
    • wget https://data.org/data.json | typst c file.typ --input-path data=- (stdin) -> json(sys.inputs.data)

    What is the advantage of doing this over letting paths work normally (as in: the Rust module will resolve it using OS APIs), first filtered by a path whitelist at the document input level ? The whitelist would be set via CLI in a similar way you described. Absolute path would work and be safe on Unix-like (instead of allowing everything) or work at all (Windows). Relative paths would'n be affected. Both at the same time. Sounds like a simpler solution working around edge cases (because you let the OS handle its path), and real-world use cases described in multiple instances made possible (I have one as well). In this instance: the language server would have valid paths in source documents to follow.
    What am I missing? Questioning in #4195 was never answered.

  16. laurmaedje commented on Sep 12, 2026

    @laurmaedje
    Member

    @toine512 A Typst document has multiple roots: The project root and package roots. Absolute paths refer to the root of the project and package folder respectively, depending on where they are used. Using normal OS path semantics would mean there is fundamentally just one root: The real file system's root. It would be somewhat strange how to interpret absolute paths in packages. Of course, they could also be relative to disk root, but that would not feel ideal to me as a package doesn't really have any business in reading from your OS root.

    Meanwhile, both absolute and relative paths have a useful definition in terms of a project. You could use include "chapter1.typ" as a relative path, but use image("/assets/image.jpg") from anywhere in your project to consistently refer to the assets folder in the project root.

    These semantics obviously present a trade-off compared to using absolute paths to refer to the real file system's root. But I think it's a worthwhile trade-off as it encourages use cases to be safe and reproducible across different machines, which is a requirement as soon as multiple people work on a project.

    The proposed --input-path mechanism is a natural extension to the existing roots mechanism that makes any potential irreproducible use case explicit. And it's also not really more annoying to use than an explicit white list. Meanwhile, using absolute paths to refer to the OS disk root would leave the absolute path syntax useless without an explicit whitelist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clifeature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions