{"record":{"id":"297a454fe19318cc","repo":"apple/pkl","slug":"cannotfindcollectionelement","errorCode":"cannotFindCollectionElement","errorMessage":"cannotFindCollectionElement","messagePattern":"cannotFindCollectionElement","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/stdlib/base/ListNodes.java","lineNumber":296,"sourceCode":"\n    @Specialization\n    protected Object eval(VmList self, VmFunction function) {\n      var iterator = self.reverseIterator();\n      while (iterator.hasNext()) {\n        var elem = iterator.next();\n        if (applyLambdaNode.executeBoolean(function, elem)) return elem;\n      }\n      return VmNull.withoutDefault();\n    }\n  }\n\n  public abstract static class indexOf extends ExternalMethod1Node {\n    @Specialization\n    protected long eval(VmList self, Object elem) {\n      var result = self.indexOf(elem);\n      if (result == -1) {\n        CompilerDirectives.transferToInterpreter();\n        throw exceptionBuilder()\n            .evalError(\"cannotFindCollectionElement\")\n            .withProgramValue(\"Element\", elem)\n            .withProgramValue(\"Collection\", self)\n            .build();\n      }\n      return result;\n    }\n  }\n\n  public abstract static class indexOfOrNull extends ExternalMethod1Node {\n    @Specialization\n    protected Object eval(VmList self, Object elem) {\n      return self.indexOfOrNull(elem);\n    }\n  }\n\n  public abstract static class lastIndexOf extends ExternalMethod1Node {\n    @Specialization","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/stdlib/base/ListNodes.java#L278-L314","documentation":"List.indexOf(elem) returns the 0-based index of the element, but Pkl has no null/-1-safe path in this API: when the element is not present, the runtime throws cannotFindCollectionElement with the 'Element' and 'Collection' attached.","triggerScenarios":"Calling List.indexOf(value) where value is not present in the list (indexOf internally returns -1 and the node converts it into this error).","commonSituations":"Searching for a key, enum value, or name removed or renamed in the data; comparing by equality where types differ subtly; expecting an element that an earlier stage filtered out.","solutions":["Check membership first: list.contains(elem) before indexOf","Use list.filter((e) -> e == elem).isEmpty handling instead of indexOf","Fix the searched value (spelling, type, case) so equality holds","Ensure the element was not removed by upstream transformations"],"exampleFix":"// before\nlocal idx = fruits.indexOf(\"banana\")\n// after\nlocal idx = fruits.contains(\"banana\") ? fruits.indexOf(\"banana\") : -1","handlingStrategy":"validation","validationCode":"if (!list.contains(elem)) throw new Error(\"element not in list: \"+elem)","typeGuard":"function canLocate(list, elem) { return list.includes(elem); }","tryCatchPattern":null,"preventionTips":["Guard indexOf with contains()","Verify equality semantics (type/case) of the searched value","Handle absence with -1/default instead of relying on the throw"],"tags":["pkl","list","indexof","not-found"],"backgroundTag":"resource-not-found","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}