Skip to content

Commit c32d382

Browse files
authored
Fix AddDependency bypassing onlyIfUsing with explicit config (#6834)
* Fix AddDependency bypassing onlyIfUsing check when configuration is explicit When AddDependency is configured with both an explicit configuration (e.g., testRuntimeOnly) and onlyIfUsing (e.g., org.junit.jupiter.api.Test), the per-project type usage check is bypassed. This causes dependencies to be added to all build.gradle files, including buildSrc/build.gradle, rather than only projects that actually use the specified type. Split the single guard condition into two separate checks: always check per-project type usage when onlyIfUsing is set, and only check configurationsByProject when configuration needs to be inferred. Add test to verify the fix. Fixes #6833 * Pull up the second check on `onlyIfUsing` into the larger `if`
1 parent 82d6e55 commit c32d382

2 files changed

Lines changed: 77 additions & 5 deletions

File tree

rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependency.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,13 @@ public TreeVisitor<?, ExecutionContext> getVisitor(Scanned acc) {
202202
if (!maybeGp.isPresent()) {
203203
return s;
204204
}
205-
// When configuration needs to be inferred and onlyIfUsing is set, require JavaProject and source set info
206-
if (!hasExplicitConfiguration && onlyIfUsing != null) {
207-
if (!maybeJp.isPresent() ||
208-
!acc.usingType.getOrDefault(maybeJp.get(), false) ||
209-
!acc.configurationsByProject.containsKey(maybeJp.get())) {
205+
if (onlyIfUsing != null) {
206+
// When onlyIfUsing is set, skip projects that don't use the specified type
207+
if (!maybeJp.isPresent() || !acc.usingType.getOrDefault(maybeJp.get(), false)) {
208+
return s;
209+
}
210+
// When configuration needs to be inferred, also require source set info
211+
if (!hasExplicitConfiguration && !acc.configurationsByProject.containsKey(maybeJp.get())) {
210212
return s;
211213
}
212214
}

rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,76 @@ void addDependencyToProjectsThatNeedIt() {
11611161
);
11621162
}
11631163

1164+
@Test
1165+
@Issue("https://github.com/openrewrite/rewrite/issues/6833")
1166+
void doesNotAddToProjectNotUsingTypeWhenConfigurationIsExplicit() {
1167+
rewriteRun(
1168+
spec -> spec.recipe(addDependency("com.google.guava:guava:29.0-jre", "com.google.common.math.IntMath", "implementation")),
1169+
mavenProject("root",
1170+
buildGradle(
1171+
""
1172+
),
1173+
settingsGradle(
1174+
"""
1175+
include "project1"
1176+
include "project2"
1177+
"""
1178+
),
1179+
mavenProject("project1",
1180+
srcMainJava(
1181+
java(usingGuavaIntMath)
1182+
),
1183+
buildGradle(
1184+
"""
1185+
plugins {
1186+
id 'java-library'
1187+
}
1188+
1189+
repositories {
1190+
mavenCentral()
1191+
}
1192+
""",
1193+
"""
1194+
plugins {
1195+
id 'java-library'
1196+
}
1197+
1198+
repositories {
1199+
mavenCentral()
1200+
}
1201+
1202+
dependencies {
1203+
implementation "com.google.guava:guava:29.0-jre"
1204+
}
1205+
"""
1206+
)
1207+
),
1208+
mavenProject("project2",
1209+
srcMainJava(
1210+
java(
1211+
"""
1212+
public class B {
1213+
String hello() { return "world"; }
1214+
}
1215+
"""
1216+
)
1217+
),
1218+
buildGradle(
1219+
"""
1220+
plugins {
1221+
id 'java-library'
1222+
}
1223+
1224+
repositories {
1225+
mavenCentral()
1226+
}
1227+
"""
1228+
)
1229+
)
1230+
)
1231+
);
1232+
}
1233+
11641234
@Test
11651235
void addDynamicVersionDependency() {
11661236
rewriteRun(

0 commit comments

Comments
 (0)