Repository navigation
GROOVY-12784: The shell Logger never prints ANSI colors because IO looks for the old Jansi - #3309
codeconsole wants to merge 1 commit into
Conversation
…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 Report❌ Patch coverage is
Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
|
Thanks for this — the diagnosis is right. The nested-holder approach also keeps Jansi optional, which was the point of the original reflection: Two things I'd like addressed before merging. 1. Native-access warnings on JDK 24+
The warning is:
Could we:
That leaves embedded use on a real terminal on JDK 24+, which we can cover in the release notes. 2. The replaced-
The case I have in mind is the Ant Minor
|
✅ All tests passed ✅🏷️ Commit: 47ee89a Learn more about TestLens at testlens.app/docs. |
daniellansun
left a comment
There was a problem hiding this comment.
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.
| 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); |
There was a problem hiding this comment.
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?
https://issues.apache.org/jira/browse/GROOVY-12784
IOlooked fororg.fusesource.jansi.AnsiRenderWriter, which Groovy has not shipped since GROOVY-8162, soansiSupportedwas alwaysfalseand the shellLogger(groovydoc's logger) printed DEBUG, WARN and ERROR uncolored.IOnow renders through JLine Jansi'sAnsiConsole, for the JVM's own standard output and error only:AnsiConsolestrips the escape sequences there-Djansi.mode=force/stripstill override the detectionSystem.outreplaced afterAnsiConsolecaptured the original, keeps a plainPrintWriterJansi stays optional:
IOchecks fororg.jline.jansi.AnsiConsolebefore touching it. Core's tests now run with theloggingfeature, as they already do withgrapes, soIOTestcan 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.