{"record":{"id":"a683227421c1360a","repo":"apple/pkl","slug":"expectednonemptycollection","errorCode":"expectedNonEmptyCollection","errorMessage":"expectedNonEmptyCollection","messagePattern":"expectedNonEmptyCollection","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/runtime/VmCollection.java","lineNumber":52,"sourceCode":"  public abstract boolean isEmpty();\n\n  @Override\n  public boolean isSequence() {\n    return true;\n  }\n\n  public abstract VmCollection add(Object element);\n\n  public abstract VmCollection concatenate(VmCollection other);\n\n  public abstract Iterator<Object> reverseIterator();\n\n  public abstract Builder<? extends VmCollection> builder();\n\n  public final void checkNonEmpty() {\n    if (isEmpty()) {\n      CompilerDirectives.transferToInterpreter();\n      throw new VmExceptionBuilder()\n          .evalError(\"expectedNonEmptyCollection\")\n          .withProgramValue(\"Collection\", this)\n          .build();\n    }\n  }\n\n  @SuppressWarnings(\"BooleanMethodIsAlwaysInverted\")\n  public abstract boolean isLengthOne();\n\n  public final void checkLengthOne() {\n    if (!isLengthOne()) {\n      CompilerDirectives.transferToInterpreter();\n      throw new VmExceptionBuilder()\n          .evalError(\"expectedSingleElementCollection\")\n          .withProgramValue(\"Collection\", this)\n          .build();\n    }\n  }","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/runtime/VmCollection.java#L34-L70","documentation":"Pkl's non-empty collection types (`NonEmptyList`, `NonEmptySet`, `NonEmptyMap`, `NonEmptyString`) enforce their invariant lazily: `VmCollection.checkNonEmpty` throws `expectedNonEmptyCollection` as soon as an empty collection is constructed or accessed. The error carries the offending collection as program value `Collection`, so an empty value reached a non-empty-typed slot.","triggerScenarios":"Amending/constructing a `Listing!`, `Mapping!`, `Set!`, or `String!` (non-empty variants) with zero elements; passing an empty List/Set/Map/Expr where a non-empty type is required; a `Listing!` whose contents are filtered down to nothing.","commonSituations":"Schema declares `serverHosts: NonEmptyList<String>` but the config renders an empty list; an environment with no matching entries produces an empty listing; default values that are empty lists assigned to non-empty typed properties.","solutions":["Supply at least one element to the collection at the reported property.","Relax the schema type from the non-empty variant (`Listing!`/`Mapping!`) to `Listing`/`Mapping` if emptiness is legitimate.","Add a conditional/default so an empty result falls back to a populated value instead of the non-empty type.","Inspect the `Collection` value attached to the error to find exactly which property was empty."],"exampleFix":"// before\nhosts: NonEmptyList<String> = List.empty // error\n// after\nhosts: NonEmptyList<String> = List(\"localhost\")","handlingStrategy":"validation","validationCode":"// validate before assigning to a non-empty-typed property\nif (hosts.isEmpty()) {\n  throw new IllegalArgumentException(\"hosts must contain at least one entry\");\n}","typeGuard":"boolean isNonEmpty(List<?> list) { return list != null && !list.isEmpty(); }","tryCatchPattern":"catch (EvalError e) {\n  if (e.getMessage().contains(\"expectedNonEmptyCollection\")) {\n    // inspect the 'Collection' program value; provide a default non-empty value\n  }\n}","preventionTips":["Give non-empty-typed properties a populated default value in the schema.","Guard filtered/derived listings against becoming empty before binding them to non-empty types.","Only use NonEmpty* types when emptiness is genuinely invalid."],"tags":["pkl","collections","validation","schema"],"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-14T11:17:12.474Z"}