Skip to content

Display typst compile errors to the user in a friendly way#74

Open
imphil wants to merge 2 commits into
atsphinx:mainfrom
imphil:typst-compile-errors
Open

Display typst compile errors to the user in a friendly way#74
imphil wants to merge 2 commits into
atsphinx:mainfrom
imphil:typst-compile-errors

Conversation

@imphil

@imphil imphil commented Jun 19, 2026

Copy link
Copy Markdown

Typst can throw an compile error if the input was malformed. Currently,
typst.compile() raises a TypstError exception that bubbles up to
Sphinx, which then displays a stacktrace. Unfortunately, this stacktrace
does not contain information about the typst code that actually failed
to compile.

Capture the exception, and rethrow it with the right information
included. For example, the unit test gives the following output:

E               sphinx.errors.SphinxError: Typst compilation failed for '/tmp/pytest-of-philipp/pytest-19/typst-compile-error/_build/typstpdf/index.typ': error: unclosed delimiter
E                   ┌─ ../../../../tmp/pytest-of-philipp/pytest-19/typst-compile-error/_build/typstpdf/index.typ:169:16
E                   │
E               169 │ #let unclosed = [
E                   │                 ^

Additionally, this PR resolves deprecation warnings that were introduced in Sphinx 9. I included these changes as a separate commit in this PR because the changes conflict otherwise. If you prefer to have two PRs let me know and I'll resolve the merge conflicts after the first PR goes in.

  • Do not use deprecated Builder.app member
  • Display typst compile errors to the user in a friendly way

Summary by CodeRabbit

  • Bug Fixes

    • Corrected configuration and output path handling in Typst builders to use appropriate sources
    • Fixed image path resolution during Typst translation
  • Improvements

    • Enhanced Typst compilation error reporting with detailed diagnostic messages
  • Tests

    • Added test coverage for Typst compilation error scenarios

imphil added 2 commits June 19, 2026 15:35
It's been deprecated in Sphinx 9, and will be removed in Sphinx 11.
Typst can throw an compile error if the input was malformed. Currently,
`typst.compile()` raises a TypstError exception that bubbles up to
Sphinx, which then displays a stacktrace. Unfortunately, this stacktrace
does not contain information about the typst code that actually failed
to compile.

Capture the exception, and rethrow it with the right information
included. For example, the unit test gives the following output:

```
E               sphinx.errors.SphinxError: Typst compilation failed for '/tmp/pytest-of-philipp/pytest-19/typst-compile-error/_build/typstpdf/index.typ': error: unclosed delimiter
E                   ┌─ ../../../../tmp/pytest-of-philipp/pytest-19/typst-compile-error/_build/typstpdf/index.typ:169:16
E                   │
E               169 │ #let unclosed = [
E                   │                 ^
```
@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR removes the .app. indirection from builder and writer attribute accesses: self.app.config.* becomes self.config.*, self.app.outdir becomes self.outdir, and self.builder.app.srcdir becomes self.builder.srcdir in TypstTranslator.visit_image. In TypstPDFBuilder.finish(), the Typst compilation is updated to use self.outdir-based paths and is wrapped in a try/except typst.TypstError block that re-raises as SphinxError with the diagnostic message. A new test fixture (test-typst-compile-error) with intentionally malformed Typst and a corresponding test case are added to validate the error handling path.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • atsphinx/typst#42: Modifies TypstBuilder.write_doc()'s ThemeContext construction in builders.py, overlapping directly with the config-sourcing changes in this PR.
  • atsphinx/typst#44: Restructures the builder and changes which config values are passed into ThemeContext, directly related to the attribute access refactoring in this PR.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 37.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main objective: improving Typst compilation error handling and displaying errors in a user-friendly manner.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@imphil

imphil commented Jun 19, 2026

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
tests/test_builders.py (1)

90-91: ⚡ Quick win

Assert the wrapped error includes file-context prefix.

The test currently validates Typst’s diagnostic text but not your wrapper contract (Typst compilation failed for ...). Add one assertion so regressions in the rethrow message are caught.

Proposed test tightening
-            with pytest.raises(SphinxError, match="unclosed delimiter"):
-                app.build()
+            with pytest.raises(SphinxError, match="unclosed delimiter") as exc_info:
+                app.build()
+
+            msg = str(exc_info.value)
+            assert "Typst compilation failed for" in msg
+            assert "index.typ" in msg
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_builders.py` around lines 90 - 91, The test with pytest.raises
currently only validates the Typst diagnostic message "unclosed delimiter" but
does not verify that the wrapper error message includes the file-context prefix
"Typst compilation failed for". After the app.build() call that raises the
SphinxError, add an additional assertion to capture the raised exception and
verify that its full message string contains the expected wrapper prefix,
ensuring the error rethrow logic that adds the file-context wrapper is
functioning correctly and preventing regressions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tests/test_builders.py`:
- Around line 90-91: The test with pytest.raises currently only validates the
Typst diagnostic message "unclosed delimiter" but does not verify that the
wrapper error message includes the file-context prefix "Typst compilation failed
for". After the app.build() call that raises the SphinxError, add an additional
assertion to capture the raised exception and verify that its full message
string contains the expected wrapper prefix, ensuring the error rethrow logic
that adds the file-context wrapper is functioning correctly and preventing
regressions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 01d02bbe-4a2d-45c1-b59f-27ab937adc7c

📥 Commits

Reviewing files that changed from the base of the PR and between ef2cea2 and af0b1e4.

📒 Files selected for processing (5)
  • src/atsphinx/typst/builders.py
  • src/atsphinx/typst/writer.py
  • tests/test_builders.py
  • tests/testdocs/test-typst-compile-error/conf.py
  • tests/testdocs/test-typst-compile-error/index.rst

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.

1 participant