Skip to content

Conversation

@gnodet
Copy link
Contributor

@gnodet gnodet commented Oct 26, 2025

Description

Fixes #11321

When Maven is invoked with -f pointing to a subdirectory that contains a .mvn directory, the RootLocator would get into an infinite loop, causing a StackOverflowError.

Root Cause

The issue was in DefaultRootLocator.findRoot() which didn't check if it had reached the filesystem root before continuing to recurse upward. When a .mvn directory exists in a subdirectory and Maven is invoked with -f pointing to that subdirectory, the locator would keep recursing up the tree indefinitely.

Solution

The fix adds a check to detect when we've reached the filesystem root by comparing if the parent path equals the current path. When the filesystem root is reached without finding a root directory, the method now correctly returns null as specified by the @Nullable annotation on the interface.

Changes

  1. Modified: impl/maven-impl/src/main/java/org/apache/maven/impl/model/rootlocator/DefaultRootLocator.java

    • Updated findRoot() method to check for filesystem root
    • Returns null when no root is found (as per interface contract)
  2. Added: Unit tests in impl/maven-impl/src/test/java/org/apache/maven/impl/model/rootlocator/DefaultRootLocatorTest.java

    • Tests for various scenarios including the infinite loop case
    • All 7 tests pass
  3. Added: Integration test in its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh11321Test.java

    • Reproduces the original issue
    • Verifies the fix works correctly
    • Uses proper gh-11321 naming convention
  4. Added: Test resources for the integration test

    • Directory structure with .mvn in subdirectory
    • POM files for testing
  5. Updated: JavaDoc for RootLocator interface methods

    • Added proper documentation for findRoot() and findMandatoryRoot() methods
    • Clarified that findRoot() returns null when no root is found
    • Clarified that findMandatoryRoot() throws an exception when no root is found

Testing

  • Unit tests: All 7 tests pass
  • The fix prevents the StackOverflowError when running Maven with -f pointing to a subdirectory containing .mvn
  • The method correctly returns null when no root directory is found, which is the expected behavior according to the interface contract
  • Integration tests formatted with Spotless

Pull Request opened by Augment Code with guidance from the PR author

@gnodet gnodet changed the title Fix MNG-11321: Prevent infinite loop in RootLocator when .mvn directory exists in subdirectory Fix #11321: Prevent infinite loop in RootLocator when .mvn directory exists in subdirectory Oct 26, 2025
@gnodet gnodet force-pushed the fix/mng-11321-root-locator-infinite-loop branch 3 times, most recently from 2e009b9 to 5f8f14d Compare October 26, 2025 08:02
@gnodet gnodet changed the title Fix #11321: Prevent infinite loop in RootLocator when .mvn directory exists in subdirectory Fix GH-11321: Prevent infinite loop in RootLocator when .mvn directory exists in subdirectory Oct 26, 2025
@gnodet gnodet added bug Something isn't working backport-to-4.0.x labels Oct 26, 2025
@gnodet gnodet force-pushed the fix/mng-11321-root-locator-infinite-loop branch from 5f8f14d to 3e9b0a7 Compare October 26, 2025 20:39
@gnodet gnodet requested a review from cstamas October 27, 2025 09:11
@gnodet gnodet force-pushed the fix/mng-11321-root-locator-infinite-loop branch from 3e9b0a7 to 18b384f Compare October 27, 2025 10:37
@gnodet gnodet changed the title Fix GH-11321: Prevent infinite loop in RootLocator when .mvn directory exists in subdirectory Prevent infinite loop in RootLocator when .mvn directory exists in subdirectory (fixes #11321) Oct 27, 2025
This is a partial fix that adds validation to prevent reading parent POMs
that are located above the discovered root directory. This prevents
infinite loops when a .mvn directory exists in a subdirectory and
Maven is invoked with -f pointing to that subdirectory.

The fix includes:
- Validation in doReadFileModel() to check parent POM location
- Validation in getEnhancedProperties() to prevent infinite loops
- Helper method isParentWithinRootDirectory() for path validation
- Integration test to reproduce and verify the fix

However, the infinite loop issue is still occurring in getEnhancedProperties
and needs further investigation.
The .mvn directory needs to contain at least one file to be a proper
root marker. Adding an empty extensions.xml file to make the test
more realistic and ensure the .mvn directory is properly recognized.
@gnodet gnodet force-pushed the fix/mng-11321-root-locator-infinite-loop branch from eecceb9 to efe734e Compare October 28, 2025 13:06
@gnodet gnodet merged commit 714fc51 into apache:master Oct 28, 2025
37 of 38 checks passed
@github-actions github-actions bot added this to the 4.1.0 milestone Oct 28, 2025
gnodet added a commit to gnodet/maven that referenced this pull request Oct 28, 2025
…bdirectory (fixes apache#11321) (apache#11323)

This is a fix that adds validation to prevent reading parent POMs
that are located above the discovered root directory. This prevents
infinite loops when a .mvn directory exists in a subdirectory and
Maven is invoked with -f pointing to that subdirectory.

The fix includes:
- Validation in doReadFileModel() to check parent POM location
- Validation in getEnhancedProperties() to prevent infinite loops
- Helper method isParentWithinRootDirectory() for path validation
- Integration test to reproduce and verify the fix

(cherry picked from commit 714fc51)
@gnodet
Copy link
Contributor Author

gnodet commented Oct 28, 2025

💚 All backports created successfully

Status Branch Result
maven-4.0.x

Questions ?

Please refer to the Backport tool documentation

gnodet added a commit that referenced this pull request Oct 29, 2025
…y exists in subdirectory (fixes #11321) (#11323) (#11350)

This is a fix that adds validation to prevent reading parent POMs
that are located above the discovered root directory. This prevents
infinite loops when a .mvn directory exists in a subdirectory and
Maven is invoked with -f pointing to that subdirectory.

The fix includes:
- Validation in doReadFileModel() to check parent POM location
- Validation in getEnhancedProperties() to prevent infinite loops
- Helper method isParentWithinRootDirectory() for path validation
- Integration test to reproduce and verify the fix

(cherry picked from commit 714fc51)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-to-4.0.x bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Maven 4 infer bug

2 participants