{"record":{"id":"8b62ce582c1e2131","repo":"apple/pkl","slug":"cannot-write-to-stdin","errorCode":null,"errorMessage":"Cannot write to stdin","messagePattern":"Cannot write to stdin","errorType":"exception","errorClass":"CliException","httpStatus":null,"severity":"error","filePath":"pkl-cli/src/main/kotlin/org/pkl/cli/CliFormatterCommand.kt","lineNumber":109,"sourceCode":"        if (!silent) {\n          writeErrLine(\"An error occurred during formatting.\")\n        }\n        throw CliTestException(\"\", status.status)\n      }\n    }\n  }\n\n  private fun handlePaths(status: Status) {\n    for (path in allPaths()) {\n      val pathStr = path.toString()\n      try {\n        val contents =\n          when {\n            pathStr == \"-\" -> IoUtils.readString(System.`in`)\n            else -> Files.readString(path)\n          }\n        if (pathStr == \"-\" && overwrite) {\n          throw CliException(\"Cannot write to stdin\", ERROR)\n        }\n\n        val formatted = format(contents)\n        if (contents != formatted) {\n          if (diffNameOnly || overwrite) {\n            // if `--diff-name-only` or `-w` is specified, only write file names\n            writeLine(pathStr)\n          }\n\n          if (overwrite) {\n            path.writeText(formatted, Charsets.UTF_8)\n          } else {\n            // only exit on violation for \"check\" operations, not when overwriting\n            status.update(FORMATTING_VIOLATION)\n          }\n        }\n\n        if (!diffNameOnly && !overwrite) {","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-cli/src/main/kotlin/org/pkl/cli/CliFormatterCommand.kt#L91-L127","documentation":"`pkl format` with `-w`/`--overwrite` and stdin (`-`) as a path cannot write the formatted result back to stdin, so handlePaths throws. Reading from stdin is only meaningful with diff/check modes where no in-place write is needed.","triggerScenarios":"Running `pkl format -w -` or `echo 'x' | pkl format --overwrite -`.","commonSituations":"Piping editor buffers into the formatter while also passing `-w` out of habit from other tools.","solutions":["Drop the `-w`/`--overwrite` flag when reading from stdin; capture the formatted output instead, e.g. `pkl format - < in.pkl > in.pkl`.","Or pass the real file path instead of `-` so in-place overwrite works."],"exampleFix":"# before\ncat foo.pkl | pkl format -w -\n# after\ncat foo.pkl | pkl format - > foo.pkl","handlingStrategy":"validation","validationCode":"if (paths.contains(\"-\") && overwrite) {\n  throw IllegalArgumentException(\"cannot combine -w/--overwrite with stdin ('-')\")\n}","typeGuard":null,"tryCatchPattern":"try {\n  pklCli.runFormat(args)\n} catch (e: CliException) {\n  if (e.message == \"Cannot write to stdin\") args.remove(\"-w\")\n  else throw e\n}","preventionTips":["Never pair `-w/--overwrite` with `-`.","When reading stdin, redirect stdout to the target file instead.","Document formatter pipelines to use `pkl format - < in > in`."],"tags":["cli","formatter","stdin"],"backgroundTag":"mutually-exclusive-flags","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}