Skip to content

GROOVY-12784: The shell Logger never prints ANSI colors because IO looks for the old Jansi - #3309

Open
codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:GROOVY-12784
Open

codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:GROOVY-12784

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/GROOVY-12784

IO looked for org.fusesource.jansi.AnsiRenderWriter, which Groovy has not shipped since GROOVY-8162, so ansiSupported was always false and the shell Logger (groovydoc's logger) printed DEBUG, WARN and ERROR uncolored.

IO now renders through JLine Jansi's AnsiConsole, for the JVM's own standard output and error only:

  • a terminal gets the colors (bold green DEBUG, bold red WARN and ERROR)
  • output redirected to a file or a pipe stays plain, since AnsiConsole strips the escape sequences there
  • -Djansi.mode=force / strip still override the detection
  • any other stream, or a System.out replaced after AnsiConsole captured the original, keeps a plain PrintWriter

Jansi stays optional: IO checks for org.jline.jansi.AnsiConsole before touching it. Core's tests now run with the logging feature, as they already do with grapes, so IOTest can cover the redirected and the forced cases in a forked JVM.

Opening the console builds JLine's system terminal, which sends JLine's usual terminal probes (DSR/DECRQM) and reads the replies itself, as groovysh does when it starts.

…oks for the old Jansi

IO looked for org.fusesource.jansi.AnsiRenderWriter, which Groovy has not
shipped since GROOVY-8162 replaced Jansi with JLine's org.jline:jansi, so
ansiSupported was always false and Logger printed its levels uncolored.

IO now renders through JLine Jansi's AnsiConsole for the JVM's own standard
output and error. AnsiConsole strips the escape sequences when the stream is
redirected, honors jansi.mode, and enables virtual terminal processing on
Windows; every other stream keeps a plain writer.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 61.11111% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.5899%. Comparing base (88b30dd) to head (47ee89a).

Files with missing lines Patch % Lines
.../main/java/org/codehaus/groovy/tools/shell/IO.java 61.1111% 4 Missing and 3 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3309        +/-   ##
==================================================
+ Coverage     74.5879%   74.5899%   +0.0020%     
- Complexity      45361      45365         +4     
==================================================
  Files            1707       1707                
  Lines          153183     153187         +4     
  Branches        29442      29445         +3     
==================================================
+ Hits           114256     114262         +6     
+ Misses          28418      28415         -3     
- Partials        10509      10510         +1     
Files with missing lines Coverage Δ
.../main/java/org/codehaus/groovy/tools/shell/IO.java 49.2754% <61.1111%> (+4.6600%) ⬆️

... and 11 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@paulk-asert

Copy link
Copy Markdown
Contributor

Thanks for this — the diagnosis is right. IO was still probing for org.fusesource.jansi.AnsiRenderWriter, so ansiSupported has been false since the move to JLine's Jansi, and the reflective lookup is part of why nobody noticed.

The nested-holder approach also keeps Jansi optional, which was the point of the original reflection: IO itself has no Jansi types in its bytecode, and JansiConsole is only linked after the Class.forName check succeeds.

Two things I'd like addressed before merging.

1. Native-access warnings on JDK 24+

AnsiConsole.out() builds a JLine system terminal, which loads the JNI library. I ran a small probe making the same AnsiConsole.out()/err() calls against jansi-4.5.0.jar:

Run Result
JDK 21, console no warning
JDK 24 / 25 / 27, console warning, colors available
JDK 25, stdout piped warning, and a dumb terminal, so no colors
JDK 25, piped, --enable-native-access=ALL-UNNAMED no warning

The warning is:

WARNING: A restricted method in java.lang.System has been called
WARNING: java.lang.System::load has been called by org.jline.nativ.JLineNativeLoader in an unnamed module (...)
WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
WARNING: Restricted methods will be blocked in a future release unless native access is enabled

groovydoc.Main calls new IO() as its first statement, so every groovydoc run would print this, including runs redirected to a file or CI log, where no colors are produced anyway. Only the groovysh launchers pass --enable-native-access today. Launcher flags also can't help the Ant and Gradle groovydoc tasks, or anyone using GroovyDocTool or IO in their own JVM.

Could we:

  • skip AnsiConsole entirely when there is no terminal (e.g. System.console() == null) and jansi.mode isn't force, so piped, CI and daemon runs never load JLine;
  • create the console lazily, on the first log line that would be colored, rather than in the IO constructor;
  • add --enable-native-access=ALL-UNNAMED to the groovydoc launchers, as groovysh and groovysh.bat (via NATIVE_FLAG) already do.

That leaves embedded use on a real terminal on JDK 24+, which we can cover in the release notes.

2. The replaced-System.out guard

stream == System.out && System.out == AnsiConsole.sysOut() only detects a replacement made after AnsiConsole was initialized. AnsiConsole captures System.out in a static initializer, and this call is normally what triggers it, so a System.out that a host replaced earlier passes the check. If the process's stdout is a terminal, the stream returned by AnsiConsole.out() then writes to the terminal directly rather than through the host's stream.

The case I have in mind is the Ant groovydoc task run from a terminal, where log lines would bypass Ant's logger, -quiet and -logfile. This is from reading the JLine source, not from a test. Could the check compare against something known to be the JVM's original stream, or failing that, could the limitation be documented?

Minor

  • ansiSupported is public and changes meaning from "Jansi is on the classpath" to "out is rendering ANSI". I think the new meaning is the more useful one, but it's derived from out only, so it's worth saying so in the Javadoc.
  • The tests cover the redirected and forced cases well. A test for the Jansi-absent path would be nice if it's cheap, but I wouldn't hold the PR for it.

@testlens-app

testlens-app Bot commented Nov 17, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 47ee89a
▶️ Tests: 133148 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Scott. The diagnosis is right: IO still looks for org.fusesource.jansi.AnsiRenderWriter, which Groovy has not shipped since GROOVY-8162, so ansiSupported has been false and the shell Logger has printed DEBUG, WARN and ERROR uncolored. Detecting JLine's AnsiConsole and keeping Jansi optional with the nested holder is the right approach, and the new tests cover the non-standard and redirected cases well, including jansi.mode=force. There is no ABI change.

I do have two blocking comments, in the same area Paul already raised.

1. Native-access warnings and eager console install

JansiConsole.writerFor calls AnsiConsole.out() / err() as soon as the stream identity check passes, and only then asks whether the mode is Strip. Those methods always run doInstall(), which builds a JLine system terminal and, with Jansi 4.5.0, loads the JNI library (System.load). On JDK 24+ that prints the restricted-method warning unless --enable-native-access is set.

groovydoc.Main constructs new IO() as its first statement, so every official CLI run takes this path. For a pipe or a CI log, JLine typically falls back to a dumb terminal, writerFor sees Strip and discards the console — but the native load and the warning have already happened, and no colors are produced. That is a regression from current master, where IO never loads JLine. Only the groovysh launchers pass --enable-native-access=ALL-UNNAMED today.

Could we skip AnsiConsole entirely when there is no terminal and jansi.mode is not force, so piped, CI and daemon runs never load JLine? System.console() == null is a useful first check, though it is not a complete TTY test (stdout can still be a terminal when stdin is redirected). Please also add --enable-native-access=ALL-UNNAMED to the groovydoc launchers, as groovysh and groovysh.bat already do. Launcher flags cannot help Ant, Gradle, or embedded GroovyDocTool / IO; the no-terminal skip is what protects those. Embedded use on a real terminal on JDK 24+ can be mentioned in the release notes.

Creating the console lazily on the first colored log line would also help, but I would not hold the PR for that: out, err and ansiSupported are final, and the no-terminal short-circuit already removes the costly install from CI.

2. The replaced-System.out guard

stream == System.out && System.out == AnsiConsole.sysOut() only detects a replacement made after AnsiConsole has been initialized. sysOut() returns the System.out captured in a static initializer, and this call is normally what triggers that initializer, so a System.out that a host already installed passes the check. If the process's stdout is a terminal, AnsiConsole.out() then writes to terminal.output() (on POSIX, FileDescriptor.out) rather than through the host stream.

GroovyDocTool and the other groovydoc writers use this Logger, and the official Ant task goes through GroovyDocTool, not Main. Run from a terminal, log lines would bypass Ant's logger, -quiet and -logfile. That is a regression from master, where these messages go through the host System.out. I have this from the JLine 4.5.0 sources rather than from a live Ant run. Could we wrap the caller-provided stream, or otherwise refuse AnsiConsole.out() when the stream is not known to be the JVM's original one, and add a test that replaces System.out first? Documenting the limitation would not be enough, because the bypass is a behavior change for a normal Ant or Gradle invocation.

A couple of non-blocking notes: it would be useful if the redirected fork test also asserted that the child stderr has no native-access warning. I would not hold the PR for a Jansi-absent test, or for the public ansiSupported meaning change — the new meaning is the more useful one, and the Javadoc already ties it to out. The log hot path after a successful install is not a concern.

Comment on lines +145 to +152
if (stream == System.out && System.out == AnsiConsole.sysOut()) {
console = AnsiConsole.out();
} else if (stream == System.err && System.err == AnsiConsole.sysErr()) {
console = AnsiConsole.err();
} else {
return null;
}
return console.getMode() == AnsiMode.Strip ? null : new PrintWriter(console, true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnsiConsole.out() / err() always run doInstall() before this method can see that the mode is Strip, so a piped or CI new IO() still loads the JNI library and, on JDK 24+, prints a native-access warning, then throws the console away. Could we skip AnsiConsole when there is no terminal and jansi.mode is not force?

The System.out == AnsiConsole.sysOut() check also only sees a replacement made after AnsiConsole was initialized. A host such as Ant that replaced System.out earlier passes the check, and on a TTY AnsiConsole.out() then writes to terminal.output() rather than through the host stream, which would bypass Ant's logger, -quiet and -logfile. Could we wrap the caller-provided stream, or otherwise refuse AnsiConsole.out() when the stream is not known to be the JVM's original one?

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants