{"record":{"id":"deb57bb5d9890aec","repo":"apple/pkl","slug":"propertymustbeconst","errorCode":"propertyMustBeConst","errorMessage":"Cannot reference property `{0}` from here because it is not `const`.","messagePattern":"Cannot reference property `(.+?)` from here because it is not `const`\\.","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/member/AbstractReadLocalPropertyNode.java","lineNumber":52,"sourceCode":"\n  public AbstractReadLocalPropertyNode(\n      SourceSection sourceSection, Identifier name, boolean needsConst) {\n    super(sourceSection);\n    this.name = name;\n    this.needsConst = needsConst;\n  }\n\n  protected ObjectMember getProperty(VmObjectLike owner) {\n    if (property == null) {\n      CompilerDirectives.transferToInterpreterAndInvalidate();\n      property = owner.getMember(name);\n      if (property == null) {\n        // should never happen\n        CompilerDirectives.transferToInterpreter();\n        throw exceptionBuilder().bug(\"Couldn't find local variable `\" + name + \"`.\").build();\n      }\n      if (needsConst && !property.isConst()) {\n        throw exceptionBuilder().evalError(\"propertyMustBeConst\", name.toString()).build();\n      }\n    }\n    return property;\n  }\n\n  protected DirectCallNode getCallNode(ObjectMember property) {\n    if (callNode == null) {\n      CompilerDirectives.transferToInterpreterAndInvalidate();\n      callNode = DirectCallNode.create(property.getCallTarget());\n      insert(callNode);\n    }\n    assert callNode != null;\n    return callNode;\n  }\n}\n","sourceCodeStart":34,"sourceCodeEnd":68,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/member/AbstractReadLocalPropertyNode.java#L34-L68","documentation":"Pkl throws `propertyMustBeConst` when code references a local/module property from a context that requires a `const` value (e.g. inside a `const` property definition or an annotation-like position) but the property is not declared `const`. AbstractReadLocalPropertyNode.getProperty checks needsConst && !property.isConst().","triggerScenarios":"Referencing a non-const local property from within another property declared `const`, or wherever const-ness is required (e.g. in default values consumed by tooling that must be constant-foldable).","commonSituations":"Building shared config values referenced from `const` properties; introducing a helper property that a const definition starts depending on after refactoring.","solutions":["Declare the referenced property as `const` (e.g. `const x = ...`).","Inline the value into the const property if const-ness of the helper is undesirable.","Remove the `const` modifier from the referencing property if const-ness is not required."],"exampleFix":"// before\nconst a = b // b is not const\nb = 1\n// after\nconst a = b\nconst b = 1","handlingStrategy":"validation","validationCode":"// Pkl\nconst b = 1\nconst a = b // b must be const for a to be const","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Mark shared helper values `const` from the start.","Review dependencies of `const` properties after refactors.","Keep const definitions close to their (const) inputs."],"tags":["pkl","const","property","evaluation-order"],"backgroundTag":"invalid-argument-value","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"}