paulk-asert commented on PR #3309:
URL: https://github.com/apache/groovy/pull/3309#issuecomment-6939497368

   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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to