{"record":{"id":"6d7ec71cc5e502a4","repo":"apple/pkl","slug":"expectedmoduleasargument-6d7ec7","errorCode":"expectedModuleAsArgument","errorMessage":"expectedModuleAsArgument","messagePattern":"expectedModuleAsArgument","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/stdlib/reflect/ReflectNodes.java","lineNumber":35,"sourceCode":"\nimport com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;\nimport com.oracle.truffle.api.dsl.Specialization;\nimport org.pkl.core.runtime.*;\nimport org.pkl.core.stdlib.ExternalMethod1Node;\nimport org.pkl.core.stdlib.ExternalMethod2Node;\nimport org.pkl.core.stdlib.ExternalPropertyNode;\nimport org.pkl.core.util.Pair;\n\n@SuppressWarnings(\"unused\")\npublic final class ReflectNodes {\n  private ReflectNodes() {}\n\n  public abstract static class Module extends ExternalMethod1Node {\n    @Specialization\n    @TruffleBoundary\n    protected VmTyped eval(VmTyped self, VmTyped module) {\n      if (!module.isModuleObject()) {\n        throw exceptionBuilder()\n            .evalError(\"expectedModuleAsArgument\")\n            .withLocation(getArg1Node())\n            .build();\n      }\n      return module.getModuleMirror();\n    }\n  }\n\n  public abstract static class moduleOf extends ExternalMethod1Node {\n    @Specialization\n    @TruffleBoundary\n    protected VmTyped eval(VmTyped self, VmTyped module) {\n      var candidate = module;\n      while (!candidate.isModuleObject()) {\n        candidate = candidate.getParent();\n        if (candidate == null) {\n          throw exceptionBuilder()\n              .bug(\"No module found in prototype chain.\")","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/stdlib/reflect/ReflectNodes.java#L17-L53","documentation":"`module.reflect()`/reflect nodes throw `expectedModuleAsArgument` when the argument passed to a reflection API expecting a module object is not one. Pkl checks `module.isModuleObject()` and throws an evalError located at the argument. Only actual module instances (the object representing a loaded Pkl module) are acceptable.","triggerScenarios":"Calling `Reflect(...)` or the `Module` reflection method with a value that is a class, typed object, String, Function, or null instead of a module object, e.g. `moduleOf(someClass)` style misuse or passing `module` from a non-module context.","commonSituations":"Confusing a module with its class or with a typed object instantiated from it; passing an imported value that is a class rather than the module; refactoring code where a variable previously held a module but now holds something else.","solutions":["Pass the module itself: import the module and pass the imported module binding (`import \"foo.pkl\"; foo.reflect()` style APIs)","Check with `isModule` before calling if the argument comes from dynamic code","If you have a class and want its module, use the class's `module` property rather than the class","Inspect the argument's type (`value.getClass()`, `value is Module`) to confirm what you actually hold"],"exampleFix":"// before\nmyClass.reflectModule()\n// after\nimport \"foo.pkl\"\nfoo.reflectModule()","handlingStrategy":"type-guard","validationCode":"if (!(arg.isModule())) {\n  throw(\"expected a module, got ${arg.getClass().simpleName}\")\n}","typeGuard":"function isModuleArg(v: Any): Boolean = v is Module","tryCatchPattern":"result = try { moduleApi(arg) } catch (e) { die(\"arg must be a module object: ${e.message}\") }","preventionTips":["Import modules and pass the imported binding, not classes or instances","Remember `module` inside a class file refers to that class's module, check scope","Add `isModule` assertions at boundaries in dynamic/generic code"],"tags":["pkl","reflection","stdlib","argument-type"],"backgroundTag":"invalid-argument-value","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}