What version of OpenRewrite are you using?
I am using
How are you running OpenRewrite?
I am using OpenRewrite in a single-module library repository.
What is the smallest, simplest way to reproduce the problem?
build.gradle
plugins {
id 'java'
}
group = 'com.example'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
gradlePluginPortal()
}
dependencies {
implementation(platform('org.openrewrite.recipe:rewrite-recipe-bom:3.24.0'))
implementation 'org.openrewrite:rewrite-gradle'
implementation 'com.fasterxml.jackson.core:jackson-annotations'
compileOnly 'org.projectlombok:lombok:1.18.30'
annotationProcessor 'org.projectlombok:lombok:1.18.30'
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
testImplementation 'org.openrewrite:rewrite-test'
testImplementation 'org.openrewrite:rewrite-java-17'
testImplementation 'org.openrewrite:rewrite-groovy'
testImplementation 'org.openrewrite.gradle.tooling:model'
testImplementation 'com.google.guava:guava:32.1.3-jre'
testImplementation 'org.gradle:gradle-tooling-api:7.3-20210825160000+0000'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
test {
useJUnitPlatform()
}
recipes.yaml
---
type: specs.openrewrite.org/v1beta/recipe
name: com.example.AddGuavaIfUsed
displayName: Add Guava dependency if used
description: Adds Guava dependency when com.google.common.collect types are used.
recipeList:
- org.openrewrite.gradle.AddDependency:
groupId: com.google.guava
artifactId: guava
version: 32.1.3-jre
configuration: implementation
onlyIfUsing: com.google.common.collect.*
AddDependencyOnlyIfUsingBugTest.java
package com.example;
import org.junit.jupiter.api.Test;
import org.openrewrite.gradle.GradleParser;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import java.io.InputStream;
import java.util.Objects;
import static org.openrewrite.gradle.Assertions.buildGradle;
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.mavenProject;
import static org.openrewrite.java.Assertions.srcMainJava;
/**
* Reproduces bug where AddDependency's onlyIfUsing parameter is ignored
* when the recipe is nested inside a declarative recipe.
*
* With 3.23.0 (working): onlyIfUsing works correctly
* With 3.24.0 (broken): onlyIfUsing is ignored, dependency always added
*/
class AddDependencyOnlyIfUsingBugTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
// Load the declarative recipe from YAML
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try (InputStream inputStream = classLoader.getResourceAsStream("META-INF/rewrite/recipes.yaml")) {
spec.beforeRecipe(withToolingApi())
.recipe(Objects.requireNonNull(inputStream), "com.example.AddGuavaIfUsed")
.parser(JavaParser.fromJavaVersion().classpath("guava"))
.parser(GradleParser.builder());
} catch (Exception e) {
throw new RuntimeException("Failed to load recipe", e);
}
}
@Test
void demonstratesBug_DependencyIncorrectlyAddedWhenTypeNotUsed() {
rewriteRun(
mavenProject(
"project",
srcMainJava(
java(
"""
package com.example;
public class Example {
public void doesNotUseGuava() {
System.out.println("No Guava here");
}
}
"""
)
),
// 3.23.0 (working): Build file unchanged - Guava NOT added ✅
// 3.24.0 (BUG!): Guava dependency incorrectly added even though not used ❌
//
// This test PASSES with 3.23.0 (no dependency added)
// This test FAILS with 3.24.0 (dependency incorrectly added)
buildGradle(
"""
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework:spring-core:5.3.20'
}
"""
)
)
);
}
}
I have a minimal reproduction here:
openrewrite-bug-repro.zip
The project is currently using v3.24.0 of the BOM, and the tests should fail with ./gradlew clean test. Updating the build.gradle to v3.23.0 and re-running the tests should show them passing.
What did you expect to see?
See above snippet. The important bit is what we expect not to see - the resulting build.gradle in the test should not contain the Guava dependency.
What did you see instead?
See above snippet. The dependency is being added, regardless of the onlyIfUsed property.
What is the full stack trace of any errors you encountered?
No stack trace present.
I am open to contributing a fix, if I can be pointed in the right direction!
What version of OpenRewrite are you using?
I am using
How are you running OpenRewrite?
I am using OpenRewrite in a single-module library repository.
What is the smallest, simplest way to reproduce the problem?
build.gradle
recipes.yaml
AddDependencyOnlyIfUsingBugTest.java
I have a minimal reproduction here:
openrewrite-bug-repro.zip
The project is currently using v3.24.0 of the BOM, and the tests should fail with
./gradlew clean test. Updating thebuild.gradleto v3.23.0 and re-running the tests should show them passing.What did you expect to see?
See above snippet. The important bit is what we expect not to see - the resulting
build.gradlein the test should not contain the Guava dependency.What did you see instead?
See above snippet. The dependency is being added, regardless of the
onlyIfUsedproperty.What is the full stack trace of any errors you encountered?
No stack trace present.
Are you interested in contributing a fix to OpenRewrite?
I am open to contributing a fix, if I can be pointed in the right direction!