fix(view): avoid stdin tracebacks in static snapshots

Static export skips cells that read stdin when the document has no preset stdin, and shows a short explanation instead of a traceback. Preset stdin still executes normally.
This commit is contained in:
cflmy committed 2026-10-18 13:14:17 +08:00
1 parent 9da12c9093
commit 518f8e386b
2 files changed
+32 -1

No files matched your search

+1 -1
View File
@@ -5,7 +5,7 @@
### Added
- **view Cell stdin**:view 增加 `Cell stdin / input` 输入框,Run/Step/Run All 都会传给外联 cells;无状态 cell 使用 raw stdin,持久内核使用 `marqdo_input`;静态导出使用 frontmatter `stdin` 作为构建时输入。
- **foreign 拥有视图会话**:新增 `foreign.session kernels= cells=`,返回 foreign 会话句柄;`view` 只消费 `会话` 绑定,不再识别 `内核表` / `会话表` 保留名。没有 `会话` 时,view cells 使用无状态一次性执行,不创建内核。清除输出已接通 `/api/session/clear`,普通 view 与 foreign cells 共用,并由 Execution 面板的 Clear 按钮清除页面输出;无 `会话` 的 view 在 Run All 时执行整个文档并回写 Execution(不再空转);静态导出与动态 view 共用同一套 View 外壳和按钮布局;静态页面执行按钮点击显示“静态页面无法真实执行”,不伪造成功;静态导出与 view 的 Run All 共用同一 foreign_doc `run_all` 路径,成功/失败行为一致;构建时实际执行代码单元并把 stdout/stderr 注入代码卡,失败也显示真实错误;移除 `In []` / `Out []` 与 Notebook 品牌;没有新增 CLI 或第二个交互模式。
- **foreign 拥有视图会话**:新增 `foreign.session kernels= cells=`,返回 foreign 会话句柄;`view` 只消费 `会话` 绑定,不再识别 `内核表` / `会话表` 保留名。没有 `会话` 时,view cells 使用无状态一次性执行,不创建内核。清除输出已接通 `/api/session/clear`,普通 view 与 foreign cells 共用,并由 Execution 面板的 Clear 按钮清除页面输出;无 `会话` 的 view 在 Run All 时执行整个文档并回写 Execution(不再空转);静态导出与动态 view 共用同一套 View 外壳和按钮布局;静态页面执行按钮点击显示“静态页面无法真实执行”,不伪造成功;静态导出与 view 的 Run All 共用同一 foreign_doc `run_all` 路径;构建时实际执行代码单元并把 stdout/stderr 注入代码卡;没有提供 frontmatter `stdin` 时,读取 stdin 的示例不执行,显示说明而不是 traceback;移除 `In []` / `Out []` 与 Notebook 品牌;没有新增 CLI 或第二个交互模式。
### Fixed
+31
View File
@@ -50,6 +50,13 @@ fn record_field(record: &Value, key: &str) -> Option<Value> {
/// Run the document's cells at export time and keep their real outputs. This
/// is what makes a static page show foreign results instead of just source.
fn cell_needs_stdin(source: &str) -> bool {
source.contains("sys.stdin")
|| source.contains("process.stdin")
|| source.contains("readFileSync(0")
|| source.contains("readFileSync(0,")
}
fn static_cell_outputs(
root: &ViewRoot,
abs: &std::path::Path,
@@ -59,6 +66,23 @@ fn static_cell_outputs(
let Ok(mut session) = DocumentSession::open(abs, &root.root) else {
return out;
};
if stdin.trim().is_empty() {
let mut run_order = Vec::new();
for cell in session.cells.iter() {
if cell_needs_stdin(&cell.source) {
out.insert(
cell.name.clone(),
StaticCellOutput {
ok: true,
text: "(static: this cell reads stdin; its input is supplied by foreign.call — see Execution)".to_string(),
},
);
} else {
run_order.push(cell.name.clone());
}
}
session.order = run_order;
}
let Ok(summary) = session.run_all_with_stdin(false, Some(stdin)) else {
session.close();
return out;
@@ -239,6 +263,13 @@ fn resolve_abs(root: &ViewRoot, rel: &str) -> Result<PathBuf> {
mod tests {
use super::*;
#[test]
fn detects_stdin_dependent_cells() {
assert!(cell_needs_stdin("import sys\njson.load(sys.stdin)"));
assert!(cell_needs_stdin("process.stdin.on('data', () => {})"));
assert!(!cell_needs_stdin("console.log('hello')"));
}
#[test]
fn injects_visible_static_cell_outputs() {
let html = r#"<div class="card" data-code-name="a"></div><pre class="code-out" hidden></pre>"#;