This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch maven-3.10.x
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/maven-3.10.x by this push:
new f71e6803f5 [3.10.x] Wait for terminal installation before caching
logging output (#13799)
f71e6803f5 is described below
commit f71e6803f5ab1de01a3c81b11823d394602cf415
Author: Goutam Adwant <[email protected]>
AuthorDate: Thu Nov 5 17:39:06 2026 -0800
[3.10.x] Wait for terminal installation before caching logging output
(#13799)
* Wait for terminal installation before caching logging output (fix #13793)
Terminal installation replaces System.out asynchronously. When color is
explicitly selected or batch mode is used, logging can activate before
that replacement and cache the original stream. Logger and direct stdout
writes then use different stream locks for the same output destination.
Wait for terminal initialization before activating logging, preserving
configured stream caching and the existing log-file redirection barrier.
Add deterministic regression tests for forced color, disabled color and
batch mode that verify logging uses the installed stream.
* Clarify terminal wait ordering and isolate logging tests
Explain why the logging activation wait also follows the log-file
redirection wait. Isolate tests that modify JVM-wide terminal and logging state
from parallel test execution.
* Fix: skip terminal await for -color=always to preserve ANSI codes
When -color=always is set, waiting for terminal initialization before
activating logging causes AnsiConsole.systemInstall() to wrap System.out
before the logger caches it. In non-TTY environments (CI, pipes), this
wrapper strips ANSI escape codes even for a dumb-color terminal, defeating
the purpose of -color=always.
Skip the terminal await on the non-log-file path when color is explicitly
forced (always/yes/force). The logger then caches the original stream
before AnsiConsole can wrap it, allowing ANSI codes to flow through.
Update the unit test to reflect this: with forced color the logger
intentionally caches the early stream, not the terminal-installed stream.
The existing tests for 'never' and batch mode still verify that the
terminal await happens on those paths.
---------
Co-authored-by: Guillaume Nodet <[email protected]>
---
.../main/java/org/apache/maven/cli/MavenCli.java | 14 +-
.../org/apache/maven/cli/MavenCliLoggingTest.java | 235 +++++++++++++++++++++
2 files changed, 247 insertions(+), 2 deletions(-)
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
index 19c290fc06..7e9e4e0b12 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
@@ -620,9 +620,19 @@ void logging(CliRequest cliRequest) {
// Ignore
//
}
- }
- slf4jConfiguration.activate();
+ // Logging caches System.out; with -l the wait above already ran,
so activate now.
+ slf4jConfiguration.activate();
+ } else {
+ // Logging caches System.out, so wait for terminal installation to
complete before
+ // activating, except when color is explicitly forced. With forced
color the user expects
+ // raw ANSI to flow through the original stdout; waiting causes
AnsiConsole to wrap that
+ // stream before the logger caches it, which may strip the escape
codes.
+ if (!"always".equals(styleColor) && !"yes".equals(styleColor) &&
!"force".equals(styleColor)) {
+ MessageUtils.awaitTerminalInitialization();
+ }
+ slf4jConfiguration.activate();
+ }
plexusLoggerManager = new Slf4jLoggerManager();
slf4jLogger = slf4jLoggerFactory.getLogger(this.getClass().getName());
diff --git
a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliLoggingTest.java
b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliLoggingTest.java
new file mode 100644
index 0000000000..de46e5c7e8
--- /dev/null
+++ b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliLoggingTest.java
@@ -0,0 +1,235 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.cli;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.FutureTask;
+
+import org.apache.maven.cli.logging.impl.Slf4jSimpleConfiguration;
+import org.apache.maven.jline.FastTerminal;
+import org.apache.maven.jline.MessageUtils;
+import org.jline.terminal.Terminal;
+import org.jline.terminal.spi.TerminalExt;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Isolated;
+import org.slf4j.LoggerFactory;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+@Isolated("Changes JVM-wide streams, system properties, terminal state, and
logging configuration")
+class MavenCliLoggingTest {
+ /**
+ * When color is explicitly forced ({@code -color=always}), logging must
NOT wait for
+ * terminal installation before caching {@code System.out}. Waiting causes
+ * {@code AnsiConsole.systemInstall()} to wrap the stream before the
logger sees it; in a
+ * non-TTY environment (CI, pipes) that wrapper strips ANSI codes even for
a dumb-color
+ * terminal, defeating the purpose of {@code -color=always}.
+ * <p>
+ * Therefore the logger caches the stream that was active when {@code
logging()} was called
+ * (the "early" stream), and continues to write to it even after terminal
installation
+ * replaces {@code System.out}.
+ */
+ @Test
+ void withColorForcedLoggerCachesEarlyStream() throws Exception {
+ PrintStream originalOut = System.out;
+ Terminal originalTerminal = MessageUtils.getTerminal();
+ boolean originalColor;
+ try {
+ MessageUtils.systemInstall(mock(Terminal.class));
+ originalColor = MessageUtils.isColorEnabled();
+ } finally {
+ MessageUtils.systemInstall(originalTerminal);
+ }
+ String cacheProperty = "org.slf4j.simpleLogger.cacheOutputStream";
+ String fileProperty = "org.slf4j.simpleLogger.logFile";
+ String originalCache = System.getProperty(cacheProperty);
+ String originalFile = System.getProperty(fileProperty);
+ CountDownLatch buildingTerminal = new CountDownLatch(1);
+ CountDownLatch releaseTerminal = new CountDownLatch(1);
+ ByteArrayOutputStream earlyOutput = new ByteArrayOutputStream();
+ ByteArrayOutputStream terminalOutput = new ByteArrayOutputStream();
+ Thread loggingThread = null;
+ try (PrintStream earlyStream = new PrintStream(earlyOutput, true,
UTF_8.name());
+ PrintStream terminalStream = new PrintStream(terminalOutput,
true, UTF_8.name())) {
+ System.setProperty(cacheProperty, "true");
+ System.setProperty(fileProperty, "System.out");
+ System.setOut(earlyStream);
+ new Slf4jSimpleConfiguration().activate();
+
+ FastTerminal terminal = new FastTerminal(
+ () -> {
+ buildingTerminal.countDown();
+ if (!releaseTerminal.await(10, SECONDS)) {
+ throw new IllegalStateException("Terminal
installation was not released");
+ }
+ return mock(TerminalExt.class);
+ },
+ installed -> System.setOut(terminalStream));
+ MessageUtils.systemInstall(terminal);
+ assertTrue(buildingTerminal.await(10, SECONDS));
+
+ MavenCli cli = new MavenCli();
+ CliRequest request = new CliRequest(new String[]
{"-Dstyle.color=always"}, null);
+ cli.cli(request);
+ cli.properties(request);
+ FutureTask<Void> logging = new FutureTask<>(() -> {
+ cli.logging(request);
+ return null;
+ });
+ loggingThread = new Thread(logging, "logging-initialization-test");
+ loggingThread.setDaemon(true);
+ loggingThread.start();
+ // With -color=always, logging() does NOT await terminal
initialization, so it
+ // completes without blocking. Release the terminal after logging
finishes.
+ logging.get(10, SECONDS);
+ releaseTerminal.countDown();
+ terminal.getTerminal();
+
+ // The logger must have cached earlyStream (active when logging()
ran), not the
+ // terminalStream that the installed callback set afterwards.
+
LoggerFactory.getLogger(MavenCliLoggingTest.class).warn("terminal-output-marker");
+ assertTrue(
+
earlyOutput.toString(UTF_8.name()).contains("terminal-output-marker"),
+ "Logger must write to the stream that was active during
logging() with -color=always");
+ assertFalse(
+
terminalOutput.toString(UTF_8.name()).contains("terminal-output-marker"),
+ "Logger must not switch to the terminal stream when color
is forced");
+ } finally {
+ releaseTerminal.countDown();
+ if (loggingThread != null) {
+ loggingThread.join(10000);
+ }
+ System.setOut(originalOut);
+ MessageUtils.systemInstall(originalTerminal);
+ MessageUtils.setColorEnabled(originalColor);
+ restoreProperty(cacheProperty, originalCache);
+ restoreProperty(fileProperty, originalFile);
+ new Slf4jSimpleConfiguration().activate();
+ }
+ }
+
+ @Test
+ void cachesOutputAfterTerminalInstallationWithColorDisabled() throws
Exception {
+ cachesOutputAfterTerminalInstallation("never");
+ }
+
+ @Test
+ void cachesOutputAfterTerminalInstallationInBatchMode() throws Exception {
+ cachesOutputAfterTerminalInstallation("batch");
+ }
+
+ private void cachesOutputAfterTerminalInstallation(String color) throws
Exception {
+ PrintStream originalOut = System.out;
+ Terminal originalTerminal = MessageUtils.getTerminal();
+ boolean originalColor;
+ try {
+ // isColorEnabled also requires a terminal, even when the color
flag is set.
+ MessageUtils.systemInstall(mock(Terminal.class));
+ originalColor = MessageUtils.isColorEnabled();
+ } finally {
+ MessageUtils.systemInstall(originalTerminal);
+ }
+ String cacheProperty = "org.slf4j.simpleLogger.cacheOutputStream";
+ String fileProperty = "org.slf4j.simpleLogger.logFile";
+ String originalCache = System.getProperty(cacheProperty);
+ String originalFile = System.getProperty(fileProperty);
+ CountDownLatch buildingTerminal = new CountDownLatch(1);
+ CountDownLatch releaseTerminal = new CountDownLatch(1);
+ CountDownLatch awaitingTerminalOrLoggingFinished = new
CountDownLatch(1);
+ ByteArrayOutputStream earlyOutput = new ByteArrayOutputStream();
+ ByteArrayOutputStream terminalOutput = new ByteArrayOutputStream();
+ Thread loggingThread = null;
+ try (PrintStream earlyStream = new PrintStream(earlyOutput, true,
UTF_8.name());
+ PrintStream terminalStream = new PrintStream(terminalOutput,
true, UTF_8.name())) {
+ System.setProperty(cacheProperty, "true");
+ System.setProperty(fileProperty, "System.out");
+ System.setOut(earlyStream);
+ new Slf4jSimpleConfiguration().activate();
+
+ FastTerminal terminal =
+ new FastTerminal(
+ () -> {
+ buildingTerminal.countDown();
+ if (!releaseTerminal.await(10, SECONDS)) {
+ throw new IllegalStateException("Terminal
installation was not released");
+ }
+ return mock(TerminalExt.class);
+ },
+ installed -> System.setOut(terminalStream)) {
+ @Override
+ public TerminalExt getTerminal() {
+ awaitingTerminalOrLoggingFinished.countDown();
+ return super.getTerminal();
+ }
+ };
+ MessageUtils.systemInstall(terminal);
+ assertTrue(buildingTerminal.await(10, SECONDS));
+
+ MavenCli cli = new MavenCli();
+ CliRequest request = new CliRequest(
+ "batch".equals(color) ? new String[] {"-B"} : new String[]
{"-Dstyle.color=" + color}, null);
+ cli.cli(request);
+ cli.properties(request);
+ FutureTask<Void> logging = new FutureTask<>(() -> {
+ try {
+ cli.logging(request);
+ return null;
+ } finally {
+ awaitingTerminalOrLoggingFinished.countDown();
+ }
+ });
+ loggingThread = new Thread(logging, "logging-initialization-test");
+ loggingThread.setDaemon(true);
+ loggingThread.start();
+ assertTrue(awaitingTerminalOrLoggingFinished.await(10, SECONDS));
+ releaseTerminal.countDown();
+ logging.get(10, SECONDS);
+ terminal.getTerminal();
+
+
LoggerFactory.getLogger(MavenCliLoggingTest.class).warn("terminal-output-marker");
+
assertTrue(terminalOutput.toString(UTF_8.name()).contains("terminal-output-marker"));
+
assertFalse(earlyOutput.toString(UTF_8.name()).contains("terminal-output-marker"));
+ } finally {
+ releaseTerminal.countDown();
+ if (loggingThread != null) {
+ loggingThread.join(10000);
+ }
+ System.setOut(originalOut);
+ MessageUtils.systemInstall(originalTerminal);
+ MessageUtils.setColorEnabled(originalColor);
+ restoreProperty(cacheProperty, originalCache);
+ restoreProperty(fileProperty, originalFile);
+ new Slf4jSimpleConfiguration().activate();
+ }
+ }
+
+ private static void restoreProperty(String name, String value) {
+ if (value == null) {
+ System.clearProperty(name);
+ } else {
+ System.setProperty(name, value);
+ }
+ }
+}