Skip to content

Fix AddDependency onlyIfUsing ignored in declarative recipes#6822

Closed
salockhart wants to merge 1 commit intoopenrewrite:mainfrom
salockhart:fix-declarative-recipe-scanning-accumulator
Closed

Fix AddDependency onlyIfUsing ignored in declarative recipes#6822
salockhart wants to merge 1 commit intoopenrewrite:mainfrom
salockhart:fix-declarative-recipe-scanning-accumulator

Conversation

@salockhart
Copy link

@salockhart salockhart commented Feb 25, 2026

Closes #6821

What's changed?

When scanning for nested recipes, we now only look at preconditions, not all recipes. scanNestedScanningRecipes() was recursively processing all recipes in getRecipeList() during precondition scanning, causing ScanningRecipes to be invoked twice with different accumulators.

What's your motivation?

Only recursively scan preconditions, not the main recipe list. This ensures each ScanningRecipe uses the same accumulator for both getScanner() and getVisitor() calls.

Anything in particular you'd like reviewers to focus on?

The change is fairly small, I just want to make sure it doesn't have any other side effects I'm not aware of.

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

When a ScanningRecipe like AddDependency was used inside a declarative
YAML recipe, scanNestedScanningRecipes() recursed into getRecipeList()
of all recipes — including leaf ScanningRecipes. This caused their
accumulators to be populated during precondition scanning rather than
during normal recipe execution, breaking onlyIfUsing filtering.

Fix: Distinguish DeclarativeRecipe containers (recurse into raw
preconditions and recipeList fields) from leaf ScanningRecipes (skip
recursion — let the framework handle their scanning during execution).
Raw fields are used instead of getRecipeList() to avoid PreconditionBellwether wrapping.

Tests: Added addDependencyOnlyIfUsingInDeclarativeRecipe() in both
rewrite-maven and rewrite-gradle with two projects — one using Guava
(dependency added) and one not (dependency left unchanged).

Fixes #6821
@salockhart salockhart force-pushed the fix-declarative-recipe-scanning-accumulator branch from 9d5aded to 8532fab Compare February 27, 2026 17:50
@salockhart
Copy link
Author

I think this may have been solved (better) in #6834 - once there's a release with that in it, I'll verify if it solves my issue and close this if so.

@timtebeek
Copy link
Member

Thanks, that's landed in: c32d382

If you locally install the Maven or gradle plugin you should be able to verify.

@salockhart salockhart closed this Mar 3, 2026
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Mar 3, 2026
@salockhart salockhart deleted the fix-declarative-recipe-scanning-accumulator branch March 3, 2026 13:43
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.

onlyIfUsing not working properly in org.openrewrite.gradle.AddDependency

2 participants