Skip to content

Compile v2 migration OpenRewrite recipes with -parameters #6035

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

Conversation

greg-at-moderne
Copy link
Contributor

@greg-at-moderne greg-at-moderne commented Apr 15, 2025

Changing how the OpenRewrite recipes defined in v2-migration/ are compiled. Adding -parameters option to Java compiler.

Motivation and Context

Without it, OpenRewrite might fail with errors like:

[ERROR] Recipe validation error in AddCommentToMethod.methodPattern: is required
[ERROR] Recipe validation error in NumberToDuration.methodPattern: is required

or

java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null
    at org.antlr.v4.runtime.CharStreams.fromString (CharStreams.java:222)
    at org.antlr.v4.runtime.CharStreams.fromString (CharStreams.java:212)
    at org.openrewrite.java.MethodMatcher.<init> (MethodMatcher.java:112)
    at software.amazon.awssdk.v2migration.NumberToDuration$Visitor.<init> (NumberToDuration.java:76)

Explanation

Without -parameters, the code defined in this repo (incl. classes like NumberToDuration.java) gets compiled in a way that does not keep the method argument names in byte code. Which in turn prevents the logic in OpenRewrite for matching YAML arguments to find the corresponding constructor names, i.e.:

  • when a YAML recipe like this one has methodPattern: ... as the argument
  • the OpenRewrite code for inspecting constructors in your recipes sees arg0 and arg1 as the argument names
  • as a consequence, there's no match between methodPattern and arg1, thus the recipe is called with null arguments
  • which in turns results in it failing with an error

Testing

I've executed:

mvn org.openrewrite.maven:rewrite-maven-plugin:dryRun \
  -Drewrite.recipeArtifactCoordinates=software.amazon.awssdk:v2-migration:$VERSION \
  -Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2

against a 3rd-party project using AWS SDK v1 and Maven.

Without the change I saw errors like above.
With the change everything works and a patch is output by OpenRewrite.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@greg-at-moderne greg-at-moderne requested a review from a team as a code owner April 15, 2025 14:20
@sullis
Copy link
Contributor

sullis commented Apr 15, 2025

LGTM :)

@davidh44
Copy link
Contributor

Tested locally as well, do not see the errors anymore. Thanks for the PR! Approved and triggered PR checks, will merge after

@davidh44 davidh44 enabled auto-merge April 15, 2025 20:56
Copy link

@davidh44 davidh44 added this pull request to the merge queue Apr 15, 2025
@aws aws deleted a comment from allcontributors bot Apr 15, 2025
@davidh44
Copy link
Contributor

@all-contributors please add @greg-at-moderne for code

Copy link
Contributor

@davidh44

I've put up a pull request to add @greg-at-moderne! 🎉

Merged via the queue into aws:master with commit 2503122 Apr 15, 2025
10 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants