Repository navigation
Add file path type - #7555
Add file path type#7555
path type#7555Conversation
6f312bd to
a2aa127
Compare
|
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) 😅 |
|
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 |
|
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. |
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 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 but maybe this could be made easier? |
|
A possible API: #let nb = path("notebook.ipynb", share-directory: true)
#callisto.render(nb: nb)(The user could also do |
|
When will path operations ( Some solutions to "package access user workspace" I thought of:
|
Oh... I thought the code indeed has a double space. It doesn't.
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 typst/crates/typst-library/src/foundations/path.rs Lines 111 to 112 in a2aa127 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 #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.
If the |
|
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 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.
Example: |
a2aa127 to
9d2f0d9
Compare
This doesn't answer if
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? |
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
Yes, if you provide such a closure explicitly, you can.
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. |
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. |
|
Oh, no, actually, I have another question, after I was thinking of the next path-related features. When you create a |
|
The path does not need to exist. If it had to, #7556 would be pointless. :p |
|
Out of a conversation over at #7556, I want to submit in this thread:
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 |
|
|
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? |
You are always led to the place where the wrong string is used. That's also the case now. If you call |

This PR adds a Typst-exposed
pathtype. 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
pathconstructor. 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:
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:
Breaking changes
This PR introduces two breaking changes in Typst's path handling:
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
PathBufwhich 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
cargoalso require UTF-8 paths, so we're in good company with this change. There is some further discussion about this in the docs of thecaminocrate. (We don't depend oncaminosince it still has OS-dependent behavior though.)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:
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
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. ↩