apple/pkl · error · CliException
Cannot write to stdin
Error message
Cannot write to stdin
What it means
`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.
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.
Example fix
# before cat foo.pkl | pkl format -w - # after cat foo.pkl | pkl format - > foo.pkl
Defensive patterns
Strategy: validation
Validate before calling
if (paths.contains("-") && overwrite) {
throw IllegalArgumentException("cannot combine -w/--overwrite with stdin ('-')")
} Try / catch
try {
pklCli.runFormat(args)
} catch (e: CliException) {
if (e.message == "Cannot write to stdin") args.remove("-w")
else throw e
} Prevention
- Never pair `-w/--overwrite` with `-`.
- When reading stdin, redirect stdout to the target file instead.
- Document formatter pipelines to use `pkl format - < in > in`.
When it happens
Trigger: Running `pkl format -w -` or `echo 'x' | pkl format --overwrite -`.
Common situations: Piping editor buffers into the formatter while also passing `-w` out of habit from other tools.
Understand the failure class
Background: "mutually exclusive" flag errors: what "can't supply both nx and xx", "--raw is not compatible with -i" and "cannot be used with" mean, and how to fix them — this error's family across 29 libraries.
Related errors
- Cannot download packages because no cache directory is…
- Cannot generate documentation for just one module within a…
- Cannot generate JUnit report for $moduleUri. A report with…
- Cannot substitute output path placeholder
- CliException(e.message!!)
AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08).
Data as JSON: /api/errors/8b62ce582c1e2131.
Report an issue: GitHub.
Appendix: source
Thrown at pkl-cli/src/main/kotlin/org/pkl/cli/CliFormatterCommand.kt:109
if (!silent) {
writeErrLine("An error occurred during formatting.")
}
throw CliTestException("", status.status)
}
}
}
private fun handlePaths(status: Status) {
for (path in allPaths()) {
val pathStr = path.toString()
try {
val contents =
when {
pathStr == "-" -> IoUtils.readString(System.`in`)
else -> Files.readString(path)
}
if (pathStr == "-" && overwrite) {
throw CliException("Cannot write to stdin", ERROR)
}
val formatted = format(contents)
if (contents != formatted) {
if (diffNameOnly || overwrite) {
// if `--diff-name-only` or `-w` is specified, only write file names
writeLine(pathStr)
}
if (overwrite) {
path.writeText(formatted, Charsets.UTF_8)
} else {
// only exit on violation for "check" operations, not when overwriting
status.update(FORMATTING_VIOLATION)
}
}
if (!diffNameOnly && !overwrite) {View on GitHub (pinned to f3efcbfc9b)