Skip to content

Fix incremental detection of empty sources, 3.x - #1075

Open
wilx wants to merge 3 commits into
apache:maven-compiler-plugin-3.xfrom
wilx:issue-1000-empty-source-3.x
Open

Fix incremental detection of empty sources, 3.x#1075
wilx wants to merge 3 commits into
apache:maven-compiler-plugin-3.xfrom
wilx:issue-1000-empty-source-3.x

Conversation

@wilx

@wilx wilx commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

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:

  • Your pull request should address just one issue, without pulling in other changes.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body.
    Note that commits might be squashed by a maintainer on merge.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
    This may not always be possible but is a best-practice.
  • Run mvn verify to make sure basic checks pass.
    A more thorough check will be performed on your pull request automatically.
  • You have run the integration tests successfully (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.

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.
@wilx wilx changed the title Fix incremental detection of empty sources Fix incremental detection of empty sources, 3.x Jul 12, 2026
@elharo
elharo requested a review from Copilot July 28, 2026 11:47
Files.createDirectories(source.getParentFile().toPath());
Files.write(source.toPath(), new byte[0]);

Log log = mock(Log.class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing vs log messages is fragile. Any other way to verify this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same way of verification is used in two other places in the test. It is good enough.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_FILE compilers 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.

Comment on lines +1566 to +1576
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);
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment on lines +1567 to +1569
String relativePath =
rootFile.toPath().relativize(source.toPath()).toString();
boolean outputExists = mapping.getTargetFiles(outputDirectory, relativePath).stream()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants