Fix to display errors at the call site (#6991)

This commit is contained in:
Jonathan Tran
2025-05-15 22:11:37 -04:00
committed by GitHub
parent f6e26e0bab
commit 11620dfa6b
2 changed files with 20 additions and 7 deletions

View File

@ -1467,7 +1467,6 @@ impl Node<CallExpressionKw> {
.await
.map_err(|e| {
// Add the call expression to the source ranges.
// TODO currently ignored by the frontend
e.add_source_ranges(vec![callsite])
})?;

View File

@ -69,6 +69,7 @@ import {
UNLABELED_ARG,
} from '@src/lang/queryAstConstants'
import type { NumericType } from '@rust/kcl-lib/bindings/NumericType'
import { isTopLevelModule } from '@src/lang/util'
export type { ArrayExpression } from '@rust/kcl-lib/bindings/ArrayExpression'
export type {
@ -157,10 +158,23 @@ export function defaultSourceRange(): SourceRange {
return [0, 0, 0]
}
function firstSourceRange(error: RustKclError): SourceRange {
return error.sourceRanges.length > 0
? sourceRangeFromRust(error.sourceRanges[0])
: defaultSourceRange()
function bestSourceRange(error: RustKclError): SourceRange {
if (error.sourceRanges.length === 0) {
return defaultSourceRange()
}
// When there's an error, the call stack is unwound, and the locations are
// built up from deepest location to shallowest. So the shallowest call is
// last. That's the most useful to the user.
for (let i = error.sourceRanges.length - 1; i >= 0; i--) {
const range = error.sourceRanges[i]
// Skip ranges pointing into files that aren't the top-level module.
if (isTopLevelModule(range)) {
return sourceRangeFromRust(range)
}
}
// We didn't find a top-level module range, so just use the last one.
return sourceRangeFromRust(error.sourceRanges[error.sourceRanges.length - 1])
}
const splitErrors = (
@ -230,7 +244,7 @@ export const parse = (code: string | Error): ParseResult | Error => {
return new KCLError(
parsed.kind,
parsed.msg,
firstSourceRange(parsed),
bestSourceRange(parsed),
[],
[],
defaultArtifactGraph(),
@ -386,7 +400,7 @@ export const errFromErrWithOutputs = (e: any): KCLError => {
return new KCLError(
parsed.error.kind,
parsed.error.msg,
firstSourceRange(parsed.error),
bestSourceRange(parsed.error),
parsed.operations,
parsed.artifactCommands,
rustArtifactGraphToMap(parsed.artifactGraph),