-
Notifications
You must be signed in to change notification settings - Fork 354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added functionality to Gradle ChangeDependency to avoid duplica… #4523
base: main
Are you sure you want to change the base?
feat: Added functionality to Gradle ChangeDependency to avoid duplica… #4523
Conversation
ashakirin
commented
Sep 25, 2024
•
edited by timtebeek
Loading
edited by timtebeek
- Refs: Gradle ChangeDependency behaves incorrectly, if new dependency exists #4521
…te already existing new dependencies Refs: openrewrite#4514
private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionContext ctx) { | ||
List<Expression> depArgs = m.getArguments(); | ||
if (depArgs.get(0) instanceof J.Literal) { | ||
String gav = (String) ((J.Literal) depArgs.get(0)).getValue(); | ||
if (gav != null) { | ||
Dependency original = DependencyStringNotationConverter.parse(gav); | ||
if (original != null && depMatcher.matches(original.getGroupId(), original.getArtifactId())) { | ||
if (original != null && oldDepMatcher.matches(original.getGroupId(), original.getArtifactId())) { | ||
if (isNewDependencyPresent) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This either needs to be evaluated when we have found the dependency OR precomputed by configurations that contain it as a first order dependency.
With the current implementation, if the new dependency was in testImplementation
, but the old is in implementation
, we would incorrectly remove the necessary dependency.
The converse side of this, where the new dependency has now been added to a configuration upstream of the current one, I think right now we should leave as out of scope and for a future cleanup duplicate dependencies recipe instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shanman190: I think the current behaviour need to be fixed anyway, because it deliveries very strange result, if new dependency already exists:
rewriteRun(
spec -> spec.recipe(new ChangeDependency("commons-lang", "commons-lang", "org.apache.commons", "commons-lang3", "3.11.x", null, null)),
buildGradle(
"""
plugins {
id "java-library"
}
repositories {
mavenCentral()
}
dependencies {
implementation "commons-lang:commons-lang:2.6"
implementation group: "commons-lang", name: "commons-lang", version: "2.6"
implementation group: "org.apache.commons", name: "commons-lang3", version: "3.11"
}
""",
"""
plugins {
id "java-library"
}
repositories {
mavenCentral()
}
dependencies {
implementation "org.apache.commons:commons-lang3:2.6"
implementation group: "org.apache.commons", name: "commons-lang3", version: "2.6"
implementation "org.apache.commons:commons-lang3:3.11"
}
"""```
Recipe replaced commons-lang:commons-lang with new groupId and artifactId, but kept the version from old dependency.
I will update the fix to take dependnecy type into account
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shanman190: do you generally interpret this as consistent case to have dependency in testImplementation and the same dependency with older version in implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gradle configurations are a foundational component of Gradle. I was merely using implementation
and testImplementation
as examples. The more specific issue is when the old dependency is in a configuration upstream from the new dependency. (Ex: api
-> implementation
; implementation
-> testImplementation
).
The present implementation doesn't account for the Gradle configuration that the existing dependency is in or if the new dependency is strictly upstream from itself and as a result, illustrated by my example, shows how the current implementation would become broken and can lead directly to code that no longer compiles in these situations. A recipe that would result in code that fails to compile would violate OpenRewrite's do no harm principle.
Just to be clear, what you're wanting to achieve is possible, the decisioning just needs to happen where I'm indicating and taking into account the current configuration (gotten by looking up the configuration using MethodInvocation#getSimpleName
), then to be able to remove more often (ie. The new dependency is not in the same configuration as the old) looking at the configurations upstream to see if the new dependency is present.
If this still isn't clear enough, I'll add a couple of test cases that I would expect to pass to help illustrate the problem and intended positive and negative cases.
This likely got some conflicts as a result of 9936a7a ; is there anything left to fix after that change? Or perhaps a different direction you'd now recommend @shanman190 ? |
@timtebeek, this PR still needs some rework based upon my last comment here: This also applies probably to more of the recipes than just |