Fix to display warnings when there's a fatal error (#6995)

* Fix to display warnings when there's a fatal error

* Fix JSON test
This commit is contained in:
Jonathan Tran
2025-05-15 23:22:21 -04:00
committed by GitHub
parent 23a0085c78
commit f59b806a88
7 changed files with 45 additions and 4 deletions

View File

@ -129,6 +129,7 @@ impl From<KclErrorWithOutputs> for KclError {
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct KclErrorWithOutputs { pub struct KclErrorWithOutputs {
pub error: KclError, pub error: KclError,
pub non_fatal: Vec<CompilationError>,
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]
pub operations: Vec<Operation>, pub operations: Vec<Operation>,
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]
@ -141,8 +142,10 @@ pub struct KclErrorWithOutputs {
} }
impl KclErrorWithOutputs { impl KclErrorWithOutputs {
#[allow(clippy::too_many_arguments)]
pub fn new( pub fn new(
error: KclError, error: KclError,
non_fatal: Vec<CompilationError>,
#[cfg(feature = "artifact-graph")] operations: Vec<Operation>, #[cfg(feature = "artifact-graph")] operations: Vec<Operation>,
#[cfg(feature = "artifact-graph")] artifact_commands: Vec<ArtifactCommand>, #[cfg(feature = "artifact-graph")] artifact_commands: Vec<ArtifactCommand>,
#[cfg(feature = "artifact-graph")] artifact_graph: ArtifactGraph, #[cfg(feature = "artifact-graph")] artifact_graph: ArtifactGraph,
@ -152,6 +155,7 @@ impl KclErrorWithOutputs {
) -> Self { ) -> Self {
Self { Self {
error, error,
non_fatal,
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]
operations, operations,
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]
@ -166,6 +170,7 @@ impl KclErrorWithOutputs {
pub fn no_outputs(error: KclError) -> Self { pub fn no_outputs(error: KclError) -> Self {
Self { Self {
error, error,
non_fatal: Default::default(),
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]
operations: Default::default(), operations: Default::default(),
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]

View File

@ -823,6 +823,7 @@ impl ExecutorContext {
KclErrorWithOutputs::new( KclErrorWithOutputs::new(
err, err,
exec_state.errors().to_vec(),
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]
exec_state.global.operations.clone(), exec_state.global.operations.clone(),
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]
@ -999,6 +1000,7 @@ impl ExecutorContext {
return Err(KclErrorWithOutputs::new( return Err(KclErrorWithOutputs::new(
e, e,
exec_state.errors().to_vec(),
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]
exec_state.global.operations.clone(), exec_state.global.operations.clone(),
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]
@ -1048,6 +1050,7 @@ impl ExecutorContext {
KclErrorWithOutputs::new( KclErrorWithOutputs::new(
err, err,
exec_state.errors().to_vec(),
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]
exec_state.global.operations.clone(), exec_state.global.operations.clone(),
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]
@ -1100,6 +1103,7 @@ impl ExecutorContext {
KclErrorWithOutputs::new( KclErrorWithOutputs::new(
e, e,
exec_state.errors().to_vec(),
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]
exec_state.global.operations.clone(), exec_state.global.operations.clone(),
#[cfg(feature = "artifact-graph")] #[cfg(feature = "artifact-graph")]

View File

@ -12,6 +12,7 @@ describe('test kclErrToDiagnostic', () => {
kind: 'semantic', kind: 'semantic',
msg: 'Semantic error', msg: 'Semantic error',
sourceRange: topLevelRange(0, 1), sourceRange: topLevelRange(0, 1),
nonFatal: [],
operations: [], operations: [],
artifactCommands: [], artifactCommands: [],
artifactGraph: defaultArtifactGraph(), artifactGraph: defaultArtifactGraph(),
@ -24,6 +25,7 @@ describe('test kclErrToDiagnostic', () => {
kind: 'type', kind: 'type',
msg: 'Type error', msg: 'Type error',
sourceRange: topLevelRange(4, 5), sourceRange: topLevelRange(4, 5),
nonFatal: [],
operations: [], operations: [],
artifactCommands: [], artifactCommands: [],
artifactGraph: defaultArtifactGraph(), artifactGraph: defaultArtifactGraph(),

View File

@ -24,6 +24,7 @@ export class KCLError extends Error {
kind: ExtractKind<RustKclError> | 'name' kind: ExtractKind<RustKclError> | 'name'
sourceRange: SourceRange sourceRange: SourceRange
msg: string msg: string
nonFatal: CompilationError[]
operations: Operation[] operations: Operation[]
artifactCommands: ArtifactCommand[] artifactCommands: ArtifactCommand[]
artifactGraph: ArtifactGraph artifactGraph: ArtifactGraph
@ -34,6 +35,7 @@ export class KCLError extends Error {
kind: ExtractKind<RustKclError> | 'name', kind: ExtractKind<RustKclError> | 'name',
msg: string, msg: string,
sourceRange: SourceRange, sourceRange: SourceRange,
nonFatal: CompilationError[] = [],
operations: Operation[], operations: Operation[],
artifactCommands: ArtifactCommand[], artifactCommands: ArtifactCommand[],
artifactGraph: ArtifactGraph, artifactGraph: ArtifactGraph,
@ -44,6 +46,7 @@ export class KCLError extends Error {
this.kind = kind this.kind = kind
this.msg = msg this.msg = msg
this.sourceRange = sourceRange this.sourceRange = sourceRange
this.nonFatal = nonFatal
this.operations = operations this.operations = operations
this.artifactCommands = artifactCommands this.artifactCommands = artifactCommands
this.artifactGraph = artifactGraph this.artifactGraph = artifactGraph
@ -57,6 +60,7 @@ export class KCLLexicalError extends KCLError {
constructor( constructor(
msg: string, msg: string,
sourceRange: SourceRange, sourceRange: SourceRange,
nonFatal: CompilationError[],
operations: Operation[], operations: Operation[],
artifactCommands: ArtifactCommand[], artifactCommands: ArtifactCommand[],
artifactGraph: ArtifactGraph, artifactGraph: ArtifactGraph,
@ -67,6 +71,7 @@ export class KCLLexicalError extends KCLError {
'lexical', 'lexical',
msg, msg,
sourceRange, sourceRange,
nonFatal,
operations, operations,
artifactCommands, artifactCommands,
artifactGraph, artifactGraph,
@ -81,6 +86,7 @@ export class KCLInternalError extends KCLError {
constructor( constructor(
msg: string, msg: string,
sourceRange: SourceRange, sourceRange: SourceRange,
nonFatal: CompilationError[],
operations: Operation[], operations: Operation[],
artifactCommands: ArtifactCommand[], artifactCommands: ArtifactCommand[],
artifactGraph: ArtifactGraph, artifactGraph: ArtifactGraph,
@ -91,6 +97,7 @@ export class KCLInternalError extends KCLError {
'internal', 'internal',
msg, msg,
sourceRange, sourceRange,
nonFatal,
operations, operations,
artifactCommands, artifactCommands,
artifactGraph, artifactGraph,
@ -105,6 +112,7 @@ export class KCLSyntaxError extends KCLError {
constructor( constructor(
msg: string, msg: string,
sourceRange: SourceRange, sourceRange: SourceRange,
nonFatal: CompilationError[],
operations: Operation[], operations: Operation[],
artifactCommands: ArtifactCommand[], artifactCommands: ArtifactCommand[],
artifactGraph: ArtifactGraph, artifactGraph: ArtifactGraph,
@ -115,6 +123,7 @@ export class KCLSyntaxError extends KCLError {
'syntax', 'syntax',
msg, msg,
sourceRange, sourceRange,
nonFatal,
operations, operations,
artifactCommands, artifactCommands,
artifactGraph, artifactGraph,
@ -129,6 +138,7 @@ export class KCLSemanticError extends KCLError {
constructor( constructor(
msg: string, msg: string,
sourceRange: SourceRange, sourceRange: SourceRange,
nonFatal: CompilationError[],
operations: Operation[], operations: Operation[],
artifactCommands: ArtifactCommand[], artifactCommands: ArtifactCommand[],
artifactGraph: ArtifactGraph, artifactGraph: ArtifactGraph,
@ -139,6 +149,7 @@ export class KCLSemanticError extends KCLError {
'semantic', 'semantic',
msg, msg,
sourceRange, sourceRange,
nonFatal,
operations, operations,
artifactCommands, artifactCommands,
artifactGraph, artifactGraph,
@ -153,6 +164,7 @@ export class KCLTypeError extends KCLError {
constructor( constructor(
msg: string, msg: string,
sourceRange: SourceRange, sourceRange: SourceRange,
nonFatal: CompilationError[],
operations: Operation[], operations: Operation[],
artifactCommands: ArtifactCommand[], artifactCommands: ArtifactCommand[],
artifactGraph: ArtifactGraph, artifactGraph: ArtifactGraph,
@ -163,6 +175,7 @@ export class KCLTypeError extends KCLError {
'type', 'type',
msg, msg,
sourceRange, sourceRange,
nonFatal,
operations, operations,
artifactCommands, artifactCommands,
artifactGraph, artifactGraph,
@ -177,6 +190,7 @@ export class KCLIoError extends KCLError {
constructor( constructor(
msg: string, msg: string,
sourceRange: SourceRange, sourceRange: SourceRange,
nonFatal: CompilationError[],
operations: Operation[], operations: Operation[],
artifactCommands: ArtifactCommand[], artifactCommands: ArtifactCommand[],
artifactGraph: ArtifactGraph, artifactGraph: ArtifactGraph,
@ -187,6 +201,7 @@ export class KCLIoError extends KCLError {
'io', 'io',
msg, msg,
sourceRange, sourceRange,
nonFatal,
operations, operations,
artifactCommands, artifactCommands,
artifactGraph, artifactGraph,
@ -201,6 +216,7 @@ export class KCLUnexpectedError extends KCLError {
constructor( constructor(
msg: string, msg: string,
sourceRange: SourceRange, sourceRange: SourceRange,
nonFatal: CompilationError[],
operations: Operation[], operations: Operation[],
artifactCommands: ArtifactCommand[], artifactCommands: ArtifactCommand[],
artifactGraph: ArtifactGraph, artifactGraph: ArtifactGraph,
@ -211,6 +227,7 @@ export class KCLUnexpectedError extends KCLError {
'unexpected', 'unexpected',
msg, msg,
sourceRange, sourceRange,
nonFatal,
operations, operations,
artifactCommands, artifactCommands,
artifactGraph, artifactGraph,
@ -225,6 +242,7 @@ export class KCLValueAlreadyDefined extends KCLError {
constructor( constructor(
key: string, key: string,
sourceRange: SourceRange, sourceRange: SourceRange,
nonFatal: CompilationError[],
operations: Operation[], operations: Operation[],
artifactCommands: ArtifactCommand[], artifactCommands: ArtifactCommand[],
artifactGraph: ArtifactGraph, artifactGraph: ArtifactGraph,
@ -235,6 +253,7 @@ export class KCLValueAlreadyDefined extends KCLError {
'name', 'name',
`Key ${key} was already defined elsewhere`, `Key ${key} was already defined elsewhere`,
sourceRange, sourceRange,
nonFatal,
operations, operations,
artifactCommands, artifactCommands,
artifactGraph, artifactGraph,
@ -249,6 +268,7 @@ export class KCLUndefinedValueError extends KCLError {
constructor( constructor(
key: string, key: string,
sourceRange: SourceRange, sourceRange: SourceRange,
nonFatal: CompilationError[],
operations: Operation[], operations: Operation[],
artifactCommands: ArtifactCommand[], artifactCommands: ArtifactCommand[],
artifactGraph: ArtifactGraph, artifactGraph: ArtifactGraph,
@ -259,6 +279,7 @@ export class KCLUndefinedValueError extends KCLError {
'name', 'name',
`Key ${key} has not been defined`, `Key ${key} has not been defined`,
sourceRange, sourceRange,
nonFatal,
operations, operations,
artifactCommands, artifactCommands,
artifactGraph, artifactGraph,
@ -286,6 +307,7 @@ export function lspDiagnosticsToKclErrors(
[posToOffset(doc, range.start)!, posToOffset(doc, range.end)!, 0], [posToOffset(doc, range.start)!, posToOffset(doc, range.end)!, 0],
[], [],
[], [],
[],
defaultArtifactGraph(), defaultArtifactGraph(),
{}, {},
null null
@ -311,9 +333,13 @@ export function lspDiagnosticsToKclErrors(
export function kclErrorsToDiagnostics( export function kclErrorsToDiagnostics(
errors: KCLError[] errors: KCLError[]
): CodeMirrorDiagnostic[] { ): CodeMirrorDiagnostic[] {
return errors let nonFatal: CodeMirrorDiagnostic[] = []
const errs = errors
?.filter((err) => isTopLevelModule(err.sourceRange)) ?.filter((err) => isTopLevelModule(err.sourceRange))
.map((err) => { .map((err): CodeMirrorDiagnostic => {
if (err.nonFatal.length > 0) {
nonFatal = nonFatal.concat(compilationErrorsToDiagnostics(err.nonFatal))
}
return { return {
from: err.sourceRange[0], from: err.sourceRange[0],
to: err.sourceRange[1], to: err.sourceRange[1],
@ -321,6 +347,7 @@ export function kclErrorsToDiagnostics(
severity: 'error', severity: 'error',
} }
}) })
return errs.concat(nonFatal)
} }
export function compilationErrorsToDiagnostics( export function compilationErrorsToDiagnostics(

View File

@ -463,6 +463,7 @@ theExtrude = startSketchOn(XY)
expect.any(Object), expect.any(Object),
expect.any(Object), expect.any(Object),
expect.any(Object), expect.any(Object),
expect.any(Object),
null null
) )
) )

View File

@ -47,11 +47,11 @@ it('formats numbers with units', () => {
describe('test errFromErrWithOutputs', () => { describe('test errFromErrWithOutputs', () => {
it('converts KclErrorWithOutputs to KclError', () => { it('converts KclErrorWithOutputs to KclError', () => {
const blob = const blob =
'{"error":{"kind":"internal","sourceRanges":[],"msg":"Cache busted"},"operations":[],"artifactCommands":[],"artifactGraph":{"map":{}},"filenames":{},"sourceFiles":{},"defaultPlanes":null}' '{"error":{"kind":"internal","sourceRanges":[],"msg":"Cache busted"},"nonFatal":[],"operations":[],"artifactCommands":[],"artifactGraph":{"map":{}},"filenames":{},"sourceFiles":{},"defaultPlanes":null}'
const error = errFromErrWithOutputs(blob) const error = errFromErrWithOutputs(blob)
const errorStr = JSON.stringify(error) const errorStr = JSON.stringify(error)
expect(errorStr).toEqual( expect(errorStr).toEqual(
'{"kind":"internal","sourceRange":[0,0,0],"msg":"Cache busted","operations":[],"artifactCommands":[],"artifactGraph":{},"filenames":{},"defaultPlanes":null}' '{"kind":"internal","sourceRange":[0,0,0],"msg":"Cache busted","nonFatal":[],"operations":[],"artifactCommands":[],"artifactGraph":{},"filenames":{},"defaultPlanes":null}'
) )
}) })
}) })

View File

@ -247,6 +247,7 @@ export const parse = (code: string | Error): ParseResult | Error => {
bestSourceRange(parsed), bestSourceRange(parsed),
[], [],
[], [],
[],
defaultArtifactGraph(), defaultArtifactGraph(),
{}, {},
null null
@ -401,6 +402,7 @@ export const errFromErrWithOutputs = (e: any): KCLError => {
parsed.error.kind, parsed.error.kind,
parsed.error.msg, parsed.error.msg,
bestSourceRange(parsed.error), bestSourceRange(parsed.error),
parsed.nonFatal,
parsed.operations, parsed.operations,
parsed.artifactCommands, parsed.artifactCommands,
rustArtifactGraphToMap(parsed.artifactGraph), rustArtifactGraphToMap(parsed.artifactGraph),