{"record":{"id":"b97588fd6521d224","repo":"apple/pkl","slug":"moduleisnotconstannotation","errorCode":"moduleIsNotConstAnnotation","errorMessage":"moduleIsNotConstAnnotation","messagePattern":"moduleIsNotConstAnnotation","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java","lineNumber":653,"sourceCode":"  public ExpressionNode visitModuleExpr(ModuleExpr expr) {\n    var currentScope = symbolTable.getCurrentScope();\n    // cannot use unqualified `module` in a const context\n    if (currentScope.getConstLevel().isConst() && !(expr.parent() instanceof QualifiedAccessExpr)) {\n      var scope = currentScope;\n      while (scope != null\n          && !(scope instanceof AnnotationScope)\n          && !(scope instanceof ClassScope)) {\n        scope = scope.getParent();\n      }\n      if (scope == null) {\n        throw exceptionBuilder()\n            .evalError(\"moduleIsNotConst\", currentScope.getName().toString())\n            .withSourceSection(createSourceSection(expr))\n            .build();\n      }\n      var messageKey =\n          scope instanceof AnnotationScope ? \"moduleIsNotConstAnnotation\" : \"moduleIsNotConstClass\";\n      throw exceptionBuilder()\n          .evalError(messageKey)\n          .withSourceSection(createSourceSection(expr))\n          .build();\n    }\n    return symbolTable.isInTypeAliasScope\n        ? new GetTypeAliasModuleNode(createSourceSection(expr))\n        : new GetModuleNode(createSourceSection(expr));\n  }\n\n  @Override\n  public ConstantValueNode visitNullLiteralExpr(NullLiteralExpr expr) {\n    return new ConstantValueNode(createSourceSection(expr), VmNull.withoutDefault());\n  }\n\n  @Override\n  public ExpressionNode visitBoolLiteralExpr(BoolLiteralExpr expr) {\n    if (expr.isB()) {\n      return new TrueLiteralNode(createSourceSection(expr));","sourceCodeStart":635,"sourceCodeEnd":671,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java#L635-L671","documentation":"In Pkl, `module` can only be referenced as a value inside a const annotation or within class bodies where the module object itself is a compile-time constant. When a `module` expression is evaluated in a scope where it would not be constant (e.g. a plain property or method body), the AST builder rejects it. The message key distinguishes annotation scopes (moduleIsNotConstAnnotation) from class scopes (moduleIsNotConstClass).","triggerScenarios":"Writing `module` inside an annotation when the enclosing scope's const level permits non-const evaluation, i.e. `visitModuleExpr` detects the current scope is an AnnotationScope but the const check already failed.","commonSituations":"Using `module.name` or `module.dir` inside a custom annotation definition, attempting to parameterize annotations with module-relative values.","solutions":["Remove the `module` expression from the annotation; annotations must be const-evaluable literals or simple type references.","Move the module-relative value out of the annotation into a normal property or the annotated member itself.","Use a fully qualified type or literal string in the annotation instead of module introspection."],"exampleFix":"// before\n@Deprecated { since = module.name }\nprop x\n// after\n@Deprecated { since = \"my/pkg\" }\nprop x","handlingStrategy":"validation","validationCode":"// Before relying on module introspection in an annotation, ensure the value is a literal:\nconst ann = new Annotation {\n  since: \"my/pkg\" // literal, not `module.name`\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never reference `module` inside annotations; annotations must be const-evaluable.","Keep module-relative strings as plain literals in annotation parameters."],"tags":["pkl","annotation","const-evaluation"],"backgroundTag":"unsupported-operation","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"}