Skip to content

Add path.exists function - #7556

Closed
laurmaedje wants to merge 1 commit into
mainfrom
path-exists
Closed

laurmaedje wants to merge 1 commit into
mainfrom
path-exists

Conversation

@laurmaedje

@laurmaedje laurmaedje commented Dec 10, 2025 •

Copy link
Copy Markdown
Member

This PR adds support for a path.exists function that checks whether a file or directory exists at a path. It depends on #7555.

The implementation right now uses the existing World infrastructure. It tries to load the path, and matches on the result to make a decision. In particular, it depends on World implementors correctly returning FileError::IsDirectory for directories.

I am not 100% decided whether we should keep it this way or whether we should extend the World interface to make this more explicit (maybe with a default impl mirroring what I've done here), also allowing it to be more efficient. We should also consider how the World interface needs to be adapted to support directory walking. Maybe there is some overlap.

The function also still needs some more documentation.

This closes #2025.

@blaggacao

blaggacao commented Dec 11, 2025 •

Copy link
Copy Markdown

Regardless of impl concerns: Is there really a use case, where separating path construction and validation makes sense?

My imagination falls short, atm. I can imagine, however, that cost of exposing this api might outweigh the benefit.

@laurmaedje

Copy link
Copy Markdown
Member Author

Regardless of impl concerns: Is there really a use case, where separating path construction and validation makes sense?

Could you elaborate? The idea is to check something "does this image exist" before calling the image function and displaying a placeholder otherwise. If the path construction failed if that path did not exist, that use case would become impossible.

@blaggacao

blaggacao commented Dec 12, 2025 •

Copy link
Copy Markdown

Got ya. What would this placeholder be useful for? - I have little depth of context / authority to hold a strong opinion.

Without that context, I could believe that it would be beneficial to fail on construction if the path does not exists. - as an implementation of a "fail early and hard" principle.

Since paths can't break out the project root, I reason that users of path are supposed to have always intricate knowledge of the repository scope, i.e. are supposed to never construct a non-existing path.

If they do, we can assume it's a genuine mistake (e.g. typing error), hence omitting to fail upon construction delays the user facing error / fix.


In a scenario where paths could indeed break out the project root scope (which they can't), I could imagine a case where a user would want to construct and inventorize on purpose a path that does not (yet - in the context of construction) exist.


To a certain extend and in this line of thinking, it feels like this feature "contradicts" the enforcement repository scope.

@QuadnucYard

QuadnucYard commented Dec 12, 2025 •

Copy link
Copy Markdown

IMO, if we were to use the design that path is always valid, we could have path.exists(str) instead to check existence.

To avoid double-checking, we may need something like path.try-new(str), which returns none if the path is not valid.

@blaggacao

blaggacao commented Dec 12, 2025 •

Copy link
Copy Markdown

IMO, if we were to use the design that path is always valid, we could have path.exist(str) instead to check existence.

I find this an interesting proposal, indeed. This would have good use cases at the boundary between old string paths and new paths and could be a valuable tool for library (package) authors.

E.g.

# project

p = path(./assets)
# lib / package

path.exists(p + "/images")

# maybe a dir walking follows to enumerate images

@laurmaedje

laurmaedje commented Dec 12, 2025 •

Copy link
Copy Markdown
Member Author

Considering that #2025 has thirty upvotes and many use cases, people seem to have lots of use cases for checking if a path exists.

Personally, I don't really see the point in failing on path construction. It complicates the API design for existence checks (e.g. path.exists(str) would again suffer from not being able to pass that to a package, i.e. you could not write a wrapper for exists that resolves properly) and also the implementation. It's also unlike how any other language treats paths.

To me, the path is just a description of something that might be in the file system and the load operation is what fails when the file does not exist.

@blaggacao

blaggacao commented Dec 12, 2025 •

Copy link
Copy Markdown

Considering that #2025 has thirty upvotes and many use cases, people seem to have lots of use cases for checking if a path exists.

Just to quickly counter the impression it might give: that's technically unrelated to the proposal, to fail on construction.

Personally, I don't really see the point in failing on path construction.

Besides the principled concern "fail fast and hard", I explained, there's also an error context coming to my mind. If the path fails later, it is harder to trace where the path had been construed, and to ease this, some additional position context would have to be passed into the path type. And knowing quickly what went wrong where with proper error context is not something of 0 value in a typo scenario, for example.

image

In VSCode, when I click here, I'm directly led to the correct code line where the wrong path had been added.

It complicates the API design for existence checks (e.g. path.exists(str) would again suffer from not being able to pass that to a package, i.e. you could not write a wrapper for exists that resolves properly) and also the implementation

I can't give a proper opinion, but if this is the tradeoff and preferences, then I indeed have nothing useful to add.

It's also unlike how any other language treats paths.

Indeed.


Overall, I think I made my point well enough for your appreciation 😄 - Thanks for this little back & forth. I'll not push the proposal further.

@cady-b cady-b mentioned this pull request Dec 18, 2025
@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 path-type to main January 14, 2026 15:46
@laurmaedje laurmaedje added the waiting-on-decision A decision must be made to proceed. label Jan 20, 2026
@tovrstra

tovrstra commented Jan 22, 2026 •

Copy link
Copy Markdown
Contributor

One small suggestion (for a future PR if any). If this gets added (hopefully), it would be beneficial to extend the JSON dependencies format with a list of files whose existence was checked.

For example, a document summary.typ has the following:

#let cases = if path("cases.json").exists() {
  json("cases.json")
} else {
  ("case1", "case2")
}

Then typst compile summary.typ --deps deps.json --deps-format json could produce one of the following two:

  • if cases.json exists:

    {
      "inputs": ["summary.typ", "cases.json"],
      "checks": ["cases.json"],
      "outputs": ["summary.pdf"],
    }
  • if cases.json does not exist:

    {
      "inputs": ["summary.typ"],
      "checks": ["cases.json"],
      "outputs": ["summary.pdf"],
    }

This would make it possible for a build system to decide that a document needs to be recompiled when a file cases.json is created. (When it does not exists yet, it cannot be included in the list of inputs.) It might also be useful to decide that it is worth trying to recompile when the cases.json file gets removed.

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-decision A decision must be made to proceed. labels Feb 9, 2026
@Beiri22

Beiri22 commented Feb 20, 2026 •

Copy link
Copy Markdown
Contributor

I like the recent movement on this topic. Any clues on the decision, yet?

@laurmaedje

Copy link
Copy Markdown
Member Author

I'm pretty busy right now, but this is still on my radar.

@laurmaedje laurmaedje added foundations Related to the foundations category, which contains foundational types and elements. and removed scripting About Typst's coding capabilities labels Mar 2, 2026
@laurmaedje

Copy link
Copy Markdown
Member Author

The PR here is too hacky. This will need proper World support. I'll definitely revisit this later, but close it for now.

@laurmaedje laurmaedje closed this Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

foundations Related to the foundations category, which contains foundational types and elements. interface PRs that add to or change Typst's user-facing interface as opposed to internals or docs changes. waiting-on-author Pull request waits on author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Check if file exists

5 participants