knitr capture reports every chunk error as rlang_error (knitr wraps errors via rlang::entrace)

未关闭
#1,129 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
r, rust
领域
backend, testing

调研方向

Start with crates/quarto-core/src/engine/knitr/resources/rmd/capture.R:150 and the failing test crates/quarto-core/src/engine/knitr/capture_tests.rs:170; trace how the captured condition supplies its class and error details. Run the reproduction command with R, knitr, rmarkdown, jsonlite, and rlang installed. Done means captured errors retain the original class, with tests covering ordinary and custom errors; the issue also leaves the desired error text and R-enabled CI coverage for discussion.

由索引模型根据 Issue 内容生成。

描述

bug testing

Summary

When rlang is installed, knitr runs every chunk error through rlang::entrace(). That function wraps the original error in a new condition of class rlang_error. Our knitr capture code (crates/quarto-core/src/engine/knitr/resources/rmd/capture.R:150) records ename = class(p)[[1]], so every captured error is reported as rlang_error. The user's actual class, such as simpleError or a custom class, is lost.

Since rlang is installed in almost every real R setup, this affects most users.

Reproduction

With R, knitr, rmarkdown, jsonlite and rlang installed:

cargo nextest run -p quarto-core --lib -E 'test(knitr::capture_tests::capture_classifies_pieces)'
panicked at crates/quarto-core/src/engine/knitr/capture_tests.rs:170:13:
assertion `left == right` failed
  left: "rlang_error"
 right: "simpleError"

Fails with both:

  • R 4.6.0, knitr 1.51, rlang 1.2.0, evaluate 1.0.5
  • R 4.6.0, knitr 1.52, rlang 1.3.0, evaluate 1.0.5 (latest CRAN as of 2026-11-09)

Cause

knitr:::knit_handlers appends rlang_entrace_handler (list(error = function(e) rlang::entrace(e))) to the chunk's calling_handlers whenever knitr:::has_rlang is TRUE. Reproduced with evaluate alone:

h <- evaluate::new_output_handler(
  calling_handlers = list(error = function(e) rlang::entrace(e)))
e <- evaluate::evaluate('stop("boom")', output_handler = h)[[2]]
class(e)        # "rlang_error" "error" "condition"
class(e$error)  # "simpleError" "error" "condition"

A custom error class is also replaced: with stop(structure(class = c("myError", "error", "condition"), ...)), class(e) is rlang_error and class(e$error) is myError.

Suggested fix

In capture.R, when p inherits from rlang_error and has an $error field, read ename from class(p$error)[[1]]. Otherwise fall back to class(p)[[1]]. Then decide whether evalue and call should also come from p$error.

A related question: knitr also changes the error text to rlang's format (Error:\n! boom instead of Error in …: boom). The test only checks starts_with("Error"), so it still passes, but we should decide whether that text is what we want to show.

Why CI didn't catch this

No CI workflow installs R, and every test in capture_tests.rs skips itself through r_available() when R is missing. These tests therefore only run on developer machines that have R. It's worth adding R (with knitr and rlang) to at least one CI leg.

Found while investigating pandoc 3.13 test failures; this failure is unrelated to pandoc.

主要语言
Rust
星标
302
派生
22
平均合并
5 小时 8 分钟
30 天内合并 PR
220

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

quarto-dev/q2 的其他 Issue

查看 quarto-dev/q2 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。