fix(runtime): resolve runtime errors against their logical module (B-08)
RuntimeError and CallFrame now carry a logical ModuleId and an origin (source/callsite). Errors are attributed at function/body boundaries so the innermost callee module wins; argument-binding errors and host/plugin/foreign boundaries are attributed to the caller callsite without fabricating a foreign stack. The CLI resolves (module_id, span) through LoadedProject::locate instead of indexing every span against the entry file. Unknown modules and out-of-range spans keep the module id and byte span but emit null line/file. Human output and frames now point at the correct file. Record the decision in ADR-0017 and supersede the ADR-0006 limitation. Add an end-to-end imported-module division-by-zero test.
This commit is contained in:
1 parent
6fed396d20
commit
509bdb1bf6
8 files changed
+390
-38
No files matched your search
+77
-16
@@ -897,22 +897,52 @@ fn load_plugin_into_host(
|
||||
.map_err(|error| format!("{}: {}", error.code(), error.message))
|
||||
}
|
||||
|
||||
fn runtime_error_json(error: &RuntimeError, project: &LoadedProject) -> serde_json::Value {
|
||||
let entry = project.entry_file();
|
||||
let span = error.span.map(|span| {
|
||||
let position = marqdo_span::LineIndex::new(&entry.source).line_col(span.start);
|
||||
json!({"line": position.line, "column": position.column, "file": entry.path.display().to_string()})
|
||||
fn runtime_location_json(
|
||||
project: &LoadedProject,
|
||||
module_id: Option<&str>,
|
||||
span: Option<marqdo_span::Span>,
|
||||
) -> serde_json::Value {
|
||||
let mut location = json!({
|
||||
"module_id": module_id,
|
||||
"span": span.map(|span| json!({"start": span.start, "end": span.end})),
|
||||
});
|
||||
if let (Some(module_id), Some(span)) = (module_id, span) {
|
||||
if let Some(located) = project.locate(module_id, span) {
|
||||
location["line"] = json!(located.line);
|
||||
location["column"] = json!(located.column);
|
||||
location["file"] = json!(located.file.path.display().to_string());
|
||||
location["relative_path"] = json!(located.file.module_id.relative_path);
|
||||
}
|
||||
}
|
||||
location
|
||||
}
|
||||
|
||||
fn runtime_error_json(error: &RuntimeError, project: &LoadedProject) -> serde_json::Value {
|
||||
let location = runtime_location_json(project, error.module_id.as_deref(), error.span);
|
||||
let frames: Vec<serde_json::Value> = error
|
||||
.frames
|
||||
.iter()
|
||||
.map(|frame| {
|
||||
let mut value = runtime_location_json(project, frame.module_id.as_deref(), frame.span);
|
||||
value["function"] = json!(frame.function);
|
||||
value
|
||||
})
|
||||
.collect();
|
||||
json!({
|
||||
"code": error.code(),
|
||||
"origin": error.origin,
|
||||
"message": error.message,
|
||||
"span": span,
|
||||
"frames": error.frames,
|
||||
"module_id": error.module_id,
|
||||
"span": location["span"],
|
||||
"line": location["line"],
|
||||
"column": location["column"],
|
||||
"file": location["file"],
|
||||
"relative_path": location["relative_path"],
|
||||
"frames": frames,
|
||||
})
|
||||
}
|
||||
|
||||
fn render_runtime_error(error: &RuntimeError, project: &LoadedProject) -> String {
|
||||
let entry = project.entry_file();
|
||||
let mut out = String::new();
|
||||
out.push_str(&format!(
|
||||
"runtime error[{}]: {}\n",
|
||||
@@ -920,16 +950,47 @@ fn render_runtime_error(error: &RuntimeError, project: &LoadedProject) -> String
|
||||
error.message
|
||||
));
|
||||
if let Some(span) = error.span {
|
||||
let position = marqdo_span::LineIndex::new(&entry.source).line_col(span.start);
|
||||
out.push_str(&format!(
|
||||
" at {}:{}:{}\n",
|
||||
entry.path.display(),
|
||||
position.line,
|
||||
position.column
|
||||
));
|
||||
match error
|
||||
.module_id
|
||||
.as_deref()
|
||||
.and_then(|module| project.locate(module, span))
|
||||
{
|
||||
Some(located) => out.push_str(&format!(
|
||||
" at {}:{}:{}\n",
|
||||
located.file.path.display(),
|
||||
located.line,
|
||||
located.column
|
||||
)),
|
||||
None => out.push_str(&format!(
|
||||
" at {}@{}..{}\n",
|
||||
error.module_id.as_deref().unwrap_or("<unknown>"),
|
||||
span.start,
|
||||
span.end
|
||||
)),
|
||||
}
|
||||
}
|
||||
for frame in &error.frames {
|
||||
out.push_str(&format!(" in function `{}`\n", frame.function));
|
||||
out.push_str(&format!(" in function `{}`", frame.function));
|
||||
if let Some(span) = frame.span {
|
||||
match frame
|
||||
.module_id
|
||||
.as_deref()
|
||||
.and_then(|module| project.locate(module, span))
|
||||
{
|
||||
Some(located) => out.push_str(&format!(
|
||||
" at {}:{}:{}",
|
||||
located.file.path.display(),
|
||||
located.line,
|
||||
located.column
|
||||
)),
|
||||
None => {
|
||||
if let Some(module) = &frame.module_id {
|
||||
out.push_str(&format!(" at {module}@{}..{}", span.start, span.end));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out.push('\n');
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
@@ -63,6 +63,16 @@ impl FileDiagnostic {
|
||||
}
|
||||
}
|
||||
|
||||
/// A resolved source location inside a loaded file.
|
||||
pub struct LocatedSource<'a> {
|
||||
/// The owning file.
|
||||
pub file: &'a LoadedFile,
|
||||
/// 1-based line.
|
||||
pub line: usize,
|
||||
/// 1-based column.
|
||||
pub column: usize,
|
||||
}
|
||||
|
||||
/// A fully loaded project ready for checking or running.
|
||||
pub struct LoadedProject {
|
||||
/// Project root.
|
||||
@@ -92,6 +102,24 @@ impl LoadedProject {
|
||||
&self.files[&self.entry]
|
||||
}
|
||||
|
||||
/// Resolve a logical `(module_id, span)` location to a loaded file.
|
||||
///
|
||||
/// Returns `None` for an unknown module or an out-of-range span; callers
|
||||
/// must not guess or fall back to the entry file.
|
||||
pub fn locate(&self, module_id: &str, span: marqdo_span::Span) -> Option<LocatedSource<'_>> {
|
||||
let module = ModuleId::parse(module_id).ok()?;
|
||||
let file = self.files.get(&module)?;
|
||||
if span.start > span.end || span.end > file.source.len() {
|
||||
return None;
|
||||
}
|
||||
let position = marqdo_span::LineIndex::new(&file.source).line_col(span.start);
|
||||
Some(LocatedSource {
|
||||
file,
|
||||
line: position.line,
|
||||
column: position.column,
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolve a physical path to its loaded or statically addressable module.
|
||||
pub fn module_id_for_path(&self, path: &Path) -> Option<ModuleId> {
|
||||
let canonical = path.canonicalize().ok()?;
|
||||
|
||||
@@ -683,3 +683,59 @@ fn plugin_inspect_and_explicit_trusted_invocation() {
|
||||
assert!(stderr_text(&bad_args).contains("call.arity"));
|
||||
assert!(stderr_text(&bad_args).contains("missing required argument `b`"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_error_in_imported_module_reports_that_module() {
|
||||
let dir = TempDir::new("cross-module-error");
|
||||
dir.write(
|
||||
"index.mq.md",
|
||||
"---\nimport:\n physics: \"./physics.mq.md\"\n---\n\n## main\n\n> 打印(physics.boom())\n",
|
||||
);
|
||||
dir.write(
|
||||
"physics.mq.md",
|
||||
"## boom\n\n说明文字。\n\n**x = 1 / 0**\n\n*x*\n",
|
||||
);
|
||||
|
||||
let output = cli(dir.path(), &["run", "index.mq.md", "--json"]);
|
||||
assert_exit(&output, 1, "division by zero is a runtime error");
|
||||
let value: serde_json::Value =
|
||||
serde_json::from_str(&stdout_text(&output)).expect("run --json emits JSON");
|
||||
let error = &value["error"];
|
||||
assert_eq!(error["code"], "runtime.division_by_zero");
|
||||
assert!(
|
||||
error["module_id"]
|
||||
.as_str()
|
||||
.unwrap_or_default()
|
||||
.ends_with("::physics.mq.md"),
|
||||
"{}",
|
||||
error["module_id"]
|
||||
);
|
||||
assert_eq!(error["relative_path"], "physics.mq.md");
|
||||
assert!(
|
||||
error["file"]
|
||||
.as_str()
|
||||
.unwrap_or_default()
|
||||
.ends_with("physics.mq.md"),
|
||||
"{}",
|
||||
error["file"]
|
||||
);
|
||||
let line = error["line"].as_u64().unwrap_or(0);
|
||||
assert!(line >= 5, "expected the failing statement line, got {line}");
|
||||
assert!(
|
||||
!error["file"]
|
||||
.as_str()
|
||||
.unwrap_or_default()
|
||||
.ends_with("index.mq.md"),
|
||||
"must not blame the entry file"
|
||||
);
|
||||
|
||||
let human = cli(dir.path(), &["run", "index.mq.md"]);
|
||||
assert_exit(&human, 1, "human rendering also fails");
|
||||
let stderr = stderr_text(&human);
|
||||
assert!(stderr.contains("physics.mq.md"), "{stderr}");
|
||||
let primary = stderr
|
||||
.lines()
|
||||
.find(|line| line.trim_start().starts_with("at "))
|
||||
.unwrap_or_default();
|
||||
assert!(primary.contains("physics.mq.md"), "{primary}");
|
||||
}
|
||||
Reference in new issue
Block a user