{"record":{"id":"1e8d5c470382a30a","repo":"apple/pkl","slug":"cannotfindkey-key-in-map","errorCode":null,"errorMessage":"cannotFindKey ${key} in Map","messagePattern":"cannotFindKey (.+?) in Map","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/binary/SubscriptNode.java","lineNumber":77,"sourceCode":"  @Specialization\n  protected Object eval(VmList receiver, long index) {\n    if (index < 0 || index >= receiver.getLength()) {\n      CompilerDirectives.transferToInterpreter();\n      throw exceptionBuilder()\n          .evalError(\"elementIndexOutOfRange\", index, 0, receiver.getLength() - 1)\n          .withProgramValue(\"Collection\", receiver)\n          .build();\n    }\n    return receiver.get(index);\n  }\n\n  @Specialization\n  protected Object eval(VmMap receiver, Object key) {\n    var result = receiver.getOrNull(key);\n    if (result != null) return result;\n\n    CompilerDirectives.transferToInterpreter();\n    throw exceptionBuilder().cannotFindKey(receiver, key).build();\n  }\n\n  @Specialization\n  protected Object eval(\n      VmListing listing, long index, @Exclusive @Cached(\"create()\") IndirectCallNode callNode) {\n\n    var result = VmUtils.readMemberOrNull(listing, index, callNode);\n    if (result != null) return result;\n\n    CompilerDirectives.transferToInterpreter();\n    throw exceptionBuilder()\n        .evalError(\"elementIndexOutOfRange\", index, 0, listing.getLength() - 1)\n        .build();\n  }\n\n  @Specialization\n  protected Object eval(\n      VmMapping mapping, Object key, @Exclusive @Cached(\"create()\") IndirectCallNode callNode) {","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/binary/SubscriptNode.java#L59-L95","documentation":"Subscripting a Map with a key that is absent throws 'cannotFindKey ${key} in Map'. Pkl maps require explicit key existence; there is no implicit null for missing keys. The message identifies the missing key and the Map type.","triggerScenarios":"`map[key]` where `map.getOrNull(key)` returns null — key never inserted, key of wrong type/value (e.g. Int vs String), or case mismatch in string keys.","commonSituations":"Typo'd or case-mismatched config keys, expecting defaults from environment data that isn't present, or using a value as key where a symbol/type is expected.","solutions":["Use `map.getOrNull(key)` (or `getOrDefault`) and handle the missing case","Verify the key exists and matches exactly (type, case, spelling)","Add the key to the Map definition or fix the data source"],"exampleFix":"// before (Pkl)\nval region = config[\"Region\"]\n// after\nval region = config[\"region\"] ?? \"us-east-1\"","handlingStrategy":"fallback","validationCode":"function safeMapGet(map, key, dflt) { return Object.prototype.hasOwnProperty.call(map, key) ? map[key] : dflt; }","typeGuard":"const hasKey = (m, k) => m != null && Object.prototype.hasOwnProperty.call(m, k);","tryCatchPattern":null,"preventionTips":["Use getOrNull()/getOrDefault() for optional keys","Verify key spelling and case against the map definition","Ensure key type matches (String vs Int/Symbol)"],"tags":["map","missing-key","pkl"],"backgroundTag":"missing-config-key","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"}