{"record":{"id":"942f0dfddc9631a1","repo":"apple/pkl","slug":"i-o-error-writing-file-outputfile-cause-e-m","errorCode":null,"errorMessage":"I/O error writing file `$outputFile`.\nCause: ${e.message}","messagePattern":"I/O error writing file `\\$outputFile`\\.\nCause: (.+?)","errorType":"exception","errorClass":"CliException","httpStatus":null,"severity":"error","filePath":"pkl-codegen-java/src/main/kotlin/org/pkl/codegen/java/CliJavaCodeGenerator.kt","lineNumber":43,"sourceCode":"\n/** API for the Java code generator CLI. */\nclass CliJavaCodeGenerator(private val options: CliJavaCodeGeneratorOptions) :\n  CliCommand(options.base) {\n\n  override fun doRun() {\n    val builder = evaluatorBuilder()\n    try {\n      builder.build().use { evaluator ->\n        for (moduleUri in options.base.normalizedSourceModules) {\n          val schema = evaluator.evaluateSchema(ModuleSource.uri(moduleUri))\n          val codeGenerator = JavaCodeGenerator(schema, options.toJavaCodeGeneratorOptions())\n          try {\n            for ((fileName, fileContents) in codeGenerator.output) {\n              val outputFile = options.outputDir.resolve(fileName)\n              try {\n                outputFile.createParentDirectories().writeString(fileContents)\n              } catch (e: IOException) {\n                throw CliException(\"I/O error writing file `$outputFile`.\\nCause: ${e.message}\")\n              }\n            }\n          } catch (e: JavaCodeGeneratorException) {\n            throw CliException(e.message!!)\n          }\n        }\n      }\n    } finally {\n      Closeables.closeQuietly(builder.moduleKeyFactories)\n      Closeables.closeQuietly(builder.resourceReaders)\n    }\n  }\n}\n","sourceCodeStart":25,"sourceCodeEnd":57,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-codegen-java/src/main/kotlin/org/pkl/codegen/java/CliJavaCodeGenerator.kt#L25-L57","documentation":"Thrown by CliJavaCodeGenerator.doRun when an IOException occurs while writing a generated Java source file to disk. The CLI wraps the underlying IOException into a CliException so the command fails with a clear message identifying the output file. It indicates a filesystem-level failure (permissions, disk space, path issues), not a code-generation problem.","triggerScenarios":"Calling the Java codegen CLI with --output-dir pointing to a non-writable or non-existent path, a path that is a directory with the same name as a target file, or a disk that is full; outputFile.createParentDirectories().writeString(fileContents) throws IOException.","commonSituations":"Running in CI with read-only workspace, output dir owned by another user, Windows file locked by an IDE/editor, disk quota exceeded, or outputDir resolving onto a read-only mount.","solutions":["Check that the --output-dir exists and is writable by the current user (ls -ld, touch a test file).","Free up disk space / check quota if the disk is full.","Ensure no file at the target path is locked or is a directory; close editors/AV that hold the file open.","Run the command with appropriate permissions or fix ownership of the output directory.","Use a different output directory to isolate whether the path is the problem."],"exampleFix":"// before (CI failing on read-only dir)\npkl gen java --output-dir /srv/app/generated module.pkl\n// after\npkl gen java --output-dir \"$PWD/generated\" module.pkl","handlingStrategy":"try-catch","validationCode":"val out = File(outputDir); require(out.canWrite()) { \"output dir not writable: $out\" }","typeGuard":"fun isWritableDir(path: Path): Boolean = Files.isDirectory(path) && Files.isWritable(path)","tryCatchPattern":"try { runGenerator() } catch (e: CliException) { if (e.message?.startsWith(\"I/O error writing file\") == true) { /* check disk/permissions, retry or fail fast */ } else throw e }","preventionTips":["Verify the output directory is writable in CI before running codegen","Monitor disk space/quota on build agents","Avoid output paths on read-only or network mounts","Close editors/AV that may lock generated files on Windows"],"tags":["io","file-write","filesystem"],"backgroundTag":"file-write-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"}