Fix example result span (#16395)

Refs
https://discord.com/channels/601130461678272522/614593951969574961/1403435414416654427

# Description

Previously Example result values would have a test span, which would
cause hard to understand errors for the code that uses `scope commands`.

Now they will have the span that points to `scope commands` invocation.

# User-Facing Changes

Errors referencing example results will get slightly better.

# Tests + Formatting

All pass.
This commit is contained in:
Bruce Weirdan
2025-08-09 01:29:27 +02:00
committed by GitHub
parent 06fa1784c1
commit bf83756562
2 changed files with 17 additions and 1 deletions

View File

@ -100,7 +100,7 @@ impl<'e, 's> ScopeData<'e, 's> {
record! {
"description" => Value::string(x.description, span),
"example" => Value::string(x.example, span),
"result" => x.result.unwrap_or(Value::nothing(span)),
"result" => x.result.unwrap_or(Value::nothing(span)).with_span(span),
},
span,
)

View File

@ -330,3 +330,19 @@ fn correct_scope_variables_fields() {
let actual = nu!(&inp.join("; "));
assert_eq!(actual.out, "false");
}
#[test]
fn example_results_have_valid_span() {
let inp = &[
"scope commands",
"| where name == 'do'",
"| first",
"| get examples",
"| where result == 177",
"| get 0.result",
"| metadata",
"| view span $in.span.start $in.span.end",
];
let actual = nu!(&inp.join(" "));
assert_eq!(actual.out, "scope commands");
}