This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-script-interpreter.git


The following commit(s) were added to refs/heads/master by this push:
     new bd995cf  Fix #214: FileLogger no longer swallows write failures
bd995cf is described below

commit bd995cf06806a13e41d1a99c7d3cd1b3749bf620
Author: Arturo Bernal <[email protected]>
AuthorDate: Wed Nov 4 12:38:19 2026 +0100

    Fix #214: FileLogger no longer swallows write failures
    
    consumeLine wrote through a PrintStream, which never reports a
    failed write through an exception, only an internal error flag
    nothing here ever checked. A failed write to the log file was
    silently lost.
    
    Write through the real OutputStream instead, which throws IOException
    on a failed write, and surface it as an UncheckedIOException from
    consumeLine and close. Add getOutputStream to expose that stream
    directly, and deprecate getPrintStream in favor of it, keeping it
    working by wrapping the same stream for existing callers.
    
    Claude-Session: https://claude.ai/code/session_0153qyh22xBMN1vPfWGz3jSH
---
 .../maven/shared/scriptinterpreter/FileLogger.java | 73 +++++++++++++++++-----
 .../shared/scriptinterpreter/FileLoggerTest.java   | 58 +++++++++++++++++
 2 files changed, 115 insertions(+), 16 deletions(-)

diff --git 
a/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java 
b/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java
index 229c46c..aa679cd 100644
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java
+++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java
@@ -23,6 +23,8 @@ import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintStream;
+import java.io.UncheckedIOException;
+import java.io.UnsupportedEncodingException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.OpenOption;
@@ -39,9 +41,17 @@ public class FileLogger implements ExecutionLogger, 
AutoCloseable {
     private File file;
 
     /**
-     * The underlying file stream this logger writes to.
+     * The underlying file stream this logger writes to. Writing through this 
stream, instead of
+     * through a {@link PrintStream}, lets {@link #consumeLine(String)} detect 
a failed write
+     * instead of silently losing it.
      */
-    private PrintStream stream;
+    private final OutputStream outputStream;
+
+    /**
+     * Lazily created {@link PrintStream} view of {@link #outputStream}, 
returned by the deprecated
+     * {@link #getPrintStream()}.
+     */
+    private PrintStream printStream;
 
     /**
      * Creates a new logger that writes to the specified file.
@@ -63,7 +73,7 @@ public class FileLogger implements ExecutionLogger, 
AutoCloseable {
     public FileLogger(File outputFile, FileLoggerMirrorHandler mirrorHandler) 
throws IOException {
         this.file = outputFile;
 
-        OutputStream outputStream;
+        OutputStream rawStream;
 
         if (outputFile != null) {
             Path outputPath = outputFile.toPath();
@@ -71,16 +81,15 @@ public class FileLogger implements ExecutionLogger, 
AutoCloseable {
             if (parent != null) {
                 Files.createDirectories(parent);
             }
-            outputStream = createOutputStream(outputPath);
+            rawStream = createOutputStream(outputPath);
         } else {
-            outputStream = new NullOutputStream();
+            rawStream = new NullOutputStream();
         }
 
         if (mirrorHandler != null) {
-            this.stream = new PrintStream(
-                    new MirrorStreamWrapper(outputStream, mirrorHandler), 
false, StandardCharsets.UTF_8.name());
+            this.outputStream = new MirrorStreamWrapper(rawStream, 
mirrorHandler);
         } else {
-            this.stream = new PrintStream(new 
LineFeedNormalizer(outputStream), false, StandardCharsets.UTF_8.name());
+            this.outputStream = new LineFeedNormalizer(rawStream);
         }
     }
 
@@ -106,11 +115,35 @@ public class FileLogger implements ExecutionLogger, 
AutoCloseable {
     /**
      * Gets the underlying stream used to write message to the log file.
      *
+     * <p>A {@link PrintStream} never reports a failed write through an 
exception; it only sets an
+     * internal error flag that nothing in this plugin ever checks, so a 
failure to write a message
+     * is silently lost. Prefer {@link #getOutputStream()}, which reports a 
failed write by throwing.
+     *
      * @return The underlying stream used to write message to the log file, 
never <code>null</code>.
+     * @deprecated Use {@link #getOutputStream()} instead.
      */
     @Override
+    @Deprecated
     public PrintStream getPrintStream() {
-        return stream;
+        if (printStream == null) {
+            try {
+                printStream = new PrintStream(outputStream, false, 
StandardCharsets.UTF_8.name());
+            } catch (UnsupportedEncodingException e) {
+                // UTF-8 is a required charset on every Java platform, so this 
never happens.
+                throw new AssertionError(e);
+            }
+        }
+        return printStream;
+    }
+
+    /**
+     * Gets the underlying stream used to write messages to the log file.
+     *
+     * @return The underlying stream used to write messages to the log file, 
never <code>null</code>.
+     * @since 1.10
+     */
+    public OutputStream getOutputStream() {
+        return outputStream;
     }
 
     /**
@@ -120,23 +153,31 @@ public class FileLogger implements ExecutionLogger, 
AutoCloseable {
      * identical across operating systems.
      *
      * @param line The message to log.
+     * @throws UncheckedIOException If the line could not be written to the 
log file.
      */
     @Override
     public void consumeLine(String line) {
-        stream.print(line);
-        stream.print('\n');
-        stream.flush();
+        try {
+            outputStream.write(line.getBytes(StandardCharsets.UTF_8));
+            outputStream.write('\n');
+            outputStream.flush();
+        } catch (IOException e) {
+            throw new UncheckedIOException("Failed to write to log file " + 
file, e);
+        }
     }
 
     /**
      * Closes the underlying file stream.
+     *
+     * @throws UncheckedIOException If the log file could not be flushed or 
closed.
      */
     @Override
     public void close() {
-        if (stream != null) {
-            stream.flush();
-            stream.close();
-            stream = null;
+        try {
+            outputStream.flush();
+            outputStream.close();
+        } catch (IOException e) {
+            throw new UncheckedIOException("Failed to close log file " + file, 
e);
         }
     }
 
diff --git 
a/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java 
b/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java
index 588759f..73c3386 100644
--- 
a/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java
+++ 
b/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java
@@ -19,8 +19,12 @@
 package org.apache.maven.shared.scriptinterpreter;
 
 import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.nio.file.Path;
 
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
@@ -28,6 +32,8 @@ import org.junit.jupiter.api.io.TempDir;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
@@ -162,6 +168,38 @@ public class FileLoggerTest {
                 "line1\nline2\nline3\n".getBytes(StandardCharsets.UTF_8), 
Files.readAllBytes(outputFile.toPath()));
     }
 
+    /**
+     * A {@link java.io.PrintStream} never reports a failed write through an 
exception; it only
+     * sets an internal error flag that {@link FileLogger#consumeLine(String)} 
never checked,
+     * silently losing the message. Verifies that a write failure is now 
reported instead.
+     */
+    @Test
+    void consumeLineReportsAFailedWrite(@TempDir File tempDir) throws 
Exception {
+        File outputFile = new File(tempDir, "test.log");
+
+        try (FailingFileLogger fileLogger = new FailingFileLogger(outputFile)) 
{
+            assertThrows(UncheckedIOException.class, () -> 
fileLogger.consumeLine("Test1"));
+        }
+    }
+
+    @Test
+    void getOutputStreamReturnsTheStreamWrappedByGetPrintStream(@TempDir File 
tempDir) throws Exception {
+        File outputFile = new File(tempDir, "test.log");
+
+        try (FileLogger fileLogger = new FileLogger(outputFile)) {
+            OutputStream outputStream = fileLogger.getOutputStream();
+            outputStream.write("Test1\n".getBytes(StandardCharsets.UTF_8));
+
+            fileLogger.getPrintStream().print("Test2");
+            fileLogger.getPrintStream().print('\n');
+            fileLogger.getPrintStream().flush();
+
+            assertSame(outputStream, fileLogger.getOutputStream());
+        }
+
+        assertEquals(EXPECTED_LOG, new 
String(Files.readAllBytes(outputFile.toPath()), StandardCharsets.UTF_8));
+    }
+
     @Test
     void bareFilenameNoParent() throws Exception {
         File outputFile = new File("filelogger-204-bare.log");
@@ -181,4 +219,24 @@ public class FileLoggerTest {
             Files.deleteIfExists(outputFile.toPath());
         }
     }
+
+    /**
+     * A {@link FileLogger} whose underlying stream always fails to write, 
simulating a disk-full
+     * or permission error on the real log file.
+     */
+    private static class FailingFileLogger extends FileLogger {
+        FailingFileLogger(File outputFile) throws IOException {
+            super(outputFile);
+        }
+
+        @Override
+        protected OutputStream createOutputStream(Path outputPath) {
+            return new OutputStream() {
+                @Override
+                public void write(int b) throws IOException {
+                    throw new IOException("disk full");
+                }
+            };
+        }
+    }
 }

Reply via email to