{"record":{"id":"798055cc64ff0950","repo":"apple/pkl","slug":"array","errorCode":null,"errorMessage":"array","messagePattern":"array","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ImportGraph.java","lineNumber":72,"sourceCode":"\n  /** Parses the provided JSON into an import graph. */\n  public static ImportGraph parseFromJson(String input) throws JsonParseException {\n    var parsed = Json.parseObject(input);\n    var imports = parseImports(parsed.getObject(\"imports\"));\n    var resolvedImports = parseResolvedImports(parsed.getObject(\"resolvedImports\"));\n    return new ImportGraph(imports, resolvedImports);\n  }\n\n  private static Map<URI, Set<Import>> parseImports(Json.JsObject jsObject)\n      throws JsonParseException {\n    var ret = new TreeMap<URI, Set<Import>>();\n    for (var entry : jsObject.entrySet()) {\n      try {\n        var key = new URI(entry.getKey());\n        var value = entry.getValue();\n        var set = new TreeSet<Import>();\n        if (!(value instanceof JsArray array)) {\n          throw new FormatException(\"array\", value == null ? Void.class : value.getClass());\n        }\n        for (var elem : array) {\n          if (!(elem instanceof JsObject importObj)) {\n            throw new FormatException(\"object\", elem == null ? Void.class : elem.getClass());\n          }\n          set.add(parseImport(importObj));\n        }\n        ret.put(key, set);\n      } catch (URISyntaxException e) {\n        throw new MappingException(entry.getKey(), e);\n      }\n    }\n    return ret;\n  }\n\n  private static ImportGraph.Import parseImport(Json.JsObject jsObject) throws JsonParseException {\n    var uri = jsObject.getURI(\"uri\");\n    return new Import(uri);","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ImportGraph.java#L54-L90","documentation":"ImportGraph.parseImports validates the JSON structure of a module-import cache/graph file. When the value mapped to a module URI key is not a JSON array of import objects, it throws FormatException(\"array\", actualClass), i.e. 'expected array but found <type>'. This guards against corrupt or hand-edited import-graph cache files being fed to the evaluator.","triggerScenarios":"Loading an import graph cache (used by pkl CLI caching / evaluation caching via the imports entry point) where an entry's value is null, a JSON object/string/number instead of an array of {import objects}. Caused by a corrupted, truncated, or manually edited cache file, or an incompatible cache format from a different Pkl version.","commonSituations":"Stale or corrupted ~/.cache/pkl import-graph entries after an aborted run or version upgrade; sharing cache dirs between Pkl versions; hand-editing cache JSON.","solutions":["Delete the corrupted cache entry/directory (e.g. ~/.cache/pkl or the project .pkl cache) and re-run to regenerate.","Ensure all Pkl processes use the same Pkl version so cache formats match.","Don't hand-edit cache files; regenerate them via the tooling.","If constructing the graph programmatically, ensure each URI maps to a JSON array of import objects."],"exampleFix":"// before (corrupt cache entry)\n{\"file:///proj/Main.pkl\": {\"uri\": \"...\"}}\n// after\n{\"file:///proj/Main.pkl\": [{\"uri\": \"file:///proj/Dep.pkl\", \"isImportedFromModule\": false}]}","handlingStrategy":"validation","validationCode":"// validate a parsed import-graph JSON before handing it to the evaluator\nfor (var entry : graph.entrySet()) {\n  if (!(entry.getValue() instanceof List<?> list) || list.stream().anyMatch(o -> !(o instanceof Map))) {\n    throw new IllegalStateException(\"Corrupt import graph entry for \" + entry.getKey());\n  }\n}","typeGuard":"static boolean isValidImportGraphEntry(Object value) {\n  return value instanceof List<?> list\n      && !list.isEmpty()\n      && list.stream().allMatch(elem -> elem instanceof Map<?, ?>);\n}","tryCatchPattern":"try {\n  evaluator.evaluate(source);\n} catch (PklException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"array\")) {\n    Files.deleteIfExists(cacheFile); // drop corrupt cache and retry\n    return evaluateFresh(source);\n  }\n  throw e;\n}","preventionTips":["Delete stale/corrupt pkl caches instead of editing them","Use one Pkl version per shared cache directory","Let the tool regenerate cache files after failed/interrupted runs","Validate cache JSON structure before programmatic use"],"tags":["cache","format-validation","json"],"backgroundTag":"schema-validation-failed","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"}