Skip to content

Command.runWith has no way to opt out of its own console-rendered help/error output on parse failures #6313

Closed

Description

@Coly010

Effect version

4.0.0-beta.93 (effect/unstable/cli)

Description

Command.runWith's parser (packages/effect/src/unstable/cli/Command.ts) unconditionally renders its own help/error output to the console whenever a run fails with CliError.ShowHelp, with no way for a host application to opt out of, redirect, or make that rendering conditional on the specific failure. The catchFilter at the end of runWith catches every ShowHelp failure and calls showHelp(command, error) before re-failing with the same error:

const showHelp = <Name extends string, Input, E, R, ContextInput>(
  command: Command<Name, Input, ContextInput, E, R>,
  error: CliError.ShowHelp
): Effect.Effect<void, CliError.CliError, Environment> =>
  Effect.gen(function*() {
    const formatter = yield* CliOutput.Formatter
    const helpDoc = yield* getHelpForCommandPath(command, error.commandPath, GlobalFlag.BuiltIns)
    yield* Console.log(formatter.formatHelpDoc(helpDoc))
    if (error.errors.length > 0) {
      yield* Console.error(formatter.formatErrors(error.errors as any))
    }
  })

// ...
Effect.catchFilter(
  (error) =>
    CliError.isCliError(error) && error._tag === "ShowHelp"
      ? Result.succeed(error)
      : Result.fail(error),
  (error) => Effect.andThen(showHelp(command, error), Effect.fail(error))
),

A host CLI that wants full ownership of its own error/usage rendering — to match a different CLI framework's exact wording, to choose which stream a given failure lands on, or to suppress the built-in render entirely for specific failure tags — has no supported way to do that. runWith's config parameter is just { version: string }; there's no hook to skip, replace, or redirect the internal Console.log/Console.error calls.

Today the only way to get that control is to swap out the ambient Console.Console service for the duration of the run, buffer every log/error write, wait for the run's Exit, inspect the ShowHelp's errors array to classify what should have happened, and then selectively replay/redirect/drop the buffered writes. That means:

  • Re-implementing (outside the library) which ShowHelp cases the library itself would consider "should still show usage" vs. "already showed everything needed" — knowledge only runWith/showHelp really have.
  • Coupling to showHelp's exact Console call pattern (that it's exactly one log then, conditionally, one error) — any change to that internal implementation silently breaks the workaround.
  • No way to prevent the render for a subset of failures without inspecting error internals from outside the library (e.g. wanting to keep the help dump for an unrecognized flag but suppress it for a missing required flag, to match a host framework that treats those differently).

Reproduction

import { Effect, Console } from "effect"
import { Command, Flag } from "effect/unstable/cli"

const cmd = Command.make("greet", { name: Flag.string("name") }, () => Effect.void)

const writes: Array<string> = []
const bufferingConsole = {
  ...console,
  log: (...args: unknown[]) => Effect.sync(() => writes.push(`log: ${args.join(" ")}`)),
  error: (...args: unknown[]) => Effect.sync(() => writes.push(`error: ${args.join(" ")}`)),
}

const program = Command.runWith(cmd, { version: "1.0.0" })([]).pipe(
  Effect.provideService(Console.Console, bufferingConsole as any),
  Effect.exit,
)

Effect.runPromise(program).then((exit) => {
  console.log("writes:", writes) // always has the library's own help+error render
  console.log("exit:", exit)     // caller ALSO gets the ShowHelp error to handle itself
})

Running this for a missing required flag (name never supplied) produces both:

  1. The library's own Console.log/Console.error render (captured in writes above), which happens unconditionally.
  2. The ShowHelp failure surfacing through exit, for the caller to render again if it wants its own error message.

There is no runWith option to get #2 without #1.

Suggested fix

A few shapes that would work, roughly in order of preference:

  • A. Add a runWith config option (e.g. renderHelp?: boolean, default true) that skips the internal showHelp call entirely, so a host that wants to own rendering gets the bare ShowHelp failure with no console side effect at all.
  • B. Let runWith accept a render hook (e.g. config.onShowHelp?: (error: CliError.ShowHelp, formatter: CliOutput.Formatter) => Effect.Effect<void>) in place of the hardcoded Console.log/Console.error pair, so a host can redirect output per-failure-tag without needing to intercept Console at all.
  • C. At minimum, export showHelp (or an equivalent pure "format without printing" function) as public API, so hosts that need to suppress the built-in render can still reproduce it faithfully on their own terms instead of re-deriving formatter.formatHelpDoc/formatErrors's call order by trial and error.

Happy to open a PR for whichever direction maintainers prefer — we're working around this downstream in our own CLI (built on effect/unstable/cli) by swapping the ambient Console.Console service, but it's fragile against any change to showHelp's internals and duplicates classification logic the library already has.

Activity

  1. added
    3.0
    on Jul 14, 2026
  2. added
    enhancement
    and removed
    3.0
    on Jul 28, 2026
  3. schickling-assistant commented on Jul 28, 2026

    @schickling-assistant
    Contributor

    Another concrete motivation for this, from migrating a set of CLIs from v3 to v4.

    showHelp's Console.log(formatter.formatHelpDoc(helpDoc)) puts the help document on stdout. For a CLI whose stdout is a machine-readable contract — JSON or NDJSON consumed by another process — any parse failure now corrupts that stream: the consumer receives a help document where it expected a JSON document.

    In v3 the equivalent failures wrote nothing to stdout, so this is a behaviour change rather than a formatting preference. We found it via a differential harness that runs identical scenarios against v3 and v4 and diffs stdout/stderr/exit code; it showed up as help text appearing on stdout across every argument-error scenario.

    Two observations that may be useful for shaping the fix:

    • The stream choice matters independently of the opt-out. Even for CLIs that are happy with the built-in rendering, usage/help arguably belongs on stderr when it is emitted as part of a failure — that alone would resolve the stdout-corruption case without any new API.
    • The opt-out this issue asks for is still valuable for the wording/ownership reasons already described; the two are complementary rather than alternatives.

    Versions: effect 4.0.0-beta.102, Bun.

    Posted on behalf of @schickling
    field value
    agent_name cl1-heron
    agent_session_id 54711470-ae7e-4322-a3e2-129a2689a097
    agent_tool Claude Code
    agent_tool_version 2.1.220
    agent_runtime Claude Code 2.1.220
    agent_model claude-opus-5
    runtime_profile /nix/store/xg6r3nkr7spjyg2gbl3zprqbsag3fsjc-coding-agent-runtime-profile/share/coding-agents/profile.json
    skills_manifest /nix/store/2vkpfkkbc9hjmcwknzkv62v2i44an8mz-agent-skills-corpus/share/agent-skills/manifest.json
    worktree repro-cli/main
    machine dev3
    tooling_profile dotfiles@unknown-dirty
  4. IMax153 commented on Jul 29, 2026

    @IMax153
    Member

    @Coly010 @schickling-assistant - could you all take a look at #6743 and see if it satisfies your request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    4.0enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions