{"record":{"id":"8af3629e51f3b708","repo":"apple/pkl","slug":"modulecannotextendself","errorCode":"moduleCannotExtendSelf","errorMessage":"moduleCannotExtendSelf","messagePattern":"moduleCannotExtendSelf","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/member/ClassNode.java","lineNumber":176,"sourceCode":"      cachedClass.onOwnClassInitialized();\n      localContext.endClassInit();\n      return cachedClass;\n    } catch (Throwable e) {\n      localContext.clearClassInitState();\n      throw e;\n    }\n  }\n\n  private void checkSupertype(TypeNode supertypeNode, @Nullable VmClass superclass) {\n    if (superclass == null) {\n      throw exceptionBuilder()\n          .evalError(\"invalidSupertype\", supertypeNode.getSourceSection().getCharacters())\n          .withSourceSection(supertypeNode.getSourceSection())\n          .build();\n    }\n    if (moduleInfo != null) {\n      if (cachedClass == superclass) {\n        throw exceptionBuilder()\n            .evalError(\"moduleCannotExtendSelf\", moduleInfo.getModuleName())\n            .withSourceSection(supertypeNode.getSourceSection())\n            .build();\n      }\n      if (superclass.isClosed()) {\n        throw exceptionBuilder()\n            .evalError(\"cannotExtendFinalModule\", superclass.getModuleName())\n            .withSourceSection(supertypeNode.getSourceSection())\n            .build();\n      }\n    } else {\n      if (cachedClass == superclass) {\n        throw exceptionBuilder()\n            .evalError(\"classCannotExtendSelf\", superclass.getDisplayName())\n            .withSourceSection(supertypeNode.getSourceSection())\n            .build();\n      }\n      if (superclass.isClosed()) {","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/member/ClassNode.java#L158-L194","documentation":"Pkl throws moduleCannotExtendSelf when a module declares itself as its own superclass (e.g. `extends` naming the enclosing module). ClassNode.checkSupertype compares the resolved superclass with the cached class of the module being defined and rejects direct self-extension. A module extending itself would create a cyclic inheritance chain, so it is stopped at class-definition time.","triggerScenarios":"Writing `extends <thisModuleName>` inside the module itself; a module importing itself (directly or via a re-export) and extending that import; circular module extends where the chain loops back to the same module.","commonSituations":"Copy-pasting an `extends` clause into the base module itself; refactoring a two-module hierarchy into one file and forgetting to drop the extends; accidentally importing a module into itself through a barrel/index module.","solutions":["Remove the `extends` clause that names the module itself.","If you intended inheritance from a base config, extend a different module that does not (transitively) extend this one.","Break any import cycle so the module does not reach itself through re-exports.","Rename the target if you actually meant a different module with a similar name."],"exampleFix":"// before (inside base.pkl)\nmodule base\nextends base  // module extends itself\n\n// after\nmodule base","handlingStrategy":"validation","validationCode":"// Check the extends chain of a module before shipping:\n// pkl eval base.pkl  -> moduleCannotExtendSelf surfaces at load\n// Audit imports: a module must never import itself (directly or via a barrel module).","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never write an extends clause naming the enclosing module.","Keep the module hierarchy acyclic; draw the base->derived graph when it grows.","Avoid barrel/index modules that re-import themselves transitively.","Run pkl eval in CI to catch cycles at definition time."],"tags":["pkl","inheritance","circular-dependency","module"],"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-14T11:17:12.474Z"}