Skip to content

[#8485] Remember if Maven model problems were encountered in Maven 4 - #12620

Open
wilx wants to merge 8 commits into
apache:masterfrom
wilx:maven-remember-problems-4x
Open

wilx wants to merge 8 commits into
apache:masterfrom
wilx:maven-remember-problems-4x

Conversation

@wilx

@wilx wilx commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

This ports the model-problem retention feature from Maven 3 PR #298 to Maven 4.

Maven 4 retains the model problems reported while discovering the reactor. The
native Session.getModelProblemCollector() exposes a
ProblemCollector with problem
details, severity counts, and overflow information. Its default implementation
uses SessionData, so sessions sharing that data, including derived sessions,
share a writable collector without depending on Maven Core's DefaultSession.

Session.getModelProblemCollector() is now abstract. Core's DefaultSession
and the standalone ApiRunner.DefaultSession each explicitly allocate a
writable collector lazily through SessionData. Sessions sharing that data
within either implementation, including derived sessions, share the collector
and preserve its configured limits, counts, and overflow information. The
SessionModelProblems helper has been removed from the API module.
No model-problem accessors or bridge are added to the legacy MavenSession API.

Problems encountered while constructing an external parent or imported BOM are
part of the owning project's model result. Derived model-builder requests
share that result's collector, preserving its limits, counts, and causes
rather than copying problems afterward.

Project-collection problems are retained from the selected discovery result
before -pl and related project trimming, matching Maven 3 behavior. Problems
from a speculative multi-module collection attempt are buffered until that
attempt is selected; if Maven falls back to the explicitly requested POM,
those unrelated problems are discarded. Later, unrelated model-building
operations remain outside the session collector's scope.

Binary compatibility is preserved by adding default methods to the native
Session and ProjectsSelector interfaces and retaining existing project
collection entry points.

The new Maven 4.1.0 Session.getModelProblemCollector() method is abstract,
so existing Session implementations must implement it; adding this method
does not preserve binary compatibility for callers invoking it on older
implementations. Basic stubs can return ProblemCollector.empty(), as
SessionStub now does.

SessionStub now retains a writable ProblemCollector.create(100) per instance.
Repeated calls return the same collector, preserving counts and retained problems;
separate stub instances remain isolated.

The ProjectsSelector default overload and existing project-collection entry
points are retained for compatibility.

The new ProjectsSelector overload delegates to the
original two-argument method by default and does not invoke the problem
consumer. Custom implementations must override the new overload to report
model problems; Maven's DefaultProjectsSelector does so.

Fixes #8485.

Verification

  • Validation of the SessionStub fix in 212e353abe, using JDK 21 and Maven 3.9.16:
    • Targeted maven-testing reactor verify passed: 27 tests, with 0 failures,
      0 errors, and 0 skipped.
    • Includes tests for retention across repeated accesses and isolation between
      stub instances.
  • Latest Earlier validation of the changes committed as 6fbc2a4fc9, using JDK 21 and Maven 3.9.16:
    • All 90 reactor modules passed clean verify with -Prun-its and
      -Dits.test=MavenITRememberModelProblemsTest,MavenITmng2199ParentVersionRangeTest.
    • Unit-test reports recorded 3,224 tests, with 0 failures, 0 errors, and
      16 skipped, including collector sharing, isolation, and overflow coverage.
    • All 11 selected Core ITs passed against the newly built distribution.
    • Checkstyle, RAT, Spotless, and Japicmp passed.
    • The packaged API declares the collector accessor abstract and contains
      no SessionModelProblems helper.
  • Latest Earlier post-rebase validation on 31dae90c23, using JDK 21:
    • 130 selected unit tests passed, covering model building, iterative parent
      traversal, project building, graph collection, and session storage.
    • 11 Core ITs passed: MavenITRememberModelProblemsTest and
      MavenITmng2199ParentVersionRangeTest.
    • The reactor verify build passed with those test selections.
  • Earlier full-suite validation, before the latest rebase:
    • mvn -e -B -V -Prun-its clean install passed all 90 reactor modules.
    • 1,049 Core ITs completed with 0 failures, 0 errors, and 43 skipped.
    • Checkstyle, RAT, Spotless, and Japicmp passed.
    • This full-suite run has not been repeated after the latest rebase.

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 Core IT successfully.

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.

@wilx
wilx force-pushed the maven-remember-problems-4x branch from 992accf to 1edef52 Compare July 29, 2026 18:15
@wilx
wilx marked this pull request as ready for review July 29, 2026 18:26

@gnodet gnodet left a comment

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.

Well-structured port of the model-problem retention feature from Maven 3 to Maven 4 with thorough test coverage, proper backward compatibility, and correct thread-safe design. Two minor observations:

  1. @since version tag: The @since 3.10.0 tag on hasModelProblems() and setModelProblems() in MavenSession.java is inconsistent with the existing @since 4.0.0 convention used elsewhere in the file. The 3.10.0 was carried over from PR #298 (which targets maven-3.10.x), but since this is the Maven 4 codebase, using @since 4.0.0 (or whatever the target Maven 4 release version is) would be more consistent.

  2. Duplicate simple class name: Two new classes named SessionModelProblems are introduced — one package-private in org.apache.maven.api (API module) and one public in org.apache.maven.internal.impl (core module). This would be the first instance in the codebase of the same simple name appearing in both api and internal.impl packages. A distinct name for the impl class (e.g., LegacySessionModelProblems or SessionModelProblemsBridge) would reduce potential confusion for future maintainers.

Neither finding is blocking — the overall design is sound: thread safety via ProblemCollector's concurrent data structures, correct enum mapping, binary compatibility via default methods, proper problem propagation through multi-module collection, and thorough test coverage across unit and integration tests.

This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

Claude Code on behalf of gnodet

@wilx

wilx commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

The @since 3.10.0 seems appropriate if the 3.x change gets accepted.

@gnodet gnodet left a comment

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.

Thanks for the quick follow-up! The rename commit cleanly resolved the duplicate class name concern — the impl class is now SessionModelProblemsBridge, and all references in MavenSession.java, DefaultSession.java, and the class file itself are updated with no stale references.

Regarding @since 3.10.0: your reasoning is sound — the tags are only on MavenSession (the 3.x legacy class), not on the Maven 4 API Session.java, so tagging with the 3.x version they first appear in is defensible.

This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

Claude Code on behalf of gnodet

@gnodet gnodet left a comment

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.

Well-structured port of model-problem retention from Maven 3 to Maven 4. The design is solid:

  • Thread-safe: AtomicBoolean for the legacy bridge flag, proper session-data scoping
  • Backward compatible: Default methods on Session interface, bidirectional MavenSession ↔ Session propagation
  • Smart buffering: MultiModuleCollectionStrategy correctly buffers speculative problems before forwarding, preventing unrelated problems from leaking into the session
  • Well-tested: Thorough coverage across unit and integration tests; 1,049 Core ITs with zero failures

The deriveWithProblemCollector approach in DefaultModelBuilder is a clean solution for sharing the collector with parent/BOM sub-builds.

Two minor observations (non-blocking):

  • The valueOf(problem.getSeverity().name()) pattern in DefaultGraphBuilder.toApiModelProblem() assumes enum constants stay in sync between legacy and API enums — safe today but worth a comment noting the coupling.
  • Session.hasModelProblems() default method only checks the collector, while DefaultSession also checks the legacy bridge flag. Standalone Session implementations would miss problems set via the legacy MavenSession.setModelProblems(true) path — acceptable since standalone impls wouldn't have a MavenSession counterpart.

This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

Claude Code on behalf of Guillaume Nodet

gnodet added a commit to gnodet/maven that referenced this pull request Aug 16, 2026

@gnodet gnodet left a comment

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.

LGTM — Well-structured port of model-problem retention from Maven 3 (PR #298) to Maven 4. The design is sound: default methods on Session for binary compatibility, SessionData-backed storage for derived-session sharing, correct problem buffering in MultiModuleCollectionStrategy, and thorough test coverage.

Minor observations (non-blocking):

  1. Enum coupling (DefaultGraphBuilder.java): The valueOf(problem.getSeverity().name()) conversion creates a coupling between legacy and API enums. This follows the existing pattern in DefaultProjectBuilder.java:182, so it's consistent, but a comment noting the coupling would help future maintainers.

  2. Interface extensibility (ProjectCollectionStrategy): The new collectProjects(request, problemConsumer) overloads were added to concrete classes but not the interface. This is harmless since DefaultGraphBuilder uses concrete types directly, but contrasts with ProjectsSelector which did get the new default method.

📋 PR Metadata

Aspect Current Suggested
Labels (none) enhancement
Milestone (none) 4.1.0

This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

Claude Code on behalf of Guillaume Nodet

@slachiewicz slachiewicz added bug Something isn't working waiting-for-feedback Waiting for 90 days until issues or pull request will be closed labels Aug 24, 2026
@slachiewicz
slachiewicz force-pushed the maven-remember-problems-4x branch from 567a1a6 to ac53c40 Compare August 24, 2026 20:27
@slachiewicz

Copy link
Copy Markdown
Member

Rebased onto master and force-pushed to this branch, since it had fallen 59 commits behind and was blocking on conflicts.

Only DefaultModelBuilderTest.java conflicted, in two places, and both were additive collisions rather than disagreements:

  • Imports: master added ModelSource, this branch added ModelProblem. Both kept.
  • Test methods: master added testMappedSourcesSupportsConcurrentUpdates and testMappedSourcesSuppressesDuplicateSources, this branch added defaultSessionModelProblemCollectorIsWritable, externalParentProblemsAreRetainedInProjectResult and importedBomProblemsAreRetainedInProjectResult, at the same point. All five kept.

Nothing in the 827-line change was reinterpreted. git range-diff against the pre-rebase commits reports the second commit identical and the first differing only in surrounding context.

Verified: mvn -pl impl/maven-impl test -Dtest=DefaultModelBuilderTest → 21 tests, no failures.

@wilx your local branch now differs from the remote. git fetch origin && git reset --hard origin/maven-remember-problems-4x picks this up; the pre-rebase tip was 567a1a6a7d if you need to compare or recover.

This comment was created with AI assistance.

@github-actions github-actions Bot removed the waiting-for-feedback Waiting for 90 days until issues or pull request will be closed label Aug 24, 2026

@gnodet gnodet left a comment

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.

Re-review after rebase. Clean rebase onto master after 59-commit drift — no substantive code changes from the previously approved content. Only DefaultModelBuilderTest.java conflicted, and the resolution was purely additive (both sides' new tests kept). Verified correct.

📋 PR Metadata

Aspect Current Suggested
Milestone (none) 4.1.0

This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

Claude Code on behalf of Guillaume Nodet

@slachiewicz slachiewicz added this to the 4.1.0 milestone Aug 24, 2026
@wilx

wilx commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto master and force-pushed to this branch, since it had fallen 59 commits behind and was blocking on conflicts.

👍

@gnodet-bot gnodet-bot left a comment

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.

Re-review after rebase (31dae90). The core design is sound — SessionData-backed storage, shared across derived sessions, correct deriveWithProblemCollector call sites in DefaultModelBuilder, and good test coverage including the IT. Two findings.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

default List<MavenProject> selectProjects(
List<File> files, MavenExecutionRequest request, Consumer<ModelProblem> problemConsumer)
throws ProjectBuildingException {
return selectProjects(files, request);

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.

⚠️ Silent problem drop for third-party ProjectsSelector implementations.

The default body calls selectProjects(files, request) — the old 2-arg overload — which discards problemConsumer entirely. Any ProjectsSelector implementation that extends the interface but does not override the new 3-arg method will silently swallow all model problems rather than routing them to the session collector.

The Javadoc on this method says "report model problems encountered while building them", which the default implementation does not do. That's a misleading contract.

In practice, Maven's own impls (DefaultProjectsSelector) all override the method, so the issue is latent today. But the interface is an SPI: embedders and plugins that provide custom ProjectsSelector implementations won't get problems retained without overriding the new method — and they'll get no warning that they're silently discarding them.

Consider adding a @apiNote Javadoc warning:

Suggested change
return selectProjects(files, request);
// NOTE: Implementations that override only the 2-arg selectProjects() will silently
// discard problemConsumer. Override this method to route problems to the caller.
return selectProjects(files, request);

Or, if the concern is real enough, mark the old 2-arg method @Deprecated to force implementors to the new overload.

@gnodet-bot

Copy link
Copy Markdown
Contributor

📝 Stale PR description — the last commit (31dae90) removed the MavenSession#hasModelProblems() / setModelProblems(boolean) legacy bridge methods and SessionModelProblemsBridge entirely, but the PR description still says:

"the legacy MavenSession#hasModelProblems() and setModelProblems(boolean) methods bridge to the same session state"

That code is gone. The description should be updated to reflect the current design (native Session.getModelProblemCollector() only, no legacy bridge).

This comment was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet-bot gnodet-bot left a comment

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.

Re-review after rebase (561733d). Single new commit — "Document ProjectsSelector model problem reporting fallback" — directly addresses the previously raised finding.

The Javadoc on ProjectsSelector.selectProjects(List, MavenExecutionRequest, Consumer) now explicitly states:

"The default implementation delegates to selectProjects(List, MavenExecutionRequest) for compatibility with existing implementations and does not invoke problemConsumer. Implementations must override this method to report model problems to the consumer."

That resolves the misleading contract concern: the silent-drop behavior is now documented, and implementors overriding only the 2-arg method will see from the Javadoc that they need to override the 3-arg overload to opt into problem reporting.

Everything else was approved on SHA 31dae90. LGTM.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet gnodet left a comment

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.

The default implementation on Session.getModelProblemCollector() forces SessionModelProblems into the org.apache.maven.api package, which is wrong — it's an implementation detail (a SessionData key holder) with no business being in the API module.

Suggested change: make the method abstract, remove the default, and delete SessionModelProblems.java from maven-api-core.

// Session.java
@Nonnull
ProblemCollector<ModelProblem> getModelProblemCollector();

The implementation belongs in DefaultSession in impl/maven-core, where it can freely use SessionData to lazily allocate the collector. Existing Session implementations that don't override it will get a compile error, but since this is a new method in 4.1.0, that's expected — and they can trivially return ProblemCollector.empty() as a stub.

The default+helper-in-API-package pattern is only justified when the default behavior needs to be non-trivial and cannot live in impl. Here it doesn't apply: the default is just lazy SessionData allocation, which is precisely what DefaultSession should do explicitly.

@wilx

wilx commented Sep 21, 2026

Copy link
Copy Markdown
Contributor Author

@gnodet I have pushed a change to implement your design suggestion.

@gnodet-bot gnodet-bot left a comment

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.

Re-review after "Move model problem collector storage out of the API" (6fbc2a4).

The final commit directly addresses @gnodet's CHANGES_REQUESTED: getModelProblemCollector() is now abstract in Session.java, and the ModelProblems record + MODEL_PROBLEMS key moved out of maven-api-core into DefaultSession and ApiRunner.DefaultSession. The API module no longer holds implementation details. That design concern is fully resolved.

Two findings remain.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet-bot gnodet-bot left a comment

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.

Re-review after "Retain a writable model problem collector in SessionStub" (212e353).

The new commit directly addresses the third finding from the prior review: SessionStub.getModelProblemCollector() now returns ProblemCollector.create(100) — a real per-instance collector — instead of ProblemCollector.empty() which threw IllegalStateException on any reportProblem() call. The fix is correct:

  • Each SessionStub instance gets its own isolated collector, matching DefaultSession and ApiRunner.DefaultSession semantics.
  • The hardcoded limit of 100 matches the production default when MAVEN_BUILDER_MAX_PROBLEMS is unset — acceptable for a test stub where getData() returns null and the session-property lookup path is unavailable.
  • SessionStubTest covers retention across accesses (assertSame on the collector reference) and isolation between instances (assertNotSame), which are exactly the two properties that matter.

Regarding the prior finding about a missing super(versionRange) constructor in MavenITRememberModelProblemsTest: that was a false positive. On this branch, AbstractMavenIntegrationTestCase has a no-arg constructor at line 44 — the bot was inspecting the master version of that file, which only has parameterized constructors. The test class is valid.

All three findings from the previous review are now resolved. The overall design is sound: abstract getModelProblemCollector() on Session, SessionData-backed lazy allocation in DefaultSession and ApiRunner.DefaultSession, correct buffering of speculative problems in MultiModuleCollectionStrategy, and comprehensive coverage across unit and integration tests.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@wilx

wilx commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@gnodet I think I did all of the requested changes. Or am I missing some?

@gnodet-bot gnodet-bot left a comment

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.

Re-review (explicit request). The design is solid and all previous concerns have been addressed.

Interaction with #12702: After this merges, DefaultProjectsSelector in #12702 will conflict and should be simplified — instead of injecting DefaultDiagnosticCollector into the constructor and looping manually, it should use the Consumer<ModelProblem> overload introduced here, passing p -> diagnosticCollector.report(toBuilderProblem(p)). The DefaultGraphBuilderTest will also need a manual conflict resolution.

Backport to 4.0.x: Not recommended. While Session is @Experimental on 4.0.x (no API compat concern), the DefaultModelBuilder.deriveWithProblemCollector() method added here depends on internal structure that diverged significantly from 4.0.x after the [MNG-6563] iterative parent traversal refactor (which is not in 4.0.x). A straight cherry-pick would not apply cleanly and adapting it carries regression risk in parent/BOM problem collector sharing. The gap in 4.0.x is acceptable given it is approaching end-of-life.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet gnodet changed the title [MNG-6797] Remember if Maven model problems were encountered in Maven 4 [#8485] Remember if Maven model problems were encountered in Maven 4 Nov 9, 2026
wilx added 8 commits November 22, 2026 16:13
Retain reactor-discovery model problems in the Maven 4 session and expose the state through both the native and legacy session APIs. Preserve collection-strategy compatibility and ignore problems from discarded discovery attempts.

Fixes apache#8485.
Explain that the default overload preserves existing implementations without invoking the problem consumer, and that implementations must override it to report model problems.
Make Session.getModelProblemCollector abstract and keep lazy SessionData-backed storage in the Core and standalone session implementations. Remove the API key holder and provide an empty collector in SessionStub.

Cover standalone sharing and overflow, and isolation between independent sessions. Validated with clean reactor verify and the selected model-problem and parent-version-range Core ITs.
Allocate one collector per stub instance so reporting succeeds and repeated access preserves problem details and counters. Keep independent stub instances isolated.

Add regression tests for reporting, repeated access, and instance isolation. All 27 maven-testing tests and the targeted reactor verify passed.
@wilx
wilx force-pushed the maven-remember-problems-4x branch from 212e353 to 0f52332 Compare November 22, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working mvn4

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MNG-6797] Remember in MavenSession that there were problems detected when building Maven model

4 participants