apple/pkl · error · VmException

moduleIsNotConst

moduleIsNotConst

Error message

moduleIsNotConst

What it means

AstBuilder.visitModuleExpr throws 'moduleIsNotConst' when unqualified `module` appears in a const context: the module self-reference is not available as a constant (annotations can't see the module instance). The input at fault is a bare `module` expression in an annotation or other const-level scope.

Solutions

  1. Qualify the reference (e.g. `module.foo`) if permitted, or reference a constant property instead.
  2. Remove the `module` reference from the annotation/const context.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java:646 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/a17abc87f9e871c9. Report an issue: GitHub.

Appendix: source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java:646

            .build();
      }
    }
    return new OuterNode(createSourceSection(expr));
  }

  @Override
  public ExpressionNode visitModuleExpr(ModuleExpr expr) {
    var currentScope = symbolTable.getCurrentScope();
    // cannot use unqualified `module` in a const context
    if (currentScope.getConstLevel().isConst() && !(expr.parent() instanceof QualifiedAccessExpr)) {
      var scope = currentScope;
      while (scope != null
          && !(scope instanceof AnnotationScope)
          && !(scope instanceof ClassScope)) {
        scope = scope.getParent();
      }
      if (scope == null) {
        throw exceptionBuilder()
            .evalError("moduleIsNotConst", currentScope.getName().toString())
            .withSourceSection(createSourceSection(expr))
            .build();
      }
      var messageKey =
          scope instanceof AnnotationScope ? "moduleIsNotConstAnnotation" : "moduleIsNotConstClass";
      throw exceptionBuilder()
          .evalError(messageKey)
          .withSourceSection(createSourceSection(expr))
          .build();
    }
    return symbolTable.isInTypeAliasScope
        ? new GetTypeAliasModuleNode(createSourceSection(expr))
        : new GetModuleNode(createSourceSection(expr));
  }

  @Override
  public ConstantValueNode visitNullLiteralExpr(NullLiteralExpr expr) {

View on GitHub (pinned to f3efcbfc9b)