gnodet commented on code in PR #1191:
URL: 
https://github.com/apache/maven-compiler-plugin/pull/1191#discussion_r4792731728


##########
src/main/java/org/apache/maven/plugin/compiler/IncrementalBuildHelper.java:
##########
@@ -210,6 +210,30 @@ public void 
afterRebuildExecution(IncrementalBuildHelperRequest incrementalBuild
         }
     }
 
+    /**
+     * Deletes the output files recorded as created by the previous execution, 
then records that this execution
+     * created none and read no input files. Used when the current execution 
has no inputs at all (e.g. every
+     * source root was removed), so there is nothing to recompile and thus 
nothing to diff the output directory
+     * against.
+     *
+     * @param outputDirectory the output directory of the current execution
+     * @throws MojoExecutionException if the recorded status cannot be read or 
written
+     */
+    void forgetFilesFromPreviousExecution(File outputDirectory) throws 
MojoExecutionException {
+        IncrementalBuildHelperRequest request = new 
IncrementalBuildHelperRequest().outputDirectory(outputDirectory);
+        beforeRebuildExecution(request);

Review Comment:
   Nit (optional): `forgetFilesFromPreviousExecution` doesn't validate the 
`outputDirectory` parameter. If a misconfigured project causes 
`getOutputDirectory()` to return null, the method will pass null to 
`IncrementalBuildHelperRequest.outputDirectory()`, potentially causing NPE in 
`beforeRebuildExecution` or `afterRebuildExecution`. 
   
   Fix: Add null check at method start: `if (outputDirectory == null) throw new 
IllegalArgumentException("outputDirectory must not be null")`.



##########
src/test/java/org/apache/maven/plugin/compiler/IncrementalBuildHelperTest.java:
##########
@@ -138,6 +138,29 @@ void readsStatusFilesWrittenByMavenSharedIncremental() 
throws Exception {
         assertTrue(Files.exists(outputDirectory.resolve("Kept.class")));
     }
 
+    @Test
+    void forgetFilesFromPreviousExecutionDeletesStaleOutputAndClearsStatus() 
throws Exception {
+        // MCOMPILER-443: simulate a previous execution that compiled 
Foo.java, then had its sources removed.
+        Files.createDirectories(outputDirectory);
+        Files.write(outputDirectory.resolve("Foo.class"), new byte[0]);
+        Path statusDirectory = helper.getMojoStatusDirectory().toPath();
+        Files.write(
+                
statusDirectory.resolve(IncrementalBuildHelper.CREATED_FILES_LST_FILENAME),
+                "Foo.class\n".getBytes(Charset.defaultCharset()));
+        Files.write(

Review Comment:
   Nit (optional): Test uses `Charset.defaultCharset()` which could cause 
platform-dependent test behavior. While ASCII strings like "Foo.class" work 
correctly with any charset, using the default charset makes the test fragile to 
future changes. 
   
   Fix: Use `StandardCharsets.UTF_8` for consistent, platform-independent 
behavior.



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