{"record":{"id":"c641b6b241818000","repo":"apple/pkl","slug":"expectednonemptylisting","errorCode":"expectedNonEmptyListing","errorMessage":"expectedNonEmptyListing","messagePattern":"expectedNonEmptyListing","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/stdlib/base/ListingNodes.java","lineNumber":332,"sourceCode":"  }\n\n  public abstract static class toSet extends ExternalMethod0Node {\n    @Specialization\n    protected VmSet eval(VmListing self) {\n      var builder = VmSet.EMPTY.builder();\n      self.forceAndIterateMemberValues(\n          (key, member, value) -> {\n            builder.add(value);\n            return true;\n          });\n      return builder.build();\n    }\n  }\n\n  private static void checkNonEmpty(VmListing self, PklNode node) {\n    if (self.isEmpty()) {\n      CompilerDirectives.transferToInterpreter();\n      throw new VmExceptionBuilder()\n          .evalError(\"expectedNonEmptyListing\")\n          .withLocation(node)\n          .build();\n    }\n  }\n\n  private static void checkSingleton(VmListing self, PklNode node) {\n    if (self.getLength() != 1) {\n      CompilerDirectives.transferToInterpreter();\n      throw new VmExceptionBuilder()\n          .evalError(\"expectedSingleElementListing\")\n          .withLocation(node)\n          .build();\n    }\n  }\n}\n","sourceCodeStart":314,"sourceCodeEnd":349,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/stdlib/base/ListingNodes.java#L314-L349","documentation":"Listing members `first`, `last`, and similar single-value accessors are only defined for non-empty listings. checkNonEmpty throws expectedNonEmptyListing when one of these members is accessed on an empty Listing. The error is reported at the accessing node's location.","triggerScenarios":"Accessing `someListing.first`, `someListing.last` (or similar non-empty-required members) on a Listing with zero elements.","commonSituations":"A filtered/parsed input produced an empty list (e.g. no matching files, empty config list) and code assumes at least one item; reading required lists from external data that can legitimately be empty.","solutions":["Check `listing.isEmpty` before accessing first/last and handle the empty case explicitly.","Ensure upstream data actually populates the listing (fix the source/filter producing zero elements).","Provide a default when empty: `if (items.isEmpty) default else items.first`.","Use `items.toList()` and standard guards if doing complex access in code."],"exampleFix":"// before\nname = files.first\n\n// after\nname = if (files.isEmpty) \"default.txt\" else files.first","handlingStrategy":"type-guard","validationCode":"// guard before single-value access\nif (!items.isEmpty) { /* safe to use items.first */ }","typeGuard":"function firstOrNull(l: Listing) = if (l.isEmpty) null else l.first","tryCatchPattern":"try { x = items.first } catch (e) { if (e.message.contains('expectedNonEmptyListing')) x = fallback else throw e }","preventionTips":["Always check isEmpty before first/last access","Fix filters/sources that can yield zero elements","Express defaults for possibly-empty lists in config"],"tags":["pkl","listing","empty-collection","stdlib"],"backgroundTag":"empty-required-field","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"}