Skip to content

Improving error messages in case of package not found - #6433

Closed
Vanille-N wants to merge 16 commits into
typst:mainfrom
Vanille-N:main
Closed

Vanille-N wants to merge 16 commits into
typst:mainfrom
Vanille-N:main

Conversation

@Vanille-N

@Vanille-N Vanille-N commented Jun 11, 2025 •

Copy link
Copy Markdown

I talked about this a few hours ago on a half-related issue, and here I am!

Several of Typst's error messages in the case of a file not found or package not found are hard to debug because the error message does not specify the path that was searched.

Combined with dynamic loading of files or the conventions as to where the package directory is, this results in messages such as

error: failed to load file (access denied)
  ┌─ pending/main.typ:2:14
  │
2 │ #let _ = read(path)
  │               ^^^^
  │

or

error: package not found (searched for @local/mypkg:0.0.1)
  ┌─ pending/main.typ:1:8
  │
1 │ #import "@local/mypkg:0.0.1"
  │

which are unhelpful.

This PR changes the error messages in the following cases:

@local namespace doesn't exist

- error: package not found (searched for @local/mypkg:0.0.1)
+ error: package @local/mypkg:0.0.1 not found
+ = hint: the namespace @local should be located at /home/user/.local/share/typst/packages/local

package in @local doesn't exist

- error: package not found (searched for @local/mypkg:0.0.1)
+ error: package @local/mypkg:0.0.1 not found
+ = hint: the registry at /home/user/.local/share/typst/packages/local does not have package 'mypkg'

package in @local is missing this version

- error: package not found (searched for @local/mypkg:0.0.1)
+ error: package version @local/mypkg:1.0.0 not found
+ = hint: the package mypkg exists, but not with version 1.0.0
+ = hint: the registry at  /home/user/.local/share/typst/packages/local provides up to version 0.0.1

package in @local has no manifest

- error: failed to load file (access denied)
+ error: failed to load file /home/user/.local/share/typst/packages/local/mypkg/0.0.1/typst.toml (access denied)

package in @preview doesn't exist

- error: package not found (searched for @preview/mypkg:0.0.1)
+ error: package @preview/mypkg:0.0.1 not found
+ = hint: the registry at https://packages.typst.org/preview does not have package 'mypkg'

package in @preview is missing this version

- error: package found, but version 1.0.0 does not exist (latest is 0.0.1)
+ error: package version @preview/mypkg:1.0.0 not found
+ = hint: the package mypkg exists, but not with version 1.0.0
+ = hint: the registry at https://packages.typst.org/preview provides up to version 0.0.1

access denied to file

- error: failed to load file (access denied)
+ error: failed to load file ../Cargo.toml (access denied)

Minor changes to functions and types

  • download_package should return the PathBuf in case of success, it spares us one exists check.
  • variants of FileError now contain some PathBufs to remember which file they tried to access

Major change to trait

In the previous version the path from PackageError (what the function that tries to load a package returns) to SourceDiagnostic (errors that can be printed with a span) was through the following detailed chain:

  • explicit impl Display for PackageError
  • explicit impl Display for FileError defers to PackageError: Display
  • explicit impl From<FileError> for EcoString invokes FileError: Display
  • implicit impl Into<EcoString> for FileError generated from EcoString: From<FileError>
  • blanket impl<T, S> At<T> for Result<T, S> where S: Into<EcoString>

This path means that FileError and thus by extension PackageError can only provide a single line's worth of error message.

In this PR the new path is

  • explicit impl Display for PackageError provides the header
  • explicit impl Display for FileError defers to the above
  • new method write_diagnostics(self) -> EcoVec<EcoString> in impl PackageError provides the hints
  • new method write_diagnostics(self) -> EcoVec<EcoString> in impl FileError defers to the above
  • explicit impl ErrAt for FileError (new trait) invokes both FileError: Display and FileError::write_diagnostics and returns a SourceDiagnostic

If you have a better solution in mind I can adapt, or if this is fine then I'll just add a bit of documentation.

Comment thread crates/typst-kit/src/package.rs Resolved
Comment thread crates/typst-kit/src/package.rs Resolved
Comment thread crates/typst-kit/src/package.rs Resolved
@Vanille-N

Copy link
Copy Markdown
Author

Yeah I tried to figure out how to have those code paths be explored by the unit tests but I failed. Apart from 'failed to load file (is a directory)' it seems that none of the corresponding paths were being tested initially.

@laurmaedje any suggestions on how to proceed ? Is it acceptable for this logic to remain untested as it was ?

@laurmaedje

Copy link
Copy Markdown
Member

It's okay to skip tests for now. We'll add CLI tests at some point in the future.

@Vanille-N

Copy link
Copy Markdown
Author

Alright, thanks. Then I'm happy with the current state of this
Still open to continue working on it if you think that the error messages have become too long and some of the information needs to be moved to hints.

@Vanille-N
Vanille-N marked this pull request as ready for review June 24, 2025 12:39
@tingerrr

Copy link
Copy Markdown
Contributor

Still open to continue working on it if you think that the error messages have become too long and some of the information needs to be moved to hints.

I think we should do that, the error messages can be fairly long in these cases, if we move parts into the hints they get a little more digestible in my opinion.

error: package not found
  ┌─ pending/main.typ:1:8
  │
1 │ #import "@local/mypkg:0.0.1"
  │
  = hint: the namespace '@local' does not have package 'mypkg'
  = hint: searched at /home/user/.local/share/typst/packages/local/mypkg

The first hint could be adjusted according to the actual failure (namespace missing, package missing, file within missing, etc.).

I think, generally, what is placed in parentheses can almost always be moved to a hint.

@tingerrr tingerrr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noted a few things down, thanks for taking my suggestions into consideration!

"the registry at {registry} provides up to version {version}"
),
]
} else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the else branch here happen realistically, for typst init? I'd assume that, if there is no version for a package, then the package does not exist, in which case it should emit the NotFound variant.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the file structure is namespace/package/version, I see a difference between NotFound (namespace/package doesn't exist) vs VersionNotFound(None) (namespace/package exists but is empty),
and that was the logic behind keeping it as a VersionNotFound.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I only considered it originating from the registry, good point, although the package path existing with no version directories in it is more likely an indicator for an aborted downloading process. The error message makes it seems like the registry returned a package with no versions (which should be impossible).

Comment thread crates/typst-library/src/diag.rs Resolved
Comment thread crates/typst-library/src/diag.rs Resolved
@Vanille-N

Copy link
Copy Markdown
Author

Urgh. Ignore that, I made a mistake while rebasing.

@Vanille-N

Copy link
Copy Markdown
Author

Alright, I'm back to this.

Regarding the utf-8 error requiring a path: every call site has a path accessible. I'm not sure if id.vpath().as_rooted_path() is the best path to use, but at least it is something.

PackageRegistry is now a struct instead of a raw EcoString.

I assume the currently failing tests are just due to small changes is the wording of some messages, it'll be a quick fix.

@Vanille-N

Copy link
Copy Markdown
Author

Oh. No idea how to solve that.

mismatched message (tests/suite\scripting\import.typ:323):
    annotated | tried to read /assets/images/rhino.png
    emitted   | tried to read \assets\images\rhino.png

@tingerrr

tingerrr commented Sep 1, 2025

Copy link
Copy Markdown
Contributor

Oh. No idea how to solve that.

mismatched message (tests/suite\scripting\import.typ:323):
    annotated | tried to read /assets/images/rhino.png
    emitted   | tried to read \assets\images\rhino.png

Ah, that'll require some change to the test runner, I think there are two obvious solutions:

  • Add some support for special variables that the runner will interpolate before doing comparisons of the diagnostics, such as $PATH_SEPARATOR being turned into \ on Windows and / otherwise.
  • Always use Unix paths in diagnostic annotations and change how the strings are compared by making / compare equal to \.

I would go with the former because it gives greater control when these characters should be substituted, and it can be re-used for other things too. Though I'm not sure about the syntax for it.

@laurmaedje

Copy link
Copy Markdown
Member

The test runner already has a hack for this, but it appears that it only happens for the message, not the hints:

typst/tests/src/run.rs

Lines 231 to 235 in 3d64f7b

let message = if diag.message.contains("\\u{") {
&diag.message
} else {
&diag.message.replace("\\", "/")
};

@laurmaedje

Copy link
Copy Markdown
Member

The hack for keeping \u{..} intact is fairly recent and shows the limits of the current approach. On the other hand, something like $PATH_SEPARATOR would get noisy really quickly as there a lot of tests with paths in the error message.

@tingerrr

tingerrr commented Sep 1, 2025

Copy link
Copy Markdown
Contributor

The hack for keeping \u{..} intact is fairly recent and shows the limits of the current approach. On the other hand, something like $PATH_SEPARATOR would get noisy really quickly as there a lot of tests with paths in the error message.

Yes it's quite annoying really, I have not yet found a nice way to make unit tests behave nicely when they compare paths.

In other projects, I've thought about adding some sort of compile-time flag to switch paths to a normalized form for unit tests regardless of platform. It is the only approach I could think of that doesn't seem like a hack, but it's quite the scope creep and extra work for path printing.

Should they just go with doing the same hack for annotations for now?

@laurmaedje

Copy link
Copy Markdown
Member

Should they just go with doing the same hack for annotations for now?

That would be okay with me. Would be good to factor it out into a function though.

@Vanille-N

Vanille-N commented Sep 1, 2025 •

Copy link
Copy Markdown
Author

There will be an additional hack needed though.

rg 'Error:.*\\' reveals that the only instance of \ in error messages is for \u{...},
however there are more in Hints.

$ rg 'Hint:.*\\'
tests/suite/scripting/blocks.typ
140:// Hint: 1-2 try using a backslash escape: \]
149:// Hint: 10-11 try using a backslash escape: \]
160:// Hint: 6-7 try using a backslash escape: \]
164:// Hint: 4-5 try using a backslash escape: \]
169:// Hint: 7-8 try using a backslash escape: \]
175:// Hint: 4-5 try using a backslash escape: \]

tests/suite/model/cite.typ
148:// Hint: 7-17 use `label("%@&#*!\\")` to create a label

I'll think of a way to not make it too ad-hoc.

@tingerrr

tingerrr commented Sep 1, 2025

Copy link
Copy Markdown
Contributor

Let me know if you figure something nice out, and I'll use it in my test suites too.

@Vanille-N
Vanille-N force-pushed the main branch 2 times, most recently from 2c1b721 to 8be061e Compare September 15, 2025 13:33
@Vanille-N

Copy link
Copy Markdown
Author

So in principle this approach works.
Let me know if you find that satisfactory.

@laurmaedje laurmaedje added the diagnostics Improvements to compiler errors label Oct 27, 2025
@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Nov 17, 2025
@laurmaedje

Copy link
Copy Markdown
Member

@Vanille-N please excuse the long radio silence. I was primarily shying away from landing this because I wasn't confident in the changes in the public API. However, with #7555 the public API will change quite a bit anyway, so it's a good opportunity to take another stab at this. My plan was to re-review and reconsider the public API changes once that PR has landed and this PR here is rebased.

@Vanille-N

Copy link
Copy Markdown
Author

Alright. Then I'll ignore merge conflicts until #7555 lands and rebase all at once.

@Vanille-N
Vanille-N marked this pull request as draft December 17, 2025 08:49
@laurmaedje laurmaedje added blocked This PR is blocked by something. and removed waiting-on-review This PR is waiting to be reviewed. labels Jan 8, 2026
@Vanille-N

Copy link
Copy Markdown
Author

I tried to have a look at the new API, and it seems it's going to be a challenge to get a concrete path from a VirtualRoot. Am I missing something ?

@laurmaedje

Copy link
Copy Markdown
Member

In principle, the API should be roughly isomorphic to the old one (VirtualPath::realize versus the old VirtualPath::resolve). I am currently still making some changes in the area though, which would result in further conflicts. These should land very soon. I'll let you know once that's the case and we'll discuss it, alright?

@laurmaedje laurmaedje removed the blocked This PR is blocked by something. label Jan 19, 2026
@laurmaedje

Copy link
Copy Markdown
Member

#7710 has landed, so we should now be ready to proceed here.

I tried to have a look at the new API, and it seems it's going to be a challenge to get a concrete path from a VirtualRoot. Am I missing something ?

I'm not sure what exactly you mean, maybe you can elaborate. Also, feel free to ping me on Discord to discuss this.

@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Jan 20, 2026
@laurmaedje

Copy link
Copy Markdown
Member

Hi @Vanille-N, are you still interested in working on this? If not, that's also totally fine, but then I would close the PR.

@Vanille-N

Copy link
Copy Markdown
Author

Ah, yeah sorry for letting this get stale.

What happened is that the merge conflicts were too big to rebase, to the point that the final result was a bit nonsensical. This would have to be redone from scratch.

I'm still interested in solving this issue eventually, but it won't be this PR and probably not right now.

@Vanille-N Vanille-N closed this Mar 18, 2026
@laurmaedje laurmaedje removed the waiting-on-author Pull request waits on author label Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diagnostics Improvements to compiler errors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants