Skip to content

Add file path type - #7555

Merged
laurmaedje merged 1 commit into
mainfrom
path-type
Jan 14, 2026
Merged

laurmaedje merged 1 commit into
mainfrom
path-type

Conversation

@laurmaedje

@laurmaedje laurmaedje commented Dec 10, 2025 •

Copy link
Copy Markdown
Member

This PR adds a Typst-exposed path type. It closes #971.

Purpose

The primary purpose of this type is to allow for construction of paths in one file that correctly resolve in another. All functionality that used to accept a path string now accepts a string or a path.

The path type is designed to allow for operations like file existence checks or path enumeration. However, those additional features are not part of this initial PR. This PR also neither introduces syntax for the path type, nor does it remove support for using strings as paths. It's an additive change that allows explicit construction of a path via the path constructor. Special syntax for paths is not explicitly planned, but also not entirely off the table.

The PR ships with two (relatively minor) breaking changes (see below).

Usage

Once constructed, a path can be freely used across the project and all packages, so that e.g. a package can read from a path passed to it by the project. An example usage of this would look like this:

// This is main.typ, with data.json next to it.
#import "@local/my-pkg:0.1.0": process
#let data-path = path("data.json")
#process(data-path)

Roots

The project and each package are separate roots that are fully isolated. The path type always represents an absolute path within the project or a package. It validates upon construction that its associated root is not escaped:

#path("../secret.txt")
error: path would escape the project root
  ┌─ hi.typ:1:6
  │
1 │ #path("../secret.txt")
  │       ^^^^^^^^^^^^^^^
  │
  = hint: cannot access files outside of the project sandbox
  = hint: you can adjust the project root with the --root argument

Breaking changes

This PR introduces two breaking changes in Typst's path handling:

  1. The Typst CLI does not accept non-Unicode input paths anymore.1 This change is necessary to let Typst switch to a fully portable, custom path representation. So far, Typst internally used Rust's PathBuf which has platform-specific behavior, leading to subtle differences in behavior across platforms (see point 2).

    Non-Unicode paths are exceedingly rare nowadays and popular tools such as cargo also require UTF-8 paths, so we're in good company with this change. There is some further discussion about this in the docs of the camino crate. (We don't depend on camino since it still has OS-dependent behavior though.)

  2. Typst now rejects paths containing backslashes in source files. These used to be allowed, but with varying behavior across platforms. On Windows, they would indicate path separators while on Linux they would be part of a component. In the web app, backslashes cannot occur in file names.

    I considered deprecating paths with backslashes before removing support. However, if a package would use backslashes, it would already be broken today across every non-Windows system, including the web app. Meanwhile, for document authors, updating paths to use forward slashes should be a very straightforward migration that's aided by the following error message:

    error: path must not contain a backslash
      ┌─ hi.typ:1:8
      │
    1 │ #import "path\\to\\file.typ"
      │         ^^^^^^^^^^^^^^^^^^^^
      │
      = hint: use forward slashes instead: `"path/to/file.typ"`
      = hint: in earlier Typst versions, backslashes indicated path separators on Windows
      = hint: this behavior is no longer supported as it is not portable
    

    Shaking out any non-portable usage of backslashes now and not burdening the new path implementation with platform-specific behavior seemed more important to me than letting document authors update a bit more slowly.

    Edit: Paths with backslashes will of course continue to be accepted by the CLI on Windows. The restriction only applies to "virtual" paths within Typst files. Real paths are segmented using Rust's platform-specific component logic when they are converted to virtual paths.

Notes

If you want to take a look at the diff, I recommend going commit by commit.

Footnotes

  1. More precisely, it does not accept paths with non-UTF-8 components in the part that exceeds the project root. The path prefix that is shared with the project root may still contain non-UTF-8 components as it does not end up in Typst's path type. ↩

@laurmaedje laurmaedje changed the title Add path type Add file path type Dec 10, 2025
@gwennlbh

gwennlbh commented Dec 10, 2025 •

Copy link
Copy Markdown

does this mean the CLI won't take in backslash-separated paths? on windows (powershell), autocompleting paths with Tab turns all slashes into backslashes (even forward ones) 😅

@laurmaedje

Copy link
Copy Markdown
Member Author

Good point, that wasn't clear in my PR description. No worries, backslashes will continue to work in the CLI on Windows. The restriction only applies to paths within Typst (sandboxed virtual paths). Real paths are segmented using Rust's normal, system-specific Path component logic when they are converted to virtual paths.

@MDLC01

MDLC01 commented Dec 10, 2025

Copy link
Copy Markdown
Collaborator

In the "path would escape the project root" error, it might be useful to display the path in the hints, in case it's not a literal in the source code.

@sijow

sijow commented Dec 10, 2025

Copy link
Copy Markdown
Contributor

On its own, code in a package cannot construct a path that lives in the project or another package. For this reason, it can also not load files from the project directory.

This will limit the functionality of packages that process files that refer to other files. For example a Markdown cell in a Jupyter notebook can include images from the notebook directory with ![](file.png). Calling #callisto.render(nb: path("notebook.ipynb")) will fail to render these images...

This may seem like an edge case but many data formats include relative paths to other files, for example HTML, 3D graphics (text formats with external texture files), various medical imaging and GIS formats, HDF5 (a generic data format used in many science/engineering applications)...

We could use hacks to work around this, for example I can make callisto.render() accept an optional callback:

#callisto.render(nb: path("notebook.ipynb"), reader: name => path(name))

but maybe this could be made easier?

@sijow

sijow commented Dec 10, 2025

Copy link
Copy Markdown
Contributor

A possible API:

#let nb = path("notebook.ipynb", share-directory: true)
#callisto.render(nb: nb)

(The user could also do #set path(share-directory: true) once at the beginning of the document.)

@QuadnucYard

Copy link
Copy Markdown

When will path operations (join, parent, stem, extension, etc.) come?

Some solutions to "package access user workspace" I thought of:

  • Join user user-provided assets root path with the file names required by the package.
  • Provide a global path constant for the workspace root for packages to use.

@Andrew15-5

Copy link
Copy Markdown
Contributor
= hint: use forward slashes instead: `"path/to/file.typ"`
= hint: in earlier Typst versions, backslashes indicated path separators on   Windows
= hint: this behavior is no longer supported as it is not portable

Oh... I thought the code indeed has a double space. It doesn't.

Edit: Paths with backslashes will of course continue to be accepted by the CLI on Windows. The restriction only applies to "virtual" paths within Typst files. Real paths are segmented using Rust's platform-specific component logic when they are converted to virtual paths.

Can you give a small example of what this means? I don't get how only virtual paths cannot have it, but actual paths can, but only on Windows...even though supposedly virtual paths are always constructed from real ones (i.e., from string)?

/// the project or another package. For this reason, it can also not load files
/// from the project directory.

I don't get it, it seems like this prevents the whole point of paths, making the example error on project root access:

// This is main.typ, with data.json next to it.
#import "@local/my-pkg:0.1.0": process
#let data-path = path("data.json")
#process(data-path)

If this also is true for the package-func(read: path => read(path)) hack that allows packages to read stuff from project root, then it's definitely a breaking change, and a way to do the same (but better?) must exist. From my last usage, this will affect not only Alexandria, but also my long-used code helper that will read the file content for a path, and then generate the listing (with provided path string as its header) + pdf.attach.

#let code = code-inner.with(read: path => read(path))

This is a very powerful feature-hack that must not go away, even if just for a few months or one release.


The unification and simplification of paths is very nice, since indeed it should immensely help with removing a ton of edge cases and in general, when working with paths in any way later down the road.

When will path operations (join, parent, stem, extension, etc.) come?

If the read: path => read(path) still works, then I guess not very soon, but I'm pretty sure it all a must-have, right with file/dir/link/etc. existence check and directory walking. If not, then at least some of that will have to be a part of this PR.

@laurmaedje

laurmaedje commented Dec 10, 2025 •

Copy link
Copy Markdown
Member Author

Oops, the docs contain a wrong sentence that is still partly left from the old docs. I'll fix that (Edit: Done).

To clarify: It is possible for a package to read from a path pointing into the project. That's the whole point of this. It's just not possible for the package to, on its own, create a path living in the project. It needs to be handed such a path by the project.

If we expose path modification operators (like joining), it would also be possible for packages to read from a whole directory tree when it's passed a single path. And unless we protect against joining .. also from files upwards of that directory (not beyond the root though).

For this reason, it needs careful consideration how the path modification operations should be exposed and which kinds of cross-root operations we want to allow. There is no concrete timeline for that yet, it might happen soon, but could also take a while.


Can you give a small example of what this means? I don't get how only virtual paths cannot have it, but actual paths can, but only on Window

Example: C:\\Users\\Laurenz\\Desktop\\Project\\src\\main.typ would be normalized within the root C:\\Users\\Laurenz\\Desktop\\Project to /src/main.typ through VirtualPath::virtualize. And when you call .realize on the virtual path /src/main.typ, it's joined to the physical root and becomes C:\\Users\\Laurenz\\Desktop\\Project\\src\\main.typ once again. The backslashes never appear in the virtual path because the real path is segmented into components using Rust's path API and those components are joined by / to create the virtual path. The backslash only works on Windows because std::path::Path::components splits by a platform-dependent path separator.

@Andrew15-5

Andrew15-5 commented Dec 10, 2025 •

Copy link
Copy Markdown
Contributor

It is possible for a package to read from a path pointing into the project. That's the whole point of this. It's just not possible for the package to, on its own, create a path living in the project. It needs to be handed such a path by the project.

This doesn't answer if read: path => read(path) will still work or will it break. It doesn't use path type, so you can easily do whatever path operations through strings, and still access any project-root stuff like that.

Example: C:\\Users\\Laurenz\\Desktop\\Project\\src\\main.typ would be normalized within the root C:\\Users\\Laurenz\\Desktop\\Project to /src/main.typ through VirtualPath::virtualize. And when you call .realize on the virtual path /src/main.typ, it's joined to the physical root and becomes C:\\Users\\Laurenz\\Desktop\\Project\\src\\main.typ once again. The backslashes never appear in the virtual path because the real path is segmented into components using Rust's path API and those components are joined by / to create the virtual path. The backslash only works on Windows because std::path::Path::components splits by a platform-dependent path separator.

No, I mean a Typst example. Conceptually, it's not that hard. What will and won't work on which platform within Typst code? The example error shows that in Typst, you are not allowed to use backslashes, yet it is somehow "allowed" on Windows platform. Is it strictly for CLI path arguments or for Typst code as well?

@laurmaedje

Copy link
Copy Markdown
Member Author

This doesn't answer if read: path => read(path) will still work or will it break.

It will continue to work, otherwise this would be included in the "Breaking changes" section. The breaking changes section is intended to be comprehensive. Even if this closure is passed to a package, the read operation happens in the project and not a package.

It doesn't use path type, so you can easily do whatever path operations through strings, and still access any project-root stuff like that.

Yes, if you provide such a closure explicitly, you can.

No, I mean a Typst example. Conceptually, it's not that hard. What will and won't work on which platform within Typst code?

Within Typst, backslashes will work on no platform, forward slashes on all. It's always the same, that's the point. It being allowed on Windows is strictly for CLI path arguments.

@Andrew15-5

Copy link
Copy Markdown
Contributor

otherwise this would be included in the "Breaking changes" section. The breaking changes section is intended to be comprehensive.

Making sure you didn't forget about it, since it's also missing from outside the "Breaking changes" section.


Great. I don't have any more questions. Though I haven't looked at the diff yet. Not sure if I'll have time for this in the upcoming weeks. Either way, the docs can be adjusted later.

@Andrew15-5

Copy link
Copy Markdown
Contributor

Oh, no, actually, I have another question, after I was thinking of the next path-related features. When you create a path object, does it error if the target does not exist upon construction? Or is it currently the same as string, i.e., only errors when trying to access the path target within read, raw, etc.?

@laurmaedje

Copy link
Copy Markdown
Member Author

The path does not need to exist. If it had to, #7556 would be pointless. :p

@blaggacao

blaggacao commented Dec 12, 2025 •

Copy link
Copy Markdown

Out of a conversation over at #7556, I want to submit in this thread:

image

When I click on this in VSCode, I'm directly led to the site where the wrong string representation of the file is put.

Is that still ensured with the current path() construction, or would be extra amendments to the PR necessary (e.g. passing position context) to make sure, the site of path declaration is also reported at the site of loading, if loading fails?

@laurmaedje laurmaedje added scripting About Typst's coding capabilities interface PRs that add to or change Typst's user-facing interface as opposed to internals or docs changes. labels Jan 5, 2026
Base automatically changed from remove-path-elem to main January 5, 2026 16:35
@TheAlgorythm

Copy link
Copy Markdown

If we expose path modification operators (like joining), it would also be possible for packages to read from a whole directory tree when it's passed a single path. And unless we protect against joining .. also from files upwards of that directory (not beyond the root though).

path could have an optional argument allow-modification. The second issue could be solved with my simple path_ratchet crate.

@blaggacao

Copy link
Copy Markdown

Post Datum:

I havn't checkt, so this concern may be invalid.

You click on an image in tinimist IDE preview, then you land in the implementation details of some library instead at the site of path definition.

Is the necessary foundations in place for an IDE to implement correct UX behaviour?

@laurmaedje

Copy link
Copy Markdown
Member Author

When I click on this in VSCode, I'm directly led to the site where the wrong string representation of the file is put.

You are always led to the place where the wrong string is used. That's also the case now. If you call image with a wrong path, the error conceptually directs you to the image call rather than the path itself; but both happen to be in the same place. It will remain the same with the path type. You will be led to the place where the path is wrongly used, not where the path is constructed. Often, both will still be the same; in particular when a string is coerced to a path. I think that's reasonable.

ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request May 24, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request May 24, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

interface PRs that add to or change Typst's user-facing interface as opposed to internals or docs changes. scripting About Typst's coding capabilities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strings and paths should be separate types

9 participants