8000 Fix `FindDependency` to exclude constraints by knutwannheden · Pull Request #5663 · openrewrite/rewrite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix FindDependency to exclude constraints #5663

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Conversation

knutwannheden
Copy link
Contributor
@knutwannheden knutwannheden commented Jun 24, 2025

Changes

  • Modified FindDependency.java to skip matching dependencies inside constraints { } blocks

  • Added isWithinConstraintsBlock() method to traverse AST and detect constraint context

  • Added comprehensive tests verifying constraints are ignored while regular dependencies are still found

  • Switch implementation to use GradleDependency trait, for consistent match and to remove duplicated logic.

  • Fixes gradle.FindDependency also finds Gradle Dependency Constraints #5599

## Changes
- Modified `FindDependency.java` to skip matching dependencies inside `constraints { }` blocks
- Added `isWithinConstraintsBlock()` method to traverse AST and detect constraint context
- Added comprehensive tests verifying constraints are ignored while regular dependencies are still found

Fixes:
 - #5599
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Jun 24, 2025
@shanman190
Copy link
Contributor

@knutwannheden , any reason to not refactor this to the GradleDepencency trait?

@knutwannheden
Copy link
Contributor Author

That would probably make sense.

@timtebeek timtebeek added the bug Something isn't working label Jun 25, 2025
@timtebeek
Copy link
Member

Figured give that a quick shot, as it does clean up the code nicely. But now we seem to fail on .kts. :(

@timtebeek timtebeek moved this from Ready to Review to In Progress in OpenRewrite Jun 25, 2025
@timtebeek timtebeek marked this pull request as draft June 25, 2025 09:41
@shanman190
Copy link
Contributor

@timtebeek , I think I see what the issue is there. With these test cases, they don't add the GradleProject marker which in this case is fine, but it triggers the method matching logic found here which does not match unknown types for kotlin-dsl. That simple fix should restore the test to functioning correctly.

Also note, I believe that this test case is an invalid Gradle Kotlin DSL script, but only marginally so. When applying plugins as a literal I believe it has to be a back ticked string versus a quoted string. I haven't tested this, but I've also never seen a script with a quoted string plugin application like is described here.

@timtebeek timtebeek self-assigned this Jun 25, 2025
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Jun 27, 2025
@timtebeek timtebeek marked this pull request as ready for review June 27, 2025 10:51
@timtebeek
Copy link
Member

Thanks for the help here @shanman190 ; your suggestions as usual where spot on. With that I think this is good to merge: we still pass all the tests we did before, we pass additional tests, and we now use the GradleDependency trait that has also separately passed review and is used in other core recipes to change/upgrade/remove Gradle dependencies, making this nicely consistent.

@timtebeek
Copy link
Member

Previously we were also able to find some older Gradle version dependencies; should the Gradle Matcher trait be updated to allow for those as well?

@shanman190
Copy link
Contributor
shanman190 commented Jun 27, 2025

@timtebeek , the trait prefers using the Gradle model itself for this exact reason. Now the reason why the test fails when attempting to check for older configurations is because those don't exist within the Gradle version that we use in the tooling API by default and for this reason is exactly why the tooling API helped methods expose a version property to the test author.

https://github.com/openrewrite/rewrite-gradle-tooling-model/blob/08f89b0f74ae1140b4aa54dc4576b099eda973d6/model/src/main/java/org/openrewrite/gradle/toolingapi/OpenRewriteModelBuilder.java#L83

https://github.com/openrewrite/rewrite-gradle-tooling-model/blob/08f89b0f74ae1140b4aa54dc4576b099eda973d6/model/src/main/java/org/openrewrite/gradle/toolingapi/Assertions.java#L201

Gradle 7.0 removed these old configurations officially, so since when we don't specify the version we will use Gradle 8.12, this is why those test examples fail under the current conditions.

Comment on lines +88 to +89
"kapt",
"ksp"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not Gradle native configurations and instead come from a custom configuration that come from a plugin not defined within this test case which explains why they fail.

Comment on lines +83 to +86
"debugImplementation",
"releaseImplementation",
"androidTestImplementation",
"featureImplementation",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And these all come from the Android plugin.

"testCompile", // deprecated
"testRuntime" // deprecated
})
void decprecatedMethods(String method) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is presently failing because Gradle 6.9.4 supports Java 15 at the time of its release... We may need to think about how to deal with these sorts of things...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Ready to Review
Development

Successfully merging this pull request may close these issues.

gradle.FindDependency also finds Gradle Dependency Constraints
3 participants
0