Skip to content

Match requested dependencies in ModuleHasDependency and RepositoryHasDependency#179

Merged
Jenson3210 merged 1 commit into
mainfrom
Jenson3210/match-requested-dependencies
May 28, 2026
Merged

Match requested dependencies in ModuleHasDependency and RepositoryHasDependency#179
Jenson3210 merged 1 commit into
mainfrom
Jenson3210/match-requested-dependencies

Conversation

@Jenson3210
Copy link
Copy Markdown
Contributor

Summary

  • After Speed up ModuleHasDependency and RepositoryHasDependency by removing DependencyInsight #176 removed DependencyInsight, ModuleHasDependency and RepositoryHasDependency only inspect resolved dependencies. A dependency that is declared but never resolved (unit tests without a repositories block, partially resolved POMs) is now invisible to these recipes.
  • This PR adds a fallback that also matches against requested (declared) dependencies for both recipes, on both the Maven and Gradle paths. The fallback runs only after the resolved-deps pass fails, so behavior is unchanged when resolution succeeds.
  • This mirrors the existing behavior of FindDependency for both Maven (walks the pom XML tags) and Gradle (uses GradleDependency.Matcher against the build script AST) — both already match declared dependencies, not the resolved tree. Bringing ModuleHasDependency / RepositoryHasDependency in line keeps the "find" family of recipes consistent and unblocks a 1-on-1 refactor that swaps one for the other in cleaner recipes.

Version glob handling on the requested-deps path:

  • requested version is null → accept (no constraint declared).
  • requested version starts with ${ → skip (placeholder; cannot evaluate).
  • otherwise → compare against the configured version pattern.

Scope filtering is applied to Maven requested deps (default compile when null); Gradle does not currently apply scope filtering on the resolved path either, so the requested path mirrors that.

Test plan

  • gradleMatchesOnRequested — Gradle build with no repositories block; declared spring-beans matched via requested fallback.
  • mavenMatchesOnRequested — pom with no <repositories>, pinned empty MavenSettings (same trick as Pin empty MavenSettings in DependencyResolutionDiagnosticTest.maven() #178) so resolution actually fails; declared spring-beans matched via requested fallback.
  • gradleVersionRangeOnRequestedDoesNotMatchWhenOutOfRange — version glob is still enforced on the requested-deps path.
  • Full ModuleHasDependencyTest + RepositoryHasDependencyTest suites still pass.

…Dependency

After #176 removed DependencyInsight, both recipes only inspect resolved
dependencies. Add a fallback that also matches against requested (declared)
dependencies, so a dep that is declared but unresolved (e.g. unit tests with no
repositories block, partially resolved POMs) is still recognized.

Mirrors the existing behavior of `FindDependency` (both Maven and Gradle), which
matches on what is declared in the build file rather than on the resolved tree.
This keeps the two families of "find" recipes consistent and unblocks a 1-on-1
refactor that swaps one for the other in cleaner recipes.

Version glob handling: null version is accepted (no constraint declared);
`${...}` placeholders are skipped (cannot be evaluated); otherwise compared
against the version pattern.
Copy link
Copy Markdown
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Thanks a lot!

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite May 28, 2026
@Jenson3210 Jenson3210 merged commit ad8dbec into main May 28, 2026
1 check passed
@Jenson3210 Jenson3210 deleted the Jenson3210/match-requested-dependencies branch May 28, 2026 16:47
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite May 28, 2026
timtebeek added a commit that referenced this pull request May 29, 2026
* Trust resolved version over declared dep in dependency search

PR #179 extended ModuleHasDependency / RepositoryHasDependency to also
match against requested/declared dependencies so they still match when
resolution fails. This introduced false positives whenever a version
range is supplied: the declared version string could satisfy the
comparator even when the resolved version did not (e.g. a Gradle
resolutionStrategy.force or platform alignment overriding the declared
coordinate).

- When a coordinate is present in the resolved dependencies, trust the
  resolved check and skip the declared-dependency fallback for that
  group:artifact. Only fall back for declared deps not already resolved.
- Tighten versionMatches so a null declared version no longer
  auto-matches when a version constraint is supplied (same treatment as
  a ${...} property reference).

Adds regression tests covering both rules in ModuleHasDependencyTest and
RepositoryHasDependencyTest.

* Apply suggestions from code review

Co-authored-by: Jente Sondervorst <jentesondervorst@gmail.com>

* Add Maven regression tests for BOM-managed dependency version range

---------

Co-authored-by: Jente Sondervorst <jentesondervorst@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants