Fix incremental detection of empty sources, 3.x - #1075
Conversation
The stale source scanner assumes every Java source produces a mapped output, causing zero-byte compilation units to trigger recompilation on every invocation. Filter zero-byte stale candidates only for one-output-per-input compilers and only when no mapped output exists. This preserves detection when an existing source is truncated and leaves aggregate-output compilers unchanged. Add a regression test that compiles an empty source twice and verifies that the second invocation is up to date. Fixes apache#1000.
| Files.createDirectories(source.getParentFile().toPath()); | ||
| Files.write(source.toPath(), new byte[0]); | ||
|
|
||
| Log log = mock(Log.class); |
There was a problem hiding this comment.
Testing vs log messages is fragile. Any other way to verify this?
There was a problem hiding this comment.
The same way of verification is used in two other places in the test. It is good enough.
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Fixes incremental compilation change detection so that truly empty Java sources (0 bytes) don’t cause perpetual recompilation when no mapped output exists, while preserving detection for truncated sources that previously produced outputs.
Changes:
- Filter stale-source candidates for
ONE_OUTPUT_FILE_PER_INPUT_FILEcompilers to ignore 0-byte sources with no mapped output. - Add a regression test that compiles an empty source twice and asserts the second run reports “up to date”.
- Add a dedicated test plugin configuration for the new empty-source change-detection scenario.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java | Filters stale sources to avoid recompile loops for empty sources without mapped outputs. |
| src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTest.java | Adds regression test ensuring second compilation is up-to-date for empty source. |
| src/test/resources/unit/compiler-empty-source-change-detection-test/plugin-config.xml | Provides test-specific compiler plugin configuration and directories. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for (File source : includedSources) { | ||
| String relativePath = | ||
| rootFile.toPath().relativize(source.toPath()).toString(); | ||
| boolean outputExists = mapping.getTargetFiles(outputDirectory, relativePath).stream() | ||
| .anyMatch(File::exists); | ||
| // A zero-byte compilation unit legitimately produces no class. Keep it stale if an output | ||
| // exists, however, so that truncating an existing source is still detected as a change. | ||
| if (Files.size(source.toPath()) != 0 || outputExists) { | ||
| staleSources.add(source); | ||
| } | ||
| } |
| String relativePath = | ||
| rootFile.toPath().relativize(source.toPath()).toString(); | ||
| boolean outputExists = mapping.getTargetFiles(outputDirectory, relativePath).stream() |
There was a problem hiding this comment.
The string is passed to the getTargetFiles method which uses it to construct File instance. OS specific separators are appropriate.
There was a problem hiding this comment.
While java.io.File (and Java's NIO layer on Windows) can parse backslashes, Maven plugins like maven-war-plugin or maven-ear-plugin do not just write files to disk—they map them into archives (ZIP, JAR, WAR, EAR).
- Archive Entry Standards: ZIP and JAR specifications mandate that entry names use forward slashes (
/) as path separators, regardless of the host operating system. - Map/Cache Lookups: If target paths are stored as keys in a
MaporSet(e.g., to track packaged or copied files, check for staleness, or prevent duplicates), a path string containing\on Windows will not match an entry lookup using/. This leads to cross-platform bugs where builds behave differently on Windows versus Unix. - Java's
FileFlexibility: Ironically, Java'sFileconstructor on Windows accepts forward slashes (/) natively and normalizes them internally. Therefore, forcing platform-specific separators viaPath#toString()introduces risks for map keys and archive matching without providing any functional benefit to theFileconstructor itself.
The finding is valid; relying on Path#toString() for target mappings in archive-building plugins frequently causes Windows-specific regressions.
There was a problem hiding this comment.
While
java.io.File(and Java's NIO layer on Windows) can parse backslashes, Maven plugins likemaven-war-pluginormaven-ear-plugindo not just write files to disk—they map them into archives (ZIP, JAR, WAR, EAR).1. **Archive Entry Standards:** ZIP and JAR specifications mandate that entry names use forward slashes (`/`) as path separators, regardless of the host operating system. 2. **Map/Cache Lookups:** If target paths are stored as keys in a `Map` or `Set` (e.g., to track packaged or copied files, check for staleness, or prevent duplicates), a path string containing `\` on Windows will not match an entry lookup using `/`. This leads to cross-platform bugs where builds behave differently on Windows versus Unix. 3. **Java's `File` Flexibility:** Ironically, Java's `File` constructor on Windows accepts forward slashes (`/`) natively and normalizes them internally. Therefore, forcing platform-specific separators via `Path#toString()` introduces risks for map keys and archive matching without providing any functional benefit to the `File` constructor itself.The finding is valid; relying on
Path#toString()for target mappings in archive-building plugins frequently causes Windows-specific regressions.
I am sorry, but this is irrelevant in the face of what the org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping implementations actually do. The AI is wrong.
| <compileSourceRoot>${basedir}/target/test-classes/unit/compiler-empty-source-change-detection-test/src/main/java</compileSourceRoot> | ||
| </compileSourceRoots> | ||
| <compilerId>javac</compilerId> | ||
| <release>17</release> |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (4)
src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTest.java:132
- Using a bare Mockito mock for
Logcan preventinfo()from ever being called if the production code checkslog.isInfoEnabled()(default boolean return from mocks isfalse). To make this test reliable, stubwhen(log.isInfoEnabled()).thenReturn(true)(and similarly for other levels if used), or use a realLogimplementation suitable for tests.
Log log = mock(Log.class);
compilerMojo.setLog(log);
compilerMojo.execute();
src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTest.java:136
- This assertion is tightly coupled to the exact log message text, which makes the test brittle to harmless wording changes. Consider verifying with a matcher (e.g.,
startsWith/contains) or asserting the underlying behavior (e.g., no compilation triggered / no sources considered stale) if you can observe it from the test harness.
verify(log).info("Nothing to compile - all classes are up to date.");
src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java:1569
- Using
Files.size(...)introduces checked-IO handling (and an extra filesystem call) for a simple empty-file check. Sincesourceis aFile,source.length() != 0is typically sufficient here and would let you avoidIOException-driven control flow in this block.
for (File source : includedSources) {
if (Files.size(source.toPath()) != 0) {
staleSources.add(source);
} else {
src/test/resources/unit/compiler-empty-source-change-detection-test/plugin-config.xml:24
- This file is used as the
pominput to@InjectMojo, but it lacks typical required Maven POM coordinates (e.g.,modelVersion,groupId,artifactId,version). If the test harness parses it as a MavenModel, missing coordinates can cause parsing/validation issues or environment-dependent defaults. Consider making it a minimal valid POM by adding those elements to ensure deterministic test setup.
<project>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
The stale source scanner assumes every Java source produces a mapped output, causing zero-byte compilation units to trigger recompilation on every invocation.
This change filters zero-byte stale candidates only for one-output-per-input compilers and only when no mapped output exists. This preserves detection when an existing source is truncated while leaving aggregate-output compilers unchanged.
A regression test compiles an empty source twice and verifies that the second invocation reports all classes as up to date.
Fixes #1000.
Following this checklist to help us incorporate your
contribution quickly and easily:
Note that commits might be squashed by a maintainer on merge.
This may not always be possible but is a best-practice.
mvn verifyto make sure basic checks pass.A more thorough check will be performed on your pull request automatically.
mvn -Prun-its verify).If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.
To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.