DRAFT: Stdio: no stderrIsTerminal, so a program can't tell whether stderr is a terminal
Thanks for #7088 and #7090. stdinIsTerminal and stdoutIsTerminal let us drop our own process.*.isTTY reads for stdin and stdout, and tests can now substitute them. We have one follow-up question about the third stream.
What we observed
Stdio answers whether stdin and stdout are terminals, but not stderr. A CLI that writes diagnostics to stderr decides colour and hyperlinks for that stream from stderr's own TTY state. That state often differs from stdout's: tool | less pipes stdout while stderr stays on the terminal, and tool 2>log.txt does the opposite. Through Stdio, the only choices are to reach past the platform abstraction to process.stderr.isTTY, or to borrow stdout's answer, which is wrong in both of those cases.
Environment: [email protected] (tag [email protected], ad61db8) and @effect/[email protected], on Node 26.10.0 and macOS. From reading the source, the same lines are unchanged on main at 8b0eac6 (packages/effect/src/Stdio.ts:63-85, packages/platform/node-shared/src/NodeStdio.ts:31-32).
Reproduction
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
import * as NodeServices from "@effect/platform-node/NodeServices"
import { Console, Effect, Stdio } from "effect"
Effect.gen(function*() {
const stdio = yield* Stdio.Stdio
yield* Console.log(`stdinIsTerminal: ${yield* stdio.stdinIsTerminal}`)
yield* Console.log(`stdoutIsTerminal: ${yield* stdio.stdoutIsTerminal}`)
yield* Console.log(`Stdio members: ${Object.keys(stdio).filter((k) => k.endsWith("IsTerminal")).join(", ")}`)
yield* Console.log(`process.stderr.isTTY (outside Stdio): ${process.stderr.isTTY === true}`)
}).pipe(Effect.provide(NodeServices.layer), NodeRuntime.runMain)We ran it in a terminal three ways and captured the output live:
$ node repro.ts
stdinIsTerminal: true
stdoutIsTerminal: true
Stdio members: stdinIsTerminal, stdoutIsTerminal
process.stderr.isTTY (outside Stdio): true
$ node repro.ts 2>/dev/null
stdinIsTerminal: true
stdoutIsTerminal: true
Stdio members: stdinIsTerminal, stdoutIsTerminal
process.stderr.isTTY (outside Stdio): false
$ node repro.ts | cat
stdinIsTerminal: true
stdoutIsTerminal: false
Stdio members: stdinIsTerminal, stdoutIsTerminal
process.stderr.isTTY (outside Stdio): trueIn the second and third runs, stderr's terminal state differs from stdout's, and Stdio has no member that reports it.
Where it happens
Stdio.ts:63-85: the interface declaresstdinIsTerminalandstdoutIsTerminal, beside astderr()sink with no matching check.NodeStdio.ts:31-32: the Node layer implements those two fromprocess.stdin.isTTYandprocess.stdout.isTTY.
How we work around it
In a CLI kit we maintain, the environment layer takes a stderrIsTerminal option from the program's entry point, and otherwise mirrors stdout's answer. That keeps process reads out of library code, but the fallback is wrong in exactly the cases above.
Would a stderrIsTerminal fit?
Was stderr left out deliberately? If not, one possible direction is a stderrIsTerminal: Effect<boolean> beside the other two, with the same Stdio.make default and layerTest override. We haven't looked at what the Deno and Bun layers would need.
Related
- #7088 (closed) and #7090: stdin and stdout terminal state on
Stdio. This issue is a follow-up to them.
Source: Effect-TS/effect