From 79ba451d2a0c75efd67e8bd4793c197c901ebe58 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Thu, 22 Oct 2026 19:16:50 +0200 Subject: [PATCH] Treat a missing dependency directory as empty instead of an I/O error DependencyState.modificationState() branched on Files.isDirectory(), which is also false for a path that does not exist at all. A reactor module with no main sources never creates target/classes, but that path is still on the test classpath, so testCompile tried to read it as a single file, got NoSuchFileException, and logged a spurious "I/O error reading dependency state" warning on every build (#1124, a regression introduced by #1102 in 3.16.0). Checking Files.notExists() first and treating a missing path like an empty directory removes the warning and keeps the recorded state stable across builds instead of recording "unreadable" from a caught exception. Claude-Session: https://claude.ai/code/session_01WUpJGfMpe1TZMx18K9zF4M --- .../plugin/compiler/DependencyState.java | 9 ++++++ .../plugin/compiler/DependencyStateTest.java | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/main/java/org/apache/maven/plugin/compiler/DependencyState.java b/src/main/java/org/apache/maven/plugin/compiler/DependencyState.java index 238d7ebe4..a87b539a3 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/DependencyState.java +++ b/src/main/java/org/apache/maven/plugin/compiler/DependencyState.java @@ -137,6 +137,15 @@ private static Path addDependencies( * The directory digest covers each relevant file's relative path, size and modification time. */ private static ModificationState modificationState(Path dependency, ScanContext context) { + if (Files.notExists(dependency)) { + // A reactor module with no sources for this output kind (e.g. no main sources, so + // `target/classes` is never created) still appears on the classpath. Treat it like an + // empty directory instead of letting `readAttributes` below fail with a spurious + // "I/O error reading dependency state" warning and an "unreadable" state that never + // compares equal across builds, which defeated incremental compilation (forced a + // recompile on every run). + return new ModificationState(0 + ":" + toHexString(newDigest().digest()), null); + } if (!Files.isDirectory(dependency)) { try { BasicFileAttributes attributes = readAttributes(dependency); diff --git a/src/test/java/org/apache/maven/plugin/compiler/DependencyStateTest.java b/src/test/java/org/apache/maven/plugin/compiler/DependencyStateTest.java index b8c60581f..b4eebaa44 100644 --- a/src/test/java/org/apache/maven/plugin/compiler/DependencyStateTest.java +++ b/src/test/java/org/apache/maven/plugin/compiler/DependencyStateTest.java @@ -34,7 +34,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.contains; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class DependencyStateTest { @@ -113,6 +116,33 @@ void preservesRepeatedDependencyEntriesAndPathKinds() throws Exception { assertTrue(state.get(2).startsWith("modulepath:")); } + @Test + void treatsMissingDependencyDirectoryAsEmptyWithoutWarningOrForcedRebuild() throws Exception { + Path dependency = temporaryDirectory.resolve("classes-not-yet-compiled"); + IncrementalBuildHelper helper = mock(IncrementalBuildHelper.class); + when(helper.getMojoStatusDirectory()).thenReturn(temporaryDirectory.toFile()); + Log log = mock(Log.class); + + DependencyState.Configuration configuration = + new DependencyState.Configuration(Collections.emptySet(), BUILD_START, 0, log, false); + List classpath = Collections.singletonList(dependency.toString()); + + assertFalse(DependencyState.hasChanged( + helper, + temporaryDirectory.resolve("output").toFile(), + classpath, + Collections.emptyList(), + configuration)); + assertFalse(DependencyState.hasChanged( + helper, + temporaryDirectory.resolve("output").toFile(), + classpath, + Collections.emptyList(), + configuration)); + + verify(log, never()).warn(contains("I/O error")); + } + @Test void retainsCurrentBuildDetectionWhenStatusDirectoryIsUnavailable() throws Exception { Path dependency = dependency("dependency.jar", 3_000, 1);