mirror of
https://github.com/apple/pkl.git
synced 2026-06-11 16:23:01 +02:00
Fix error rendering @ConvertProperty annotations during error reporting (#1648)
The added snippet test originally produced error "A value of type `Function2` cannot be exported." This PR actually fixes the bug twice: * By marking `ConvertProperty.render` as `hidden` so that it is skipped when the enclosing object is exported. This broke any attempts to obtain the module schema because this requires exporting all annotations on all class properties. * By changing the way that `VmUndefinedValueException.fillInHint()` obtains the module URI to avoid obtaining the module schema (and triggering the more expensive module schema generation process). It also makes function-typed annotation properties in `pkl:Command` hidden to avoid similar issues there.
This commit is contained in:
@@ -74,7 +74,7 @@ public final class VmUndefinedValueException extends VmEvalException {
|
||||
if (topLevelValue instanceof VmTyped typed && typed.isModuleObject()) {
|
||||
builder
|
||||
.append(" of module `")
|
||||
.append(typed.getModuleInfo().getModuleSchema(typed).getModuleUri())
|
||||
.append(typed.getModuleInfo().getModuleKey().getUri())
|
||||
.append('`');
|
||||
}
|
||||
builder.append('.');
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
class WrapTypeWithName extends ConvertProperty {
|
||||
// render: (Pair<String, Any>, BaseValueRenderer) -> Pair<String, Any>
|
||||
render = (p, _) ->
|
||||
Pair(p.key, new Mapping {
|
||||
[p.value.getClass().simpleName] = p.value
|
||||
})
|
||||
}
|
||||
|
||||
class Something {
|
||||
@WrapTypeWithName
|
||||
prop1: String
|
||||
prop2: Int
|
||||
}
|
||||
|
||||
a: Something = new Something {
|
||||
prop2 = 1
|
||||
}
|
||||
|
||||
// this should result in: Tried to read property `prop1` but its value is undefined.
|
||||
// any other error would result from an error in the reporting path
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
–– Pkl Error ––
|
||||
Tried to read property `prop1` but its value is undefined.
|
||||
|
||||
xx | prop1: String
|
||||
^^^^^
|
||||
at convertProperty1#Something.prop1 (file:///$snippetsDir/input/errors/convertProperty1.pkl)
|
||||
|
||||
The above error occurred when rendering path `a.prop1` of module `file:///$snippetsDir/input/errors/convertProperty1.pkl`.
|
||||
|
||||
xxx | renderer.renderDocument(value)
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
at pkl.base#Module.output.text (pkl:base)
|
||||
|
||||
xxx | if (renderer is BytesRenderer) renderer.renderDocument(value) else text.encodeToBytes("UTF-8")
|
||||
^^^^
|
||||
at pkl.base#Module.output.bytes (pkl:base)
|
||||
@@ -750,6 +750,28 @@ class EvaluatorTest {
|
||||
.hasMessageContaining("Cannot resolve dependency because there is no project found.")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `eval schema when property has a ConvertProperty annotation`() {
|
||||
// this fails when `ConvertProperty.render` is not hidden
|
||||
// because module schema generation exports all annotations
|
||||
// and this annotation has a Function2 property that is not exportable
|
||||
val evaluator = Evaluator.preconfigured()
|
||||
assertThatCode {
|
||||
evaluator.evaluateSchema(
|
||||
text(
|
||||
"""
|
||||
@ConvertProperty {
|
||||
render = (prop, ) -> prop
|
||||
}
|
||||
foo: String
|
||||
"""
|
||||
.trimIndent()
|
||||
)
|
||||
)
|
||||
}
|
||||
.doesNotThrowAnyException()
|
||||
}
|
||||
|
||||
private fun checkModule(module: PModule) {
|
||||
assertThat(module.properties.size).isEqualTo(2)
|
||||
assertThat(module.getProperty("name")).isEqualTo("pigeon")
|
||||
|
||||
Reference in New Issue
Block a user