Repository navigation
Closed
Labels
change requestA proposal for changes to an existing featurescriptingAbout Typst's coding capabilities
Description
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.
Reacted by Laurent Bergé, Masahiro Kitagawa, Théo Rozier, Finley Ge, ⑆ Neved4 ⑈, Janosh Riebesell, Yunan Wang, abel1502, Evan Finken, AhmedSalih3d and 26 more
Reacted by Ltrlg, HOU HUA WEI, sibeov, bdecoute, Gwenn Le Bihan, Tanja and Bastien Voirin
Reacted by sibeov and bdecoute
Reacted by Nathan McCamish and goyalyashpal
Metadata
Metadata
Assignees
Labels
change requestA proposal for changes to an existing featurescriptingAbout Typst's coding capabilities
Activity
user202729 commentedon Apr 25, 2023
For comparison, in TeX unless special handling is used (
subfilespackage 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 handimport .abcin Python is relative to the current file in the package. And thePathtype in Python doesn't affect that at all.Ideas? Would making
#importbe relative and everything else be absolute fix the problem?[-]Paths are unhygienic — strings and paths should be separate types[/-][+]Strings and paths should be separate types[/+]laurmaedje commentedon Jun 23, 2023
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.
Dherse commentedon Nov 14, 2023
Isn't that, in essence, similar to strings having spans?
laurmaedje commentedon Nov 14, 2023
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
pathfunction would be rather inconvenient if it's always required for things likeimage. Meanwhile, if it's not always required, the issue remains a problem in user files because people continue to use normal strings.As soon as you do string manipulation (i.e. path joining), it falls apart. I think it would be brittle.
jreniel commentedon Jul 1, 2024
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!
user202729 commentedon Jul 1, 2024
Idea: make a special "path literal" type
p"path/to/file"which is translated by the parser topath("⟨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.jreniel commentedon Jul 1, 2024
@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.
TimeTravelPenguin commentedon Oct 2, 2024
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
Pathfor either or aFilePath/DirPathfor 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"orp"path/to/dir". In both cases, they are Path types, but one is aFilePathand the other is aDirPath. 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 ofPath/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.
Beiri22 commentedon Apr 23, 2025
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?
mewmew commentedon Sep 7, 2025
I know this is bike-shedding, but I for one always thought that
b"foo"andr"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
pathwhen using the path type is important. And potentially allowstrto be automatically converted topathin functions that only takepath(orbytes) as argument, such as theimagefunction?1 remaining item
kngrektor commentedon Dec 1, 2025
In Nix (the language) the syntax is
./path.typor/path.typand 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.
mewmew commentedon Dec 1, 2025
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.
gwennlbh commentedon Dec 1, 2025
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
Andrew15-5 commentedon Dec 1, 2025
Same as in Nix, which has string interpolation, unlike Typst.
If
pathis to be "explicit", you'd still write one char more withpath("fi le"). Which I guess could be also the sugar/constructor pair like__andemphare.ZanderBrown commentedon Dec 5, 2025
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.
kngrektor commentedon Dec 5, 2025
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".pdfvs"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.tingerrr commentedon Dec 5, 2025
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.Andrew15-5 commentedon Dec 5, 2025
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 theOsString, or a regular string that is inside thepath()call, idk.tingerrr commentedon Dec 5, 2025
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.
laurmaedje commentedon Dec 5, 2025
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.
Andrew15-5 commentedon Dec 5, 2025
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.laurmaedje commentedon Dec 10, 2025
Path type is implemented in #7555
stanlrt commentedon Jan 17, 2026
@laurmaedje Should this doc line (or even whole section) get updated?
laurmaedje commentedon Jan 19, 2026
In principle yes, but I'd say only once Typst 0.15 is released.