Skip to content

Strings and paths should be separate types #971

Closed

Description

@jhpratt

Assume the following directory layout and file contents.

├── demo.rs
├── include-code.typ
├── main.typ
└── sections
    └── demo.typ

demo.rs:

fn main() {}

include-code.typ:

#let include-code(lang: "rust", code_path) = raw(read(code_path), lang: lang)

main.typ:

= Demo
#include "sections/demo.typ"

sections/demo.typ:

#import "../include-code.typ": *

#include-code("../demo.rs")

Running typst c main.typ gives an error.

error: file not found (searched at ../demo.rs)
  ┌─ include-code.typ:1:54
  │
1 │ #let include-code(lang: "rust", code_path) = raw(read(code_path), lang: lang)
  │                                                       ^^^^^^^^^

help: error occurred in this call of function `include-code`
  ┌─ sections/demo.typ:3:1
  │
3 │ #include-code("../demo.rs")
  │  ^^^^^^^^^^^^^^^^^^^^^^^^^^

help: error occurred while importing this module
  ┌─ main.typ:2:9
  │
2 │ #include "sections/demo.typ"
  │          ^^^^^^^^^^^^^^^^^^^

The reason for this makes sense given knowledge of internals. The path "../demo.rs" is merely a string, and thus unmodified when passed to include-code.typ. This is despite demo.typ and include-code.typ being in different directories. For this reason, any situation where paths are expected are fundamentally unhygienic.

My recommendation is to make paths and strings separate types, such that paths can be made hygienic. A path could reasonably be constructed from a string using a built-in path function.

Activity

  1. added
    feature request
    on Apr 25, 2023
  2. user202729 commented on Apr 25, 2023

    @user202729
    Contributor

    For comparison, in TeX unless special handling is used (subfiles package I believe) all paths are from the "root" (current working) directory. So passing it through different files is not a problem.

    The same situation is true in Python and open() function. On the other hand import .abc in Python is relative to the current file in the package. And the Path type in Python doesn't affect that at all.

    Ideas? Would making #import be relative and everything else be absolute fix the problem?

  3. changed the title [-]Paths are unhygienic — strings and paths should be separate types[/-] [+]Strings and paths should be separate types[/+] on May 9, 2023
  4. added
    scripting
    on May 9, 2023
  5. laurmaedje commented on Jun 23, 2023

    @laurmaedje
    Member

    I think this issue would largely be fixed if paths within a function call wouldn't be resolved relative to that function, but relative to the module from which the function was called. Only if there is no top-level module up the call stack (e.g. for code in a show rule), the location of the function definition is taken. I think that this was actually my initial intention back when I first implemented this. I do not remember why it turned out differently.

    There could still be cases where you want the current behaviour because the path wasn't an input to the function, but rather an asset of a library. But in those cases, you could load it outside of the function and use it within.

  6. added
    change request
    and removed
    feature request
    on Nov 14, 2023
  7. Dherse commented on Nov 14, 2023

    @Dherse
    Contributor

    I think this issue would largely be fixed if paths within a function call wouldn't be resolved relative to that function, but relative to the module from which the function was called. Only if there is no top-level module up the call stack (e.g. for code in a show rule), the location of the function definition is taken. I think that this was actually my initial intention back when I first implemented this. I do not remember why it turned out differently.

    There could still be cases where you want the current behaviour because the path wasn't an input to the function, but rather an asset of a library. But in those cases, you could load it outside of the function and use it within.

    Isn't that, in essence, similar to strings having spans?

  8. laurmaedje commented on Nov 14, 2023

    @laurmaedje
    Member

    I think this issue would largely be fixed if paths within a function call wouldn't be resolved relative to that function ...

    I don't think that this idea of mine makes sense anymore. By now, I fully agree that strings and paths should be separate types. The remaining question is syntax, because a path function would be rather inconvenient if it's always required for things like image. Meanwhile, if it's not always required, the issue remains a problem in user files because people continue to use normal strings.

    Isn't that, in essence, similar to strings having spans?

    As soon as you do string manipulation (i.e. path joining), it falls apart. I think it would be brittle.

  9. jreniel commented on Jul 1, 2024

    @jreniel

    Hello dear friends!
    Just wanted to let you know I ran into this same problem. Everything with typst is working so beatifully well, except the unhygienic paths! I'd have to get creative with putting my templates in docker images and such so that the paths become "hygienic" within the workspace context, but I'd love to see Paths as a separate type. It's not just fair, but necessary!
    Thanks for the hard work, you got a committed typst user on my end!

  10. user202729 commented on Jul 1, 2024

    @user202729
    Contributor

    Idea: make a special "path literal" type p"path/to/file" which is translated by the parser to path("⟨file containing the literal⟩/path/to/file"), unless it's absolute path.

    It's a breaking change though.

    Another idea is to maintain "cwd" similar to Python, but set the "cwd" to whichever file currently being compiled and set it along #include.

  11. jreniel commented on Jul 1, 2024

    @jreniel

    "cwd"

    @user202729
    Can I change cwd right now from my main.typ, so that the paths within my template become relative to it?
    That would be a nicer workaround for me at the moment.

  12. TimeTravelPenguin commented on Oct 2, 2024

    @TimeTravelPenguin

    I wonder how this is going to handle directory vs file paths. I think it would be worth considering there being a distinction. My best experience with file paths and directories was a Haskell package because I was doing a load of dynamic work. I can see similar issues in typst if implemented too hastily.

    To give an example, I would very much like to resolve a module to import at runtime (useful for theme imports with various flavours). So, having a directory type (which will be useful when type hints are implemented) is useful. In that way, you can have a Path for either or a FilePath/DirPath for specific subset use cases.

    In terms of the user experience, they don't really need to be aware of the difference except in the implicit sense. Suppose the syntax was p"path/to/file.ext" or p"path/to/dir". In both cases, they are Path types, but one is a FilePath and the other is a DirPath. The user can identify this distinction implicitly and the only time they have to really notice it would be when they are using/making a function whose arg type was one of Path/FilePath/DirPath.

    I am not sure how you would handle this internally, such as whether you want to distinguish between relative and absolute paths, but at the very least this kind of consideration would allow for extension down the line, should that be required.

    Additionally, implicit conversion from a regular string could be a possible abstraction, but I don't really want to think about that given the current state of typst hasn't introduced such abstractions.

    I just wanted to give my 10 cents based off my experience working with path semantics and as someone who wants to make the most out of them in typst.

  13. Beiri22 commented on Apr 23, 2025

    @Beiri22
    Contributor

    I would also appreciate a separate path type, so that the path can be used relative to the directory it is "created" whereever it may be used later... Is there anything in developement so far?

  14. mewmew commented on Sep 7, 2025

    @mewmew
    Contributor

    Idea: make a special "path literal" type p"path/to/file" which is translated by the parser to path("⟨file containing the literal⟩/path/to/file"), unless it's absolute path.

    It's a breaking change though.

    I know this is bike-shedding, but I for one always thought that b"foo" and r"foo" and other special prefixes for string literals were quite ugly. It always left me with the feeling that this was a hack that somehow made it into the language.

    So, if possible, I'd paint the bike-shed without adding special prefix string literals.

    Instead, just use path when using the path type is important. And potentially allow str to be automatically converted to path in functions that only take path (or bytes) as argument, such as the image function?

  15. 1 remaining item

  16. kngrektor commented on Dec 1, 2025

    @kngrektor

    In Nix (the language) the syntax is ./path.typ or /path.typ and it's both convenient and intuitive. It's the most ergonomic path handling I've ever used in a programming language.

    From a cursory look at https://typst.app/docs/reference/syntax/#code I don't think it collides with any existing syntax and it should be fairly easy to lex.

  17. mewmew commented on Dec 1, 2025

    @mewmew
    Contributor

    In Nix (the language) the syntax is ./path.typ or /path.typ and it's both convenient and intuitive. It's the most ergonomic path handling I've ever used in a programming language.

    How would paths with spaces be handled? Using escape sequences for spaces (e.g. /path\ to\ file.typ) feels uglier than a regular string literal "/path to file.typ". At least to me personally.

    Still, cool to see other established ideas for path literals.

  18. gwennlbh commented on Dec 1, 2025

    @gwennlbh

    In Nix (the language) the syntax is ./path.typ or /path.typ and it's both convenient and intuitive. It's the most ergonomic path handling I've ever used in a programming language.

    How would paths with spaces be handled? Using escape sequences for spaces (e.g. /path\ to\ file.typ) feels uglier than a regular string literal "/path to file.typ". At least to me personally.

    Still, cool to see other established ideas for path literals.

    tbf when youre wiritng a path literal, you're writing a hardcoded file or dir name, so i feel like spaces are pretty rare in that case

  19. Andrew15-5 commented on Dec 1, 2025

    @Andrew15-5
    Contributor

    How would paths with spaces be handled?

    Same as in Nix, which has string interpolation, unlike Typst.

    {
      a = builtins.readFile ./a.nix;
      b = builtins.readFile ./${"fi le"};
    }

    If path is to be "explicit", you'd still write one char more with path("fi le"). Which I guess could be also the sugar/constructor pair like __ and emph are.

  20. ZanderBrown commented on Dec 5, 2025

    @ZanderBrown

    i feel like spaces are pretty rare in that case

    How so?

    Something also to remember here is POSIX paths are not unicode, which is part the reason you want a proper path type, so any path syntax does need a way to handle ‘escapes’ so that one can include non-unicode bytes inside an otherwise unicode typst source.

  21. kngrektor commented on Dec 5, 2025

    @kngrektor

    How would paths with spaces be handled? Using escape sequences for spaces (e.g. /path\ to\ file.typ) feels uglier than a regular string literal "/path to file.typ". At least to me personally.

    Personally I avoid spaces in paths like the plague so I haven't given it much thought. I'd argue the same problem exists with the string syntax: /path\ to\ "file".pdf vs "path to \"file\"". There's probably some good middle ground like allowing segments to use string literal syntax /unspaced-folder/"my spaced folder"/"my spaced file.png".

    In general I'd argue for simpler rules like only allowing [-_.a-zA-Z0-9] in unquoted segments pf path literals. There's always the temptation to allow for as much as possible but it quickly becomes a mess to explain.

  22. tingerrr commented on Dec 5, 2025

    @tingerrr
    Contributor

    i feel like spaces are pretty rare in that case

    How so?

    Something also to remember here is POSIX paths are not unicode, which is part the reason you want a proper path type, so any path syntax does need a way to handle ‘escapes’ so that one can include non-unicode bytes inside an otherwise unicode typst source.

    I don't think that was ever discussed as one of the motivations, but maybe I glossed over this. The primary reason for a path type is to correctly identify various resources across package boundaries.

    I'm also not sure how this matters, spaces are perfectly valid UTF-8, they're just rare in paths, partially because spaces are often used to delimit tokens.

    I don't think we should have special syntax just to allow spaces in paths, a simple path("...") will suffice for the case dedicated syntax is not general enough. This could even take bytes to allow for arbitrary non-UTF-8 paths.

  23. Andrew15-5 commented on Dec 5, 2025

    @Andrew15-5
    Contributor

    This could even take bytes to allow for arbitrary non-UTF-8 paths.

    To not introduce anything drastic, I guess this is a good enough/only way to handle such cases with current infrastructure. Because otherwise...well, the input file will not be UTF-8 encoded, and therefore it's all downhill from there. Another potential way is to use the aforementioned escapes, though not sure how exactly, since we only have the \u{} for Unicode codepoints. str(bytes((144, 208))) causes an error, which mean that maybe "path string" can be the OsString, or a regular string that is inside the path() call, idk.

  24. tingerrr commented on Dec 5, 2025

    @tingerrr
    Contributor

    To not introduce anything drastic, I guess this is a good enough/only way to handle such cases with current infrastructure. Because otherwise...well, the input file will not be UTF-8 encoded, and therefore it's all downhill from there.

    That's not quite true, a file can have a non-UTF-8 path and UTF-8 content.

    I'm not really advocating for supporting UTF-8 paths, it's just something to consider, since it was mentioned.

  25. laurmaedje commented on Dec 5, 2025

    @laurmaedje
    Member

    I'd rather not support non-UTF-8 paths. A general purpose systems language like Rust needs that, but I don't feel like Typst does.

  26. Andrew15-5 commented on Dec 5, 2025

    @Andrew15-5
    Contributor

    To be fair, it's more of a full compatibility with FS, rather than practicality. I have yet to see a non-UTF-8 path name. There is no benefit other than maybe something malicious.

    So I'm not really against UTF-8-only paths in Typst. I would be very surprised if someone is to open an issue about unsupported non-UTF-8 paths, but also having an actual use case for it.

    And this in turn will indeed save the sanity by working only with UTF-8 strings.

    Though the input_value_parser()/output_value_parser() could also be updated. If Typst language does not support non-UTF-8 paths, then why the CLI should? Doesn't really make sense to me.

  27. laurmaedje commented on Dec 10, 2025

    @laurmaedje
    Member

    Path type is implemented in #7555

  28. stanlrt commented on Jan 17, 2026

    @stanlrt

    @laurmaedje Should this doc line (or even whole section) get updated?

  29. laurmaedje commented on Jan 19, 2026

    @laurmaedje
    Member

    In principle yes, but I'd say only once Typst 0.15 is released.

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

    change requestscripting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions