jrsonnet icon indicating copy to clipboard operation
jrsonnet copied to clipboard

Panic on undefined variables in evaluator::arr::get_lazy

Open cpg314 opened this issue 7 months ago • 0 comments

The following (invalid, as b is not defined) snippet

[x.value for x in std.objectKeysValues({ a: b })]

seems to trigger a panic during evaluation:

thread 'main' panicked at jrsonnet/crates/jrsonnet-evaluator/src/arr/spec.rs:658:14:
convertible: LocError((VariableIsNotDefined("b", []), StackTrace([StackTraceElement { location: Some(Source((SourcePath(SourceVirtual("input.jsonnet")), "[x.value for x in std.objectKeysValues({ a: b })]")):44-45), desc: "variable <b> access" }])))

The relevant code is here: https://github.com/CertainLach/jrsonnet/blob/74ea504236fd8735c47c48d7e156c809dba5ed5d/crates/jrsonnet-evaluator/src/arr/spec.rs#L656-L665

The following patch seems to work to propagate the error without a panic:

modified   crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -648,15 +648,14 @@ impl ArrayLike for PickObjectKeyValues {
 
 	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
 		let key = self.keys.get(index)?;
-		// Nothing can fail in the key part, yet value is still
-		// lazy-evaluated
-		Some(Thunk::evaluated(
+		Some(
 			KeyValue::into_untyped(KeyValue {
 				key: key.clone(),
 				value: self.obj.get_lazy_or_bail(key.clone()),
 			})
-			.expect("convertible"),
-		))
+			.map(Thunk::evaluated)
+			.unwrap_or_else(Thunk::errored),
+		)
 	}
LocError((VariableIsNotDefined("b", []), StackTrace([StackTraceElement { location: Some(Source((SourcePath(SourceVirtual("input.jsonnet")), "[x.value for x in std.objectKeysValues({ a: b })]")):44-45), desc: "variable <b> access" }, StackTraceElement { location: Some(Source((SourcePath(SourceVirtual("input.jsonnet")), "[x.value for x in std.objectKeysValues({ a: b })]")):1-2), desc: "variable <x> access" }])))

cpg314 avatar Jun 30 '24 11:06 cpg314