Skip to content

Commit eefcfea

Browse files
committed
Fix AddDependency no-op when libs.versions.toml is present on Gradle 9
The `withToolingApi` test helper was mutating `projectDir` to the `gradle/` subdirectory whenever it saw a `gradle/libs.versions.toml` source file. On Gradle 8.x the `-b` flag still pointed the daemon at the real build script, masking the bug; on Gradle 9.x the flag was removed and the daemon evaluated the wrong directory, returning a GradleProject whose `nameToConfiguration` was empty. AddDependency then silently no-opped because `getConfiguration("compileOnly")` returned null. - Drop the projectDir mutation: `gradle/libs.versions.toml` is a catalog inside the existing project, not a project-root marker. - AddDependencyVisitor now emits a `Markup.warn` listing available configurations instead of silently swallowing unknown-configuration cases, so future shape mismatches surface. - Add AddDependencyGradle9CatalogTest covering Gradle 9.0.0 and 8.14.3 with the catalog present. Fixes #7548
1 parent 1684cf4 commit eefcfea

3 files changed

Lines changed: 123 additions & 4 deletions

File tree

rewrite-gradle-tooling-model/model/src/main/java/org/openrewrite/gradle/toolingapi/Assertions.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ public static UncheckedConsumer<List<SourceFile>> withToolingApi(@Nullable Gradl
102102
Toml.Document d = (Toml.Document) sourceFile;
103103
if (d.getSourcePath().startsWith("gradle/") && d.getSourcePath().toString().endsWith(".versions.toml")) {
104104
Path versionCatalog = tempDirectory.resolve(d.getSourcePath());
105-
if (!tempDirectory.equals(versionCatalog.getParent()) && tempDirectory.equals(versionCatalog.getParent().getParent())) {
106-
projectDir = versionCatalog.getParent();
107-
}
108105
Files.createDirectories(versionCatalog.getParent());
109106
Files.write(versionCatalog, d.printAllAsBytes());
110107
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.openrewrite.java.tree.J;
2828
import org.openrewrite.java.tree.JavaSourceFile;
2929
import org.openrewrite.kotlin.tree.K;
30+
import org.openrewrite.marker.Markup;
3031
import org.openrewrite.maven.MavenDownloadingException;
3132
import org.openrewrite.maven.table.MavenMetadataFailures;
3233
import org.openrewrite.maven.tree.GroupArtifact;
@@ -80,7 +81,12 @@ public class AddDependencyVisitor extends JavaIsoVisitor<ExecutionContext> {
8081
}
8182

8283
GradleDependencyConfiguration gdc = gradleProject.getConfiguration(configuration);
83-
if (gdc == null || gdc.findRequestedDependency(groupId, artifactId) != null) {
84+
if (gdc == null) {
85+
return Markup.warn(sourceFile, new IllegalStateException(
86+
"GradleProject has no configuration named '" + configuration +
87+
"'. Available configurations: " + gradleProject.getNameToConfiguration().keySet()));
88+
}
89+
if (gdc.findRequestedDependency(groupId, artifactId) != null) {
8490
return sourceFile;
8591
}
8692

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2026 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.gradle;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.Issue;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.gradle.Assertions.buildGradleKts;
24+
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
25+
import static org.openrewrite.toml.Assertions.toml;
26+
27+
@Issue("https://github.com/openrewrite/rewrite/issues/7548")
28+
class AddDependencyGradle9CatalogTest implements RewriteTest {
29+
30+
@Override
31+
public void defaults(RecipeSpec spec) {
32+
spec.recipe(new AddDependency(
33+
"org.projectlombok", "lombok", "1.18.44",
34+
null, "compileOnly", null,
35+
null, null, null, Boolean.TRUE
36+
));
37+
}
38+
39+
@Test
40+
void gradle9_catalogPresent_addDependencyShouldEditBuildKts() {
41+
rewriteRun(
42+
spec -> spec.beforeRecipe(withToolingApi("9.0.0")),
43+
toml(
44+
"""
45+
[versions]
46+
lombok = "1.18.44"
47+
48+
[libraries]
49+
lombok = { module = "org.projectlombok:lombok", version.ref = "lombok" }
50+
""",
51+
spec -> spec.path("gradle/libs.versions.toml")
52+
),
53+
buildGradleKts(
54+
"""
55+
plugins {
56+
java
57+
}
58+
repositories {
59+
mavenCentral()
60+
}
61+
""",
62+
"""
63+
plugins {
64+
java
65+
}
66+
repositories {
67+
mavenCentral()
68+
}
69+
70+
dependencies {
71+
compileOnly("org.projectlombok:lombok:1.18.44")
72+
}
73+
"""
74+
)
75+
);
76+
}
77+
78+
@Test
79+
void gradle8_catalogPresent_addDependencyEditsBuildKts() {
80+
rewriteRun(
81+
spec -> spec.beforeRecipe(withToolingApi("8.14.3")),
82+
toml(
83+
"""
84+
[versions]
85+
lombok = "1.18.44"
86+
87+
[libraries]
88+
lombok = { module = "org.projectlombok:lombok", version.ref = "lombok" }
89+
""",
90+
spec -> spec.path("gradle/libs.versions.toml")
91+
),
92+
buildGradleKts(
93+
"""
94+
plugins {
95+
java
96+
}
97+
repositories {
98+
mavenCentral()
99+
}
100+
""",
101+
"""
102+
plugins {
103+
java
104+
}
105+
repositories {
106+
mavenCentral()
107+
}
108+
109+
dependencies {
110+
compileOnly("org.projectlombok:lombok:1.18.44")
111+
}
112+
"""
113+
)
114+
);
115+
}
116+
}

0 commit comments

Comments
 (0)