Skip to content

Commit e71e9af

Browse files
bmuschkoclaude
andauthored
Allow Gradle AddDependency to work with explicit configuration without Java sources (#6624)
* Allow Gradle AddDependency to work with explicit configuration without Java sources * Gradle AddDependency defaults to implementation when onlyIfUsing is null When onlyIfUsing is not set and no explicit configuration is provided, default to "implementation" configuration. This aligns with Maven's AddDependency behavior which defaults to "compile" scope in the same scenario. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent f8a8c04 commit e71e9af

2 files changed

Lines changed: 95 additions & 13 deletions

File tree

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

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ private boolean usesType(SourceFile sourceFile, ExecutionContext ctx) {
184184

185185
@Override
186186
public TreeVisitor<?, ExecutionContext> getVisitor(Scanned acc) {
187-
return Preconditions.check(!acc.configurationsByProject.isEmpty(),
187+
// Allow when configuration is explicitly provided, when onlyIfUsing is not set (default to "implementation"),
188+
// or when source files were scanned
189+
boolean hasExplicitConfiguration = !StringUtils.isBlank(configuration);
190+
return Preconditions.check(hasExplicitConfiguration || onlyIfUsing == null || !acc.configurationsByProject.isEmpty(),
188191
Preconditions.check(new IsBuildGradle<>(), new JavaIsoVisitor<ExecutionContext>() {
189192

190193
@Override
@@ -195,27 +198,40 @@ public TreeVisitor<?, ExecutionContext> getVisitor(Scanned acc) {
195198
JavaSourceFile s = (JavaSourceFile) tree;
196199
Optional<JavaProject> maybeJp = s.getMarkers().findFirst(JavaProject.class);
197200
Optional<GradleProject> maybeGp = s.getMarkers().findFirst(GradleProject.class);
198-
if (!maybeJp.isPresent() ||
199-
(onlyIfUsing != null && !acc.usingType.getOrDefault(maybeJp.get(), false)) || !acc.configurationsByProject.containsKey(maybeJp.get()) ||
200-
!maybeGp.isPresent()) {
201+
202+
if (!maybeGp.isPresent()) {
201203
return s;
202204
}
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())) {
210+
return s;
211+
}
212+
}
203213

204-
JavaProject jp = maybeJp.get();
214+
JavaProject jp = maybeJp.orElse(null);
205215
GradleProject gp = maybeGp.get();
206216

207-
Set<String> resolvedConfigurations = StringUtils.isBlank(configuration) ?
208-
acc.configurationsByProject.getOrDefault(jp, new HashSet<>()) :
209-
new HashSet<>(singletonList(configuration));
217+
Set<String> resolvedConfigurations = hasExplicitConfiguration ?
218+
new HashSet<>(singletonList(configuration)) :
219+
acc.configurationsByProject.getOrDefault(jp, new HashSet<>());
210220
if (resolvedConfigurations.isEmpty()) {
211221
resolvedConfigurations.add("implementation");
212222
}
213223

214-
GradleConfigurationFilter gradleConfigurationFilter = new GradleConfigurationFilter(gp, resolvedConfigurations);
215-
gradleConfigurationFilter.removeTransitiveConfigurations();
216-
gradleConfigurationFilter.removeConfigurationsContainingDependency(new GroupArtifact(groupId, artifactId));
217-
gradleConfigurationFilter.removeConfigurationsContainingTransitiveDependency(new GroupArtifact(groupId, artifactId));
218-
resolvedConfigurations = gradleConfigurationFilter.getFilteredConfigurations();
224+
// Only apply filtering when the configuration is known to the GradleProject
225+
// (which requires Java sources to have been scanned with the tooling API)
226+
boolean configurationKnown = resolvedConfigurations.stream()
227+
.anyMatch(c -> gp.getConfiguration(c) != null);
228+
if (configurationKnown) {
229+
GradleConfigurationFilter gradleConfigurationFilter = new GradleConfigurationFilter(gp, resolvedConfigurations);
230+
gradleConfigurationFilter.removeTransitiveConfigurations();
231+
gradleConfigurationFilter.removeConfigurationsContainingDependency(new GroupArtifact(groupId, artifactId));
232+
gradleConfigurationFilter.removeConfigurationsContainingTransitiveDependency(new GroupArtifact(groupId, artifactId));
233+
resolvedConfigurations = gradleConfigurationFilter.getFilteredConfigurations();
234+
}
219235

220236
if (resolvedConfigurations.isEmpty()) {
221237
return s;

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,72 @@ void addUnconditionally() {
13861386
);
13871387
}
13881388

1389+
@Test
1390+
void addWithExplicitConfigurationAndNoJavaSources() {
1391+
rewriteRun(
1392+
spec -> spec.recipe(addDependency("com.google.guava:guava:29.0-jre", null, "implementation")),
1393+
mavenProject("project",
1394+
buildGradle(
1395+
"""
1396+
plugins {
1397+
id 'java'
1398+
}
1399+
1400+
repositories {
1401+
mavenCentral()
1402+
}
1403+
""",
1404+
"""
1405+
plugins {
1406+
id 'java'
1407+
}
1408+
1409+
repositories {
1410+
mavenCentral()
1411+
}
1412+
1413+
dependencies {
1414+
implementation "com.google.guava:guava:29.0-jre"
1415+
}
1416+
"""
1417+
)
1418+
)
1419+
);
1420+
}
1421+
1422+
@Test
1423+
void addWithNoTypeFilterAndNoJavaSourcesDefaultsToImplementation() {
1424+
rewriteRun(
1425+
spec -> spec.recipe(addDependency("com.google.guava:guava:29.0-jre", null, null)),
1426+
mavenProject("project",
1427+
buildGradle(
1428+
"""
1429+
plugins {
1430+
id 'java'
1431+
}
1432+
1433+
repositories {
1434+
mavenCentral()
1435+
}
1436+
""",
1437+
"""
1438+
plugins {
1439+
id 'java'
1440+
}
1441+
1442+
repositories {
1443+
mavenCentral()
1444+
}
1445+
1446+
dependencies {
1447+
implementation "com.google.guava:guava:29.0-jre"
1448+
}
1449+
"""
1450+
)
1451+
)
1452+
);
1453+
}
1454+
13891455
@Issue("https://github.com/moderneinc/customer-requests/issues/792")
13901456
@Nested
13911457
class AddToJVMTestSuite {

0 commit comments

Comments
 (0)