codeconsole commented on code in PR #3309:
URL: https://github.com/apache/groovy/pull/3309#discussion_r4624501824


##########
src/main/java/org/codehaus/groovy/tools/shell/IO.java:
##########
@@ -109,19 +106,53 @@ public IO(final InputStream inputStream, final 
OutputStream outputStream, final
     /**
      * Attempts to create an ANSI-aware writer for the supplied stream.
      *
+     * <p>Only the JVM's own standard output and error streams can render 
ANSI, through JLine's Jansi
+     * {@code AnsiConsole}, which also enables virtual terminal processing on 
Windows. A stream that is
+     * redirected to a file or a pipe gets no ANSI writer, so no escape 
sequences reach it.</p>
+     *
      * @param stream the output stream to wrap
      * @return an ANSI render writer, or {@code null} if ANSI support is
      * unavailable
      */
     protected PrintWriter tryConstructRenderWriter(OutputStream stream) {
-        // load via reflection to avoid hard-coded dependency on jansi jar
+        if (!JANSI_AVAILABLE) {
+            return null;
+        }
         try {
-            return (PrintWriter) 
InvokerHelper.invokeConstructorOf(ANSI_RENDER_WRITER, new Object[]{stream, 
true});
-        } catch (ClassNotFoundException ignore) {
+            return JansiConsole.writerFor(stream);
+        } catch (IOError | RuntimeException | LinkageError e) {
             return null;
         }
     }
 
+    private static boolean isJansiAvailable() {
+        // JLine's Jansi is optional for groovy, so look it up before 
JansiConsole links against it
+        try {
+            Class.forName(ANSI_CONSOLE, false, IO.class.getClassLoader());
+            return true;
+        } catch (ClassNotFoundException | LinkageError e) {
+            return false;
+        }
+    }
+
+    /**
+     * Refers to JLine's Jansi, and is loaded only once {@link 
#isJansiAvailable()} found it.
+     */
+    private static final class JansiConsole {
+        static PrintWriter writerFor(OutputStream stream) {
+            AnsiPrintStream console;
+            // AnsiConsole writes to the streams the JVM started with, so a 
replaced System.out keeps its own
+            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);

Review Comment:
   Both reproduced: on JDK 25 a piped `groovydoc` printed the restricted-method 
warning, and a forked test that replaces `System.out` before logging through 
`Logger` (the Ant path) showed the line skipping the replaced stream and going 
to the process stdout.
   
   Fixed in 9bb03d5667:
   - `IO` leaves `AnsiConsole` untouched unless `jansi.mode` is `force`, or is 
not `strip` and the JVM has a terminal (`Console.isTerminal()` where the JDK 
has it), so piped and CI runs never load JLine.
   - The constructors give plain writers again. Only an IO from the new 
`IO.forConsole()` can color, and only groovydoc's `Main` uses it, so Ant, 
Gradle and embedded use through `Logger` keep writing through the host's 
`System.out`.
   
   In 4f31e60c14 the console is also opened only by `Logger`'s first colored 
line, not when the IO is created, and not at all if `System.out` was replaced 
in between.
   
   The new forked tests cover a `System.out` replaced before `Logger` creates 
its IO and one replaced before the first line. The redirected and `strip` tests 
assert that no console was opened and that stderr has no native-access warning.



-- 
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