DRAFT: Flag.withFallbackPrompt: a Prompt.succeed fallback still opens NodeTerminal's readline, which consumes piped stdin
We hit this in a CLI whose subcommand reads its input from piped stdin and falls back to a prompt for one optional flag. When the fallback is taken, the handler finds stdin empty.
What we observed
Flag.withFallbackPrompt calls Prompt.run whenever the flag is missing, even when the fallback prompt needs no input, such as Prompt.succeed("x"). Prompt.run subscribes to Terminal.readInput before it looks at the prompt. On the Node platform, that creates a readline interface on process.stdin, and it reads the bytes waiting in the pipe. The interface is released at least 10 ms later, but by then the bytes are gone. A handler that does some async work first (a 30 ms sleep in the reproduction) then gets 0 bytes when it reads Stdio.stdin. With --profile x passed on the command line, so no fallback runs, the same handler gets all 11.
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/cli/Param.ts:1469-1488, packages/effect/src/cli/Prompt.ts:1234-1248, packages/platform/node-shared/src/NodeTerminal.ts:53-87).
Reproduction
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
import * as NodeServices from "@effect/platform-node/NodeServices"
import { Console, Effect, Stdio, Stream } from "effect"
import { Command, Flag, Prompt } from "effect/cli"
const cmd = Command.make(
"cmd",
{ profile: Flag.String("profile").pipe(Flag.withFallbackPrompt(Prompt.succeed("x"))) },
({ profile }) =>
Effect.gen(function*() {
yield* Effect.sleep("30 millis")
const stdio = yield* Stdio.Stdio
const bytes = yield* Stream.runFold(stdio.stdin, () => 0, (n, chunk: Uint8Array) => n + chunk.length)
yield* Console.log(`profile=${profile} stdin bytes read by handler: ${bytes}`)
})
)
const app = Command.make("app").pipe(Command.withSubcommands([cmd]))
Command.run(app, { version: "0.0.0" }).pipe(
Effect.provide(NodeServices.layer),
NodeRuntime.runMain
)The output, captured live. The result was the same on each of five runs of each command:
$ printf 'hello world' | node repro.ts cmd
profile=x stdin bytes read by handler: 0
$ printf 'hello world' | node repro.ts cmd --profile x
profile=x stdin bytes read by handler: 11The Effect.sleep("30 millis") stands in for any async step a handler takes before it reads stdin, such as loading config or a network call. With the sleep removed, the handler read all 11 bytes in our runs, so the loss depends on timing.
Where it happens
Param.ts:1469-1488:withFallbackPromptpasses any prompt,Prompt.succeedincluded, toPrompt.run.Prompt.ts:1234-1248:runyieldsterminal.readInputfirst, thenrunWithInput, whose"Succeed"case (:1522-1524) never reads the queue.NodeTerminal.ts:53-87and:124:readInputacquires anRcRefwhose resource isreadline.createInterface({ input: stdin }). It hasidleTimeToLive: "10 millis", so it outlives the prompt's scope by at least that much. Once created, the interface pulls the pipe's bytes into its own line buffer.
A second effect of the same window, from reading the source; we haven't reproduced it separately. When stdin is a TTY, the same acquire calls stdin.setRawMode(true) (NodeTerminal.ts:67-69), which stays on until the RcRef is released. A Ctrl-C typed in that window reaches the process as input rather than as SIGINT, so the usual interrupt doesn't fire.
Is this intended?
Perhaps running every fallback through Prompt.run is deliberate, so that all prompts share one code path. If not, two possible directions, which aren't exclusive:
Prompt.run(orwithFallbackPrompt) could return aPrompt.succeedvalue without touching the terminal. It already needs no input.NodeTerminalcould avoid taking stdin until a prompt actually waits for a key, or release it together with the prompt's scope rather than after an idle TTL.
The first is the narrower change. The second would also cover a real prompt that the user answers quickly, just before the handler reads stdin.
Source: Effect-TS/effect