-
Notifications
You must be signed in to change notification settings - Fork 380
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
Make UpgradeDependencyVersion
recipe runnable with defined repositories in the buildscript only
#4988
Make UpgradeDependencyVersion
recipe runnable with defined repositories in the buildscript only
#4988
Conversation
UpgradeDependencyVersion
recipe runnable with defined repositories in the buildscript only
rewrite-gradle/src/main/java/org/openrewrite/gradle/internal/DefaultImportsCustomizer.java
Outdated
Show resolved
Hide resolved
rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java
Outdated
Show resolved
Hide resolved
rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java
Outdated
Show resolved
Hide resolved
rewrite-gradle/src/main/java/org/openrewrite/gradle/DependencyVersionSelector.java
Outdated
Show resolved
Hide resolved
Overall, I'm a little puzzled with this PR... In the description it seems like an extension variable defined in the buildscript block fails to update the version contained within the variable, but there is explicitly the UpgradeDependencyVersionTest#upgradesVariablesDefinedInExtraProperties test that verifies extension properties both on the buildscript as well as the project are updated (notice that two different variable names were used explicitly to guarantee one of them was not accidentally updated as an inadvertent side effect). |
Ah, I should have given a little more context in the motivation part of this PR. Well, let's work it out here. Yes there was a test, but it should have been two tests. So the original test looked like: @Test
void upgradesVariablesDefinedInExtraProperties() {
rewriteRun(
buildGradle(
"""
buildscript {
ext { guavaVersion = "29.0-jre" }
repositories { mavenCentral() } // <-- repositories in extension of buildscript
dependencies { classpath("com.google.guava:guava:${guavaVersion}") }
}
plugins { id "java" }
ext { guavaVersion2 = "29.0-jre" }
repositories { mavenCentral() } // <-- repositories in root
dependencies { implementation "com.google.guava:guava:${guavaVersion2}" }
""",
"""
<upgraded gradle script>
"""
)
);
} The problem here is the @Test
void upgradesVariablesDefinedInExtraPropertiesWithBuildscript() {
rewriteRun(
buildGradle(
"""
buildscript {
ext { guavaVersion = "29.0-jre" }
repositories { mavenCentral() } // <-- repositories in extension of build script
dependencies { classpath("com.google.guava:guava:${guavaVersion}") }
}
""",
"""
<upgraded gradle script>
"""
)
);
} You would end up with following error: "buildscript {
ext { /*~~(com.google.guava:guava failed. Unable to download metadata.)~~>*/guavaVersion = "29.0-jre" }
repositories { mavenCentral() } // <-- repositories in extension of build script
dependencies { classpath("com.google.guava:guava:${guavaVersion}") }
}" The reason this happens is because the retrieval of the repositories was done in two steps: try {
// ..
String selectedVersion = Optional.ofNullable(selector.select(gav, null, newVersion, versionPattern, ctx))
.orElse(selector.select(gav, "classpath", newVersion, versionPattern, ctx));
// ..
return newVersion ?: oldVersion;
} catch (MavenDownloadingException e) {
// error: Unable to download metadata.
return e.warn(a);
} The idea behind it: first use a That why I moved that idea to the
But as you mentioned a couple hours ago, this is invalid from a Gradle standpoint. So I'll revert my changes to the DependencyVersionSelector and change the recipe a little more (putting back the select(null) => select("classpath"), but without the bug). Edit: That's done now, new version of the PR could be reviewed if you want to. Thanks for the review, my Gradle knowledge is not that big. So really nice to have a review from someone that knows Gradle very well! |
Aha! That makes a lot more sense as to the where the bug in behavior comes from. Thanks for clarifying that context! |
rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java
Show resolved
Hide resolved
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.
So long as Shannon's comment is addressed this looks OK to me
…port-for-repositories-in-build-gradle
…port-for-repositories-in-build-gradle
What's changed?
UpgradeDependencyVersion
recipe can run with defined repositories in the buildscript onlyUpdateVariable
class in theUpgradeDependencyVersion
recipeDefaultImportsCustomizer
What's your motivation?
The
UpgradeDependencyVersion
recipe did not work with a setup like:Why: click.