Skip to content

AnsiConsole.systemInstall() always replaces System.err even when the terminal is backed by stdout #2316

Closed

Description

@gnodet

Problem

AnsiConsole.systemInstall() unconditionally replaces both System.out and System.err with AnsiPrintStream instances, regardless of which system stream the underlying terminal is actually attached to:

public static synchronized void systemInstall() {
    doInstall();
    System.setOut(out);
    System.setErr(err);   // always replaced
}

Both out and err are built by ansiStream(), which always uses terminal.output() as the underlying output target. That single stream is backed by whichever fd the terminal was opened on — fd 1 when selected via SysOut, fd 2 when selected via SysErr.

Consequence

When the terminal is selected via SysOut (stdout is a tty):

  • terminal.output() → fd 1
  • err AnsiPrintStream → fd 1 (stdout)
  • System.setErr(err) redirects System.err to stdout

The symmetric case holds for SysErr.

This means the stream that did not win the selection has its fd silently replaced by the winner's fd. Anything written to System.err ends up on stdout (and vice-versa in the SysErr case).

Real-world impact

Apache Maven calls TerminalBuilder.builder().systemOutput(SysOut) before AnsiConsole.systemInstall(). When a user sets -Dorg.slf4j.simpleLogger.logFile=System.err, they expect log output on stderr. Instead it arrives on stdout, because systemInstall() replaced System.err with an AnsiPrintStream backed by fd 1. See apache/maven#13387 for the workaround.

Expected behaviour

systemInstall() should only replace the stream that corresponds to the system stream the terminal is actually attached to:

  • Terminal selected via SysOut → replace System.out, leave System.err alone (or wrap it against system_err if it is itself a tty)
  • Terminal selected via SysErr → replace System.err, leave System.out alone (or wrap it against system_out if it is itself a tty)

The information needed to make this decision is already available: ((TerminalExt) terminal).getSystemStream() returns the winning SystemStream.

Correct fix

The ideal fix would provide full ANSI support for both streams when both are ttys — this would require opening a second native terminal on the other fd. At minimum, when only one stream is a tty, the other should be left as-is (or wrapped with AnsiMode.Strip against the actual system_out/system_err PrintStream) rather than being silently redirected to the tty's fd.

Activity

  1. added a commit that references this issue on Oct 11, 2026
    f5a5c53
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions