{"record":{"id":"7b6b272f5d78bd6f","repo":"apple/pkl","slug":"ioerrorreadingtestoutputfile","errorCode":"ioErrorReadingTestOutputFile","errorMessage":"ioErrorReadingTestOutputFile","messagePattern":"ioErrorReadingTestOutputFile","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/runtime/TestRunner.java","lineNumber":373,"sourceCode":"    var builder = new StringBuilder();\n    new PcfRenderer(builder, \"  \", converter, false, true).renderDocument(outputFileContent);\n    try {\n      Files.writeString(outputFile, builder);\n    } catch (IOException e) {\n      throw new VmExceptionBuilder()\n          .evalError(\"ioErrorWritingTestOutputFile\", outputFile)\n          .withCause(e)\n          .build();\n    }\n  }\n\n  private VmDynamic loadExampleOutputs(Path outputFile) {\n    // load file manually to prevent it from being cached (won't need it again)\n    String fileContent;\n    try {\n      fileContent = Files.readString(outputFile, StandardCharsets.UTF_8);\n    } catch (IOException e) {\n      throw new VmExceptionBuilder()\n          .evalError(\"ioErrorReadingTestOutputFile\", outputFile)\n          .withCause(e)\n          .build();\n    }\n    var module =\n        VmLanguage.get(null).loadModule(ModuleKeys.synthetic(outputFile.toUri(), fileContent));\n    var examples = (VmDynamic) VmUtils.readMemberOrNull(module, Identifier.EXAMPLES);\n    if (examples == null) {\n      throw new VmExceptionBuilder().evalError(\"invalidOutputFileStructure\", outputFile).build();\n    }\n    return examples;\n  }\n\n  private static String renderAsPcf(Object pklValue) {\n    var builder = new StringBuilder();\n    new PcfRenderer(builder, \"  \", converter, false, false).renderValue(pklValue);\n    if (pklValue instanceof VmObject) {\n      builder.insert(0, \"new \");","sourceCodeStart":355,"sourceCodeEnd":391,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/runtime/TestRunner.java#L355-L391","documentation":"`TestRunner.loadExampleOutputs` reads an expected or actual output Pcf file from disk to compare against fresh results. An IOException from `Files.readString` is wrapped as `ioErrorReadingTestOutputFile` naming the file. Since the file's existence is normally checked just before, this usually means a race, permissions problem, or invalid-encoding/IO issue.","triggerScenarios":"Reading `X-expected.pcf` or `X-actual.pcf` fails: file deleted between the existence check and the read, unreadable permissions, locked file, or charset/IO error.","commonSituations":"Parallel processes cleaning generated files mid-test; expected-output file unreadable by the current user (wrong ownership after sudo-generated files); filesystem hiccup in containers or network mounts.","solutions":["Verify the output file still exists and is readable (ls -l, check ownership/permissions).","Stop concurrent processes that delete or regenerate `*-pcf` files during the run.","Regenerate the expected file with `pkl test --overwrite` (after ensuring write access).","Avoid running tests against files on flaky network mounts; use a local copy."],"exampleFix":"// before: expected file owned by root, test run as non-root\n// after\nsudo chown \"$(whoami)\" MyTest-expected.pcf && chmod u+rw MyTest-expected.pcf\npkl test MyTest.pkl","handlingStrategy":"validation","validationCode":"// verify output files are readable before testing\nPath expected = moduleFile.resolveSibling(moduleFile.getFileName() + \"-expected.pcf\");\nif (Files.exists(expected) && !Files.isReadable(expected)) {\n    throw new IllegalStateException(\"Unreadable: \" + expected);\n}","typeGuard":null,"tryCatchPattern":"catch (EvalError e) { /* withCause IOException: fix permissions/regenerate expected file */ }","preventionTips":["Avoid generating expected-output files as root; use consistent ownership.","Don't run concurrent jobs that clean or regenerate *-pcf files.","Prefer local filesystems over flaky network mounts for test runs."],"tags":["pkl","io","filesystem","testing"],"backgroundTag":"file-read-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"}