Skip to content

Added span for external file errors#5415

Closed
Dherse wants to merge 5 commits intotypst:mainfrom
Dherse:spans-external
Closed

Added span for external file errors#5415
Dherse wants to merge 5 commits intotypst:mainfrom
Dherse:spans-external

Conversation

@Dherse
Copy link
Contributor

@Dherse Dherse commented Nov 14, 2024

This adds spans for all errors that occur in files outside of Typst's source files (i.e SVG images, external JSON, etc.)

This required quite a laundry list of changes, including to the test framework most notably:

  • allow external path with syntax // Error: "/path/to/external/file" 3-5 ...
  • allow using raw byte addressing // Error: "/path/to/external/file" #3-#5 -> this is needed for handling of edge cases where errors occur on line breaks, a bit annoying, if you find a workaround, let me know

And then changes in all of the image decoding (for SVG), and all of the loaders (csv, json, xml).

There are likely stuff left to do and discussions to be had, but now I am going to bed.

Copy link
Contributor

@PgBiel PgBiel left a comment

Choose a reason for hiding this comment

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

This is a nice change! I've left some initial observations.

@Dherse
Copy link
Contributor Author

Dherse commented Nov 23, 2024

Ok, I fixed all of the current code review items ;)

Copy link
Contributor

@PgBiel PgBiel left a comment

Choose a reason for hiding this comment

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

Thanks, it's all looking much better :)

@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Nov 29, 2024

if is_newline(c) {
if was_cr && c == '\n' {
was_cr = false;
Copy link
Member

@laurmaedje laurmaedje Dec 9, 2024

Choose a reason for hiding this comment

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

can't we instead advance s in here and skip the was_cr state? (note: possibly not relevant if we do the lines thing below.)

format: ImageFormat,
alt: Option<EcoString>,
) -> StrResult<Image> {
span: Span,
Copy link
Member

Choose a reason for hiding this comment

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

As discussed on Discord, we should avoid passing spans and file IDs into memoized functions if they are only used for formatting the error message. Rather, we'd have a return type that is not SourceResult and turn it into a SourceResult using span & ID outside. (Adding this to the PR discussion for completeness' sake.)


let text = String::from_utf8_lossy(&bytes);

let mut line = 1;
Copy link
Member

Choose a reason for hiding this comment

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

This looks very similar to the Span code. The Source type can already do it (and faster than this here!) with a lines vector. Maybe we could even turn that into a reusable Lines type that is not tied to Typst source files, but can also handle its in-place edits! Especially in the context of the web app, we'll also need the UTF-16 conversion methods to be able to make use of these changes.

We could even consider memoizing the output via lines(bytes: &Bytes) -> Arc<Lines> (right now many errors in a long binary file would result in technically O(n^2) runtime).

This is a bit of larger thing and I know you're busy, so don't feel forced to work on this. Maybe I'd pick it up otherwise.

} else {
format!("`{}`", self.test.source.text()[range.clone()].replace('\n', "\\n"))
let path = system_path(file).unwrap();
let bytes = read(&path).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

Why not use self.world.file(file) here (and below)? Then we can revert the changes in world.rs.

@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Dec 9, 2024
@Dherse
Copy link
Contributor Author

Dherse commented Dec 9, 2024

@laurmaedje I didn't have time to finish it last week in the end, thanks for the review, I come back from a work trip on Thursday evening so I'll try and get to it maybe on Friday :)

@laurmaedje
Copy link
Member

Sounds good, no rush

@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Dec 17, 2024
@laurmaedje
Copy link
Member

I'll close this due to inactivity. No worries about it, I fully understand that you're quite busy! :)

We can always go back and pick this back up or use it as a base for a second attempt.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants