apple/pkl · error · VmException

invalidConstObjectMemberModifier

invalidConstObjectMemberModifier

Error message

invalidConstObjectMemberModifier

What it means

The `const` modifier is not permitted on this kind of object member. When visiting an object property in this context (first overload, line 2609), the builder validates the modifier list and, if it contains `ModifierValue.CONST` where disallowed, throws after locating the offending `const` token for the source section.

Source

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

      }
      previous = member.span();
    }
  }

  private ObjectMember doVisitObjectProperty(ObjectProperty prop) {
    var modifierNodes = prop.getModifiers();
    var propertyName = prop.getIdentifier();
    var modifiers =
        doVisitModifiers(
            modifierNodes, VmModifier.VALID_OBJECT_MEMBER_MODIFIERS, "invalidObjectMemberModifier");
    if (VmModifier.isConst(modifiers) && !VmModifier.isLocal(modifiers)) {
      @SuppressWarnings("OptionalGetWithoutIsPresent")
      var constModifierCtx =
          modifierNodes.stream()
              .filter((it) -> it.getValue() == ModifierValue.CONST)
              .findFirst()
              .get();
      throw exceptionBuilder()
          .evalError("invalidConstObjectMemberModifier")
          .withSourceSection(createSourceSection(constModifierCtx))
          .build();
    }
    return doVisitObjectProperty(
        createSourceSection(prop),
        createSourceSection(propertyName),
        modifiers,
        propertyName.getValue(),
        prop.getTypeAnnotation(),
        prop.getExpr(),
        prop.getBodyList());
  }

  private ObjectMember doVisitObjectProperty(
      Node node,
      List<? extends Modifier> modifierNodes,
      Identifier propertyName,

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Remove the `const` modifier from the object member.
  2. Define the value as a module-level `const` and reference it inside the object.
  3. If immutability within the object is the goal, rely on Pkl's default single-assignment property semantics — no modifier is needed.

Example fix

// before
obj {
  const x = 42
}
// after
x = 42
// or at module level
const x = 42
obj { y = x }
Defensive patterns

Strategy: validation

Validate before calling

// strip 'const' from inside object bodies before evaluation
if (/\{[^}]*\bconst\b[^}]*=/s.test(src))
  throw new Error('const is not a valid object member modifier; use it at module level');

Prevention

When it happens

Trigger: Writing `const` on an object property/member form where the grammar context forbids it — e.g. a member of an amending object or an object member that is not a local/module-level definition, such as `obj { const x = 1 }` in a context that only allows plain members.

Common situations: Copy-pasting a module-level `const x = ...` into an object body; adding `const` inside an override/amendment to try to freeze a value; confusing Pkl's `const` (definition-site immutability for definitions) with object-member semantics.

Related errors


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