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.
| <compileSourceRoot>${basedir}/target/test-classes/unit/compiler-empty-source-change-detection-test/src/main/java</compileSourceRoot> | ||
| </compileSourceRoots> | ||
| <compilerId>javac</compilerId> | ||
| <release>17</release> |
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.