{"record":{"id":"22e201096e304438","repo":"apple/pkl","slug":"modulecannotamendself","errorCode":"moduleCannotAmendSelf","errorMessage":"Module `{0}` cannot amend itself.","messagePattern":"Module `(.+?)` cannot amend itself\\.","errorType":"exception","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/literal/AmendModuleNode.java","lineNumber":65,"sourceCode":"\n  public ModuleInfo getModuleInfo() {\n    return moduleInfo;\n  }\n\n  @Override\n  @TruffleBoundary\n  protected AmendModuleNode copy(ExpressionNode newParentNode) {\n    throw exceptionBuilder().unreachableCode().build();\n  }\n\n  @Specialization\n  protected VmTyped eval(VirtualFrame frame, VmTyped supermodule) {\n    // receiver is empty module object\n    var module = VmUtils.getTypedObjectReceiver(frame);\n\n    if (module == supermodule) {\n      CompilerDirectives.transferToInterpreter();\n      throw exceptionBuilder()\n          .evalError(\"moduleCannotAmendSelf\", moduleInfo.getModuleName())\n          .build();\n    }\n\n    checkIsValidTypedAmendment(supermodule);\n\n    module.lateInitVmClass(supermodule.getVmClass());\n    module.lateInitParent(supermodule);\n    module.addProperties(members);\n\n    module.setExtraStorage(moduleInfo);\n    moduleInfo.initAnnotations(VmUtils.evaluateAnnotations(frame, annotationNodes));\n\n    return module;\n  }\n}\n","sourceCodeStart":47,"sourceCodeEnd":82,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/literal/AmendModuleNode.java#L47-L82","documentation":"A module tried to amend itself, e.g. `module amends` with a reference that resolves back to the same module, or `X` where X is the enclosing module. Self-amendment would create a circular amendment, so Pkl rejects it.","triggerScenarios":"Writing `...myModule` style amendment or `amends`/extend syntax where the supermodule argument equals the module object being evaluated (module == supermodule).","commonSituations":"Recursive module templates: module `a` amends module `a`, or an amender at the top of a module that amends the module itself instead of a base module.","solutions":["Change the amendment target to a different module (e.g. a base/config module).","If extending a module's own output, split into two modules: a base module and an amending module.","Check `amends`/`extends` headers and top-level amend expressions for accidental self-reference."],"exampleFix":"// before (a.pkl amends itself)\namends \"a.pkl\"\n// after\namends \"pkl:base\" // or a distinct base module","handlingStrategy":"validation","validationCode":"// module header must not amend itself\n// a.pkl must not contain: amends \"a.pkl\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Review `amends`/`extends` headers for self-references.","Split self-extending modules into base + amender modules.","Check top-level amend expressions for accidental receiver == supermodule."],"tags":["pkl","module","self-reference"],"backgroundTag":"invalid-state-transition","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}